"inhereting" a struct?
-
I have some classes I am writing that represent units of measurement, and I am trying to do this: each unit (Inch, Foot, Yard) is essentially a decimal, but has different properties, eg. a foot has 12 inches, a yard has 3 "foots" and 36 inches, so on. Is there a way to say "this class is a decimal" in a way similar to inheretence? I have tried using the same structure of a decimal, including all of the interfaces it inherets, but that was a god awful mess. Any ideas/suggestions?
______________________ Oh Hamburgers!
-
I have some classes I am writing that represent units of measurement, and I am trying to do this: each unit (Inch, Foot, Yard) is essentially a decimal, but has different properties, eg. a foot has 12 inches, a yard has 3 "foots" and 36 inches, so on. Is there a way to say "this class is a decimal" in a way similar to inheretence? I have tried using the same structure of a decimal, including all of the interfaces it inherets, but that was a god awful mess. Any ideas/suggestions?
______________________ Oh Hamburgers!
I would imagine that creating wrappers around the decimal struct would do the job. I did a similar thing for binary, octal and hexadecimal with an int here[^]. [Edit] I found this interesting so I've tried a little bit of coding - figured I may as well share it with you! Depending on what you're doing with this data, this may help. This (incomplete but working) struct wraps three ints and does some basic stuff. [/Edit]
public struct Imperial
{
public static readonly Imperial Empty = new Imperial();
public Imperial(int inches)
{
m_Yards = 0; // Set default values
m_Feet = 0;
m_Inches = 0;
if (inches != 0)
{
m_Yards = inches / 36;
int remainder = inches % (m_Yards * 36);
if (remainder != 0)
{
m_Feet = remainder / 12;
if (m_Feet != 0)
m_Inches = remainder % (m_Feet * 12);
else
m_Inches = remainder;
}
}
}
public Imperial(int yards, int feet, int inches)
{
m_Yards = yards;
m_Feet = feet;
m_Inches = inches;
}
public static implicit operator Imperial(int value)
{
return new Imperial(value);
}
public static Imperial operator +(Imperial value1, Imperial value2)
{
return new Imperial(value1.GetTotalInches() + value2.GetTotalInches());
}
private int m_Yards;
private int m_Feet;
private int m_Inches;
public int Yards
{
get { return m_Yards; }
}
public int Feet
{
get { return m_Feet; }
}
public int Inches
{
get { return m_Inches; }
}
public int GetTotalInches()
{
return ((m_Yards * 36) + (m_Feet * 12) + m_Inches);
}
public static Imperial MillimetersToImperial(int millimeters)
{
return new Imperial((int)(millimeters * 0.0393700787));
}
public override string ToString()
{
return string.Format("{0} yards, {1} feet, {2} inches", m_Yards, m_Feet, m_Inches);
}
}Dave
BTW, in software, hope and pray is not a viable strategy. ( -
I have some classes I am writing that represent units of measurement, and I am trying to do this: each unit (Inch, Foot, Yard) is essentially a decimal, but has different properties, eg. a foot has 12 inches, a yard has 3 "foots" and 36 inches, so on. Is there a way to say "this class is a decimal" in a way similar to inheretence? I have tried using the same structure of a decimal, including all of the interfaces it inherets, but that was a god awful mess. Any ideas/suggestions?
______________________ Oh Hamburgers!
If you have a logical way of doing the conversion, you could add implicit conversions to and from a numerical data type. That way you can assign a numerical value to a measurement variable, and use a measurement value where a numerical value is required. (I'm not sure that Decimal is the right choise of numerical data type though. A double might be a better fit.) However, it looks like you would have to specify the unit for the value to make sense as a measurement?
Despite everything, the person most likely to be fooling you next is yourself.
-
I would imagine that creating wrappers around the decimal struct would do the job. I did a similar thing for binary, octal and hexadecimal with an int here[^]. [Edit] I found this interesting so I've tried a little bit of coding - figured I may as well share it with you! Depending on what you're doing with this data, this may help. This (incomplete but working) struct wraps three ints and does some basic stuff. [/Edit]
public struct Imperial
{
public static readonly Imperial Empty = new Imperial();
public Imperial(int inches)
{
m_Yards = 0; // Set default values
m_Feet = 0;
m_Inches = 0;
if (inches != 0)
{
m_Yards = inches / 36;
int remainder = inches % (m_Yards * 36);
if (remainder != 0)
{
m_Feet = remainder / 12;
if (m_Feet != 0)
m_Inches = remainder % (m_Feet * 12);
else
m_Inches = remainder;
}
}
}
public Imperial(int yards, int feet, int inches)
{
m_Yards = yards;
m_Feet = feet;
m_Inches = inches;
}
public static implicit operator Imperial(int value)
{
return new Imperial(value);
}
public static Imperial operator +(Imperial value1, Imperial value2)
{
return new Imperial(value1.GetTotalInches() + value2.GetTotalInches());
}
private int m_Yards;
private int m_Feet;
private int m_Inches;
public int Yards
{
get { return m_Yards; }
}
public int Feet
{
get { return m_Feet; }
}
public int Inches
{
get { return m_Inches; }
}
public int GetTotalInches()
{
return ((m_Yards * 36) + (m_Feet * 12) + m_Inches);
}
public static Imperial MillimetersToImperial(int millimeters)
{
return new Imperial((int)(millimeters * 0.0393700787));
}
public override string ToString()
{
return string.Format("{0} yards, {1} feet, {2} inches", m_Yards, m_Feet, m_Inches);
}
}Dave
BTW, in software, hope and pray is not a viable strategy. ( -
Awesome. The method mentione din the article is the one i went with. I created a class that handles the basics like converting and constructors, then inheret it with the other classes. Works like a charm :D
______________________ Oh Hamburgers!
Cool - glad you found it useful. If you haven't already, comment and/or vote on the article! :-D
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) -
I have some classes I am writing that represent units of measurement, and I am trying to do this: each unit (Inch, Foot, Yard) is essentially a decimal, but has different properties, eg. a foot has 12 inches, a yard has 3 "foots" and 36 inches, so on. Is there a way to say "this class is a decimal" in a way similar to inheretence? I have tried using the same structure of a decimal, including all of the interfaces it inherets, but that was a god awful mess. Any ideas/suggestions?
______________________ Oh Hamburgers!
Your message title is misleading. You cannot "inherit" a
struct
."Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Your message title is misleading. You cannot "inherit" a
struct
."Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001