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.
Use a struct, but make it immutable (all properties are get-only).
Properties should include at least:
Methods should include at least:
Declare the private backing field as:
private readonly double _meters;
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);
}
}
I've written self-contained unit converter classes before, but I don't know of a good one for .NET that is public.
That being said, it's quite easy to write - Just make a struct that can be constructed from inches or feet, and convertable to both.
public struct Distance
{
private Distance(int inches)
{
this.totalInches = inches;
}
private int totalInches;
public int Inches { get { return this.totalInches % 12; } }
public int Feet { get { return this.totalInches / 12; } }
public static Distance FromInches(int inches)
{
return new Distance(inches);
}
public static Distance FromFeet(int feet)
{
return new Distance(feet * 12);
}
public static Distance FromFeetAndInches(int feet, int inches)
{
return new Distance(feet * 12 + inches);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With