Click here to Skip to main content
15,902,299 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am building app with a serial port and file methods. When I make the classes should I separate all the attributes into subclasses?

I am using this code in Windows Forms and might later use in Windows Presentation Foundation.

C#
class Car
{
  // is an Array of Arrays
  // I must separate the Attributes for Each of the inherited classes 
  //They are Jagged Arrays actually
  // each attribute must be worked on individually
  //this is why I ask this question
  // I also must use a CRC function in here as I save and write the files
  // along with a method for writing to a serial port
  // Am I on the right Track? or is it better to make them
  // all attributes of //the parent class only?>
  //Please Excuse I am not an Expert
  class ArrayOne Car
  {
    class Door ArrayOne
    {
      Byte handle [3]
    }
  }
  class ArrayTwo Car
  {}
  class ArrayThree Car
  {}
}


I want to make this as easy or simple as possible without losing the ability to modify attributes. I also wish to reserve the possibility of attachimg a database to the application later. Thank You in advance for any or all advice and help. This is my first real program.
Posted
Updated 12-Feb-10 6:45am
v6

1 solution

From the example, the definition of the classes inside of other classes may not be what you want. I think you defined 5 classes, inheritance like this:
Car           (inherits from Object)
+-ArrayOne    (inherits from Car)
| +-Door      (inherits from ArrayOne)(has data Byte handle[3])
|
+-ArrayTwo    (inherits from Car)
+-ArrayThree  (inherits from Car)

You can see that only Door holds an attribute (handle).
None of the other objects have attributes.
Figure out what attributes, and functions you want every Car to have, and define it there. Only put common code/data there, and specific code in the sub classes.
--------A different interpretation------------
I think this is your intent, but not sure
C#
;class Door
{
   public Byte[3] handle
   public void Open(){open door here}
   public void Close(){close door here}
}
class Car
{
   private int m_PassengerCapacity = 4;
   private int m_numberPassengers = 0;
   abstract public Door[] Doors   {get{}set{}}
   virtual public bool LoadPeople(int numPeople)
   {
      if(numPeople < 0)
         return UnloadPeople(numPeople);
      if(m_PassengerCapacity <= (m_numberOfPassengers+numPeople))
         return false;
      OpenDoors();
      m_numberOfPassengers += numPeople
      return true
   }
}
class DoesStuff
{
   public Car[] ArrayOne = null;
   public Car[] ArrayTwo = null;
   public Car[] ArrayThree = null;
   public void InitArrays(int l1, int l2, int l3)
   {
      ArrayOne = new Car[l1];
      ArrayTwo = new Car[l2];
      ArrayThree = new Car[l3];
      for(int idx=0;idx < l1; idx++)
         ArrayOne[idx] = new Car();
      for(int idx=0;idx < l2; idx++)
         ArrayTwo[idx] = new Car();
      for(int idx=0;idx < l3; idx++)
         ArrayThree[idx] = new Car();
   }
}


Number of doors is specific to car type.
How many doors to open to load people can depend on car type, num people and existing num passengers.
Door subclasses can handle opening and closing differently for gull wing, hinge or sliding doors.

This won't compile and is not complete, but I hope it gets the idea across.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900