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. Class Design Question

Class Design Question

Scheduled Pinned Locked Moved C#
questioncomdesignoopdiscussion
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.
  • M Offline
    M Offline
    Mark Sanders
    wrote on last edited by
    #1

    I need to create two different classes which differ only in the data member types. The first class is an "IntegerPoint" class, using integer values for the x and y. The second class is a "DoublePoint" class, using double values for the x and y. These classes have extensive operator, method, etc. functionality. But all of the functionality is identical, again the only difference is the data member type. Does anyone know what the "best practice" is for handling this type of situation? Can some form of inheritance be used here? Mark Sanders sanderssolutions.com

    P R N 3 Replies Last reply
    0
    • M Mark Sanders

      I need to create two different classes which differ only in the data member types. The first class is an "IntegerPoint" class, using integer values for the x and y. The second class is a "DoublePoint" class, using double values for the x and y. These classes have extensive operator, method, etc. functionality. But all of the functionality is identical, again the only difference is the data member type. Does anyone know what the "best practice" is for handling this type of situation? Can some form of inheritance be used here? Mark Sanders sanderssolutions.com

      P Offline
      P Offline
      Philip Fitzsimons
      wrote on last edited by
      #2

      first a question: do you need two implementations - are you having two because you are worrying about performance? why not just have a DoubmePoint class - see if you have a problem then. if that does not work then: create a class called SanderNumber which can use either a integer or a double. then create your abstract class SanderNumberUtil that uses SanderNumber. then create a concrete class for integer and double. This puts most of the complexity into SanderNumber - but allows you to use it polymorphically. hope this helps.


      "When the only tool you have is a hammer, a sore thumb you will have."

      M 1 Reply Last reply
      0
      • P Philip Fitzsimons

        first a question: do you need two implementations - are you having two because you are worrying about performance? why not just have a DoubmePoint class - see if you have a problem then. if that does not work then: create a class called SanderNumber which can use either a integer or a double. then create your abstract class SanderNumberUtil that uses SanderNumber. then create a concrete class for integer and double. This puts most of the complexity into SanderNumber - but allows you to use it polymorphically. hope this helps.


        "When the only tool you have is a hammer, a sore thumb you will have."

        M Offline
        M Offline
        Mark Sanders
        wrote on last edited by
        #3

        why not just have a DoubmePoint class I am not sure what you mean by "DoubmePoint class". if that does not work then I somewhat understand the theory of what your second solution is but I also can't really invision it very well. Could you give a little more detail? Mark Sanders sanderssolutions.com

        P 1 Reply Last reply
        0
        • M Mark Sanders

          why not just have a DoubmePoint class I am not sure what you mean by "DoubmePoint class". if that does not work then I somewhat understand the theory of what your second solution is but I also can't really invision it very well. Could you give a little more detail? Mark Sanders sanderssolutions.com

          P Offline
          P Offline
          Philip Fitzsimons
          wrote on last edited by
          #4

          type - i meant why not just have the DoublePoint class - do you really need the integer class? otherwise class SanderNumber {  bool _isInteger = true;  int _valueInt;  double _valueDouble;  public SanderNumber(int value)  {   _valueInt = value;  }  public SanderNumber(double value)  {   _valueDouble = value;   _isInteger = false;  }  public int SomeFunction()  {   // if (!_isInteger) throw(some exception)   return _valueInt;  }  public double SomeFunction()  {   // if (_isInteger) throw(some exception)   return _valueDouble;  } } class SanderUtil() {  SanderNumber _number;  public SanderNumber(SanderNumber value)  {   _number = value;  }  public SomeCleverStuff()  {   if (_number.SomeFunction()) > 24.0)    return;  } } this would give you what you want and would be type safe.....


          "When the only tool you have is a hammer, a sore thumb you will have."

          1 Reply Last reply
          0
          • M Mark Sanders

            I need to create two different classes which differ only in the data member types. The first class is an "IntegerPoint" class, using integer values for the x and y. The second class is a "DoublePoint" class, using double values for the x and y. These classes have extensive operator, method, etc. functionality. But all of the functionality is identical, again the only difference is the data member type. Does anyone know what the "best practice" is for handling this type of situation? Can some form of inheritance be used here? Mark Sanders sanderssolutions.com

            R Offline
            R Offline
            Ranjan Banerji
            wrote on last edited by
            #5

            You could implement an Interface for your methods which the two classes will inherit and have to implement. That is if the implementation of the methods are different.

            M 1 Reply Last reply
            0
            • R Ranjan Banerji

              You could implement an Interface for your methods which the two classes will inherit and have to implement. That is if the implementation of the methods are different.

              M Offline
              M Offline
              Mark Sanders
              wrote on last edited by
              #6

              The implementations are identical. Mark Sanders sanderssolutions.com

              1 Reply Last reply
              0
              • M Mark Sanders

                I need to create two different classes which differ only in the data member types. The first class is an "IntegerPoint" class, using integer values for the x and y. The second class is a "DoublePoint" class, using double values for the x and y. These classes have extensive operator, method, etc. functionality. But all of the functionality is identical, again the only difference is the data member type. Does anyone know what the "best practice" is for handling this type of situation? Can some form of inheritance be used here? Mark Sanders sanderssolutions.com

                N Offline
                N Offline
                Nnamdi Onyeyiri
                wrote on last edited by
                #7

                you could make the integerPoint class, then derive the doublepoint class from that, and override the variables/properties inherited....just a thought.

                | Website: http://www.onyeyiri.co.uk | Sonork: 100.21142 : TheEclypse | "If a dolar was a chicken would the chicken be evil?"

                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