Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 635179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:22:35+00:00 2026-05-13T20:22:35+00:00

Does anyone know of any examples or code that shows a class or struct

  • 0

Does anyone know of any examples or code that shows a class or struct that can be used for distance like the DateTime struct? I need to be able to add, subtract, and display the data in feet and inches and using conversion methods just gets messy. A class or struct would be perfect, but I came across nothing in my searches.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:22:35+00:00Added an answer on May 13, 2026 at 8:22 pm

    Use a struct, but make it immutable (all properties are get-only).

    Properties should include at least:

    • TotalFeet
    • TotalInches

    Methods should include at least:

    • FromFeet (static)
    • FromInches (static)

    Declare the private backing field as:

    private readonly double _meters;
    

    Edit: Maybe something like this.

    public struct Distance : IEquatable<Distance>, IComparable<Distance>
    {
        private static readonly double MetersPerKilometer = 1000.0;
        private static readonly double CentimetersPerMeter = 100.0;
        private static readonly double CentimetersPerInch = 2.54;
        private static readonly double InchesPerFoot = 12.0;
        private static readonly double FeetPerYard = 3.0;
        private static readonly double FeetPerMeter = CentimetersPerMeter / (CentimetersPerInch * InchesPerFoot);
        private static readonly double InchesPerMeter = CentimetersPerMeter / CentimetersPerInch;
    
        private readonly double _meters;
    
        public Distance(double meters)
        {
            this._meters = meters;
        }
    
        public double TotalKilometers
        {
            get
            {
                return _meters / MetersPerKilometer;
            }
        }
    
        public double TotalMeters
        {
            get
            {
                return _meters;
            }
        }
    
        public double TotalCentimeters
        {
            get
            {
                return _meters * CentimetersPerMeter;
            }
        }
    
        public double TotalYards
        {
            get
            {
                return _meters * FeetPerMeter / FeetPerYard;
            }
        }
    
        public double TotalFeet
        {
            get
            {
                return _meters * FeetPerMeter;
            }
        }
    
        public double TotalInches
        {
            get
            {
                return _meters * InchesPerMeter;
            }
        }
    
        public static Distance FromKilometers(double value)
        {
            return new Distance(value * MetersPerKilometer);
        }
    
        public static Distance FromMeters(double value)
        {
            return new Distance(value);
        }
    
        public static Distance FromCentimeters(double value)
        {
            return new Distance(value / CentimetersPerMeter);
        }
    
        public static Distance FromYards(double value)
        {
            return new Distance(value * FeetPerYard / FeetPerMeter);
        }
    
        public static Distance FromFeet(double value)
        {
            return new Distance(value / FeetPerMeter);
        }
    
        public static Distance FromInches(double value)
        {
            return new Distance(value / InchesPerMeter);
        }
    
        public static Distance operator +(Distance a, Distance b)
        {
            return new Distance(a._meters + b._meters);
        }
    
        public static Distance operator -(Distance a, Distance b)
        {
            return new Distance(a._meters - b._meters);
        }
    
        public static Distance operator -(Distance a)
        {
            return new Distance(-a._meters);
        }
    
        public override bool Equals(object obj)
        {
            if (!(obj is Distance))
                return false;
    
            return Equals((Distance)obj);
        }
    
        public bool Equals(Distance other)
        {
            return this._meters == other._meters;
        }
    
        public int CompareTo(Distance other)
        {
            return this._meters.CompareTo(other._meters);
        }
    
        public override int GetHashCode()
        {
            return _meters.GetHashCode();
        }
    
        public override string ToString()
        {
            return string.Format("{0}[m]", TotalMeters);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know any site or book that presents problems like python challenge ,
Does anyone know of a walk-through or any examples of any code to setup
Does anyone know of any resources, examples or tutorials about testing ember.js apps ?
does anyone know any sort of app that lets users visit a page on
Does anyone know of any code samples on how to use DotNetOpenAuth with Facebook
Does anyone know of a class in Java that has a list of elements,
does any one know if, for example, I'm writing in c++ the following code:
Does anyone know any good resources with tasks or problems to get practice in
Does anyone know any more details about google's web-crawler (aka GoogleBot)? I was curious
Does anyone know any open source project or adapter for MySQL-AdWhirl? We try to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.