Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. "inhereting" a struct?

"inhereting" a struct?

Scheduled Pinned Locked Moved C#
question
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vodstok
    wrote on last edited by
    #1

    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!

    D G realJSOPR 3 Replies Last reply
    0
    • V Vodstok

      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!

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      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. (

      V 1 Reply Last reply
      0
      • V Vodstok

        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!

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • D DaveyM69

          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. (

          V Offline
          V Offline
          Vodstok
          wrote on last edited by
          #4

          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!

          D 1 Reply Last reply
          0
          • V Vodstok

            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!

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

            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)

            1 Reply Last reply
            0
            • V Vodstok

              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!

              realJSOPR Offline
              realJSOPR Offline
              realJSOP
              wrote on last edited by
              #6

              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

              V 1 Reply Last reply
              0
              • realJSOPR realJSOP

                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

                V Offline
                V Offline
                Vodstok
                wrote on last edited by
                #7

                That was why it was in quotes.

                ______________________ Oh Hamburgers!

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups