Click here to Skip to main content
15,914,820 members
Home / Discussions / C#
   

C#

 
AnswerRe: "inhereting" a struct? [modified] Pin
DaveyM6923-Oct-08 2:22
professionalDaveyM6923-Oct-08 2:22 
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. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

GeneralRe: "inhereting" a struct? Pin
Vodstok23-Oct-08 3:29
Vodstok23-Oct-08 3:29 
GeneralRe: "inhereting" a struct? Pin
DaveyM6923-Oct-08 4:04
professionalDaveyM6923-Oct-08 4:04 
AnswerRe: "inhereting" a struct? Pin
Guffa23-Oct-08 2:30
Guffa23-Oct-08 2:30 
AnswerRe: "inhereting" a struct? Pin
#realJSOP23-Oct-08 4:32
professional#realJSOP23-Oct-08 4:32 
GeneralRe: "inhereting" a struct? Pin
Vodstok23-Oct-08 4:43
Vodstok23-Oct-08 4:43 
QuestionRichTextBox RightClick [modified] Pin
V.23-Oct-08 2:10
professionalV.23-Oct-08 2:10 
AnswerRe: RichTextBox RightClick Pin
JoeSharp23-Oct-08 4:22
JoeSharp23-Oct-08 4:22 
QuestionUnbound RadioButtonList Selected Item Problem Pin
amit201123-Oct-08 1:59
amit201123-Oct-08 1:59 
QuestionHelp me in exception handaling Pin
Shantonu23-Oct-08 1:38
Shantonu23-Oct-08 1:38 
QuestionRe: Help me in exception handaling Pin
Eddy Vluggen23-Oct-08 2:02
professionalEddy Vluggen23-Oct-08 2:02 
AnswerRe: Help me in exception handaling Pin
josephbhaskar23-Oct-08 3:39
josephbhaskar23-Oct-08 3:39 
Questionsoundx coding Pin
Preeti197923-Oct-08 1:36
Preeti197923-Oct-08 1:36 
AnswerRe: soundx coding Pin
Eddy Vluggen23-Oct-08 1:42
professionalEddy Vluggen23-Oct-08 1:42 
QuestionWhy if i dont convert a integer to string the output will be coming? Pin
Samiullah23-Oct-08 0:43
Samiullah23-Oct-08 0:43 
AnswerRe: Why if i dont convert a integer to string the output will be coming? Pin
Simon P Stevens23-Oct-08 0:54
Simon P Stevens23-Oct-08 0:54 
GeneralRe: Why if i dont convert a integer to string the output will be coming? Pin
Samiullah23-Oct-08 1:56
Samiullah23-Oct-08 1:56 
GeneralRe: Why if i dont convert a integer to string the output will be coming? Pin
Guffa23-Oct-08 2:33
Guffa23-Oct-08 2:33 
GeneralRe: Why if i dont convert a integer to string the output will be coming? Pin
Simon P Stevens23-Oct-08 2:35
Simon P Stevens23-Oct-08 2:35 
GeneralRe: Why if i dont convert a integer to string the output will be coming? Pin
Dan Neely23-Oct-08 4:42
Dan Neely23-Oct-08 4:42 
QuestionTCPListener and SOAP Pin
Wilkov23-Oct-08 0:22
Wilkov23-Oct-08 0:22 
QuestionHow to draw combine shapes like Visio? Pin
Duong Tien Nam23-Oct-08 0:09
Duong Tien Nam23-Oct-08 0:09 
Question3 layers model documents Pin
hockymot2008_200923-Oct-08 0:01
hockymot2008_200923-Oct-08 0:01 
AnswerRe: 3 layers model documents Pin
AhsanS23-Oct-08 0:06
AhsanS23-Oct-08 0:06 
GeneralRe: 3 layers model documents Pin
ArghavanMostafa9-Jun-11 2:21
ArghavanMostafa9-Jun-11 2:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.