Hey Guys 'n' Gals,
I'm somewhat of a new-comer to ASP.NET and OOP for that matter and
just wanted a sanity check on the use of classes.
Here is my example:
Let's say have have 2 classes named ClassA and ClassB
public class ClassA
{
public string sData1 { get; set; }
public string sData2; { get; set; }
public ClassA()
{
}
}
public class ClassB
{
public string sData1 { get; set; }
public string sData2 { get; set; }
public ClassB()
{
}
}
Now - I have a third class, ClassC and I want it to have members which
are of the ClassA and ClassB - is this the correct way to define this?
public class ClassC
{
public ClassA cA = new ClassA();
public ClassB cB = new ClassB();
public string sData1 = { get; set; }
public string sData2 = { get; set; }
public ClassC()
{
}
}
Thanks for your help,
Possibly, it really depends.
Example of proper usage:
public class Address
{
//Imagine members here
}
public class PhoneNumber
{
//Imagine members here
}
public class person
{
public Address Address { get; set; }
public PhoneNumber PhoneNumber [ get; set; }
}
This works, as a person has both an address and a phonenumber, at least
most people, and certainly all customers of your site (I hope). ;-)
If, instead, you are talking more like "a dog is an animal" and "a dog is a
mammal", you are better to make a hierarchy, where mammal inherits from
animal and dog inherits from mammal. You can, of course, take this ad
nauseum.
If you find that the class truly is both an a and a b, there are three
possibilities (at least with .NET which only allows one parent class):
1. You need to create two interfaces and use both in your class
2. Both a and b should inherit from the same class or interface (just
noting your set up) and you should create a collection of the parent class
(this may be stretching things)
3. You need to rearchitect your solution to better set up the hierarchy
If you see a path to rearchitect, but cannot do it today, one solution is
to set up C as such 9assumign a and b do not really have field names sData1
and sData2.
Example:
public class ClassA
{
public string field1; { get; set; }
public string field2; { get; set; }
public ClassA()
{
}
}
public class ClassB
{
public string fieldA; { get; set; }
public string fieldB; { get; set; }
public ClassB()
{
}
}
public class ClassC
{
private ClassA _classA;
private ClassB _classB;
public string field1
{
get
{
return _classA.field1;
}
set
{
_classA.field1 = value;
}
}
public string field2
{
get
{
return _classA.field2;
}
set
{
_classA.field2 = value;
}
}
public string fieldA
{
get
{
return _classB.fieldA;
}
set
{
_classB.fieldA = value;
}
}
public string fieldB
{
get
{
return _classB.fieldB;
}
set
{
_classB.fieldB = value;
}
}
public ClassC(ClassA a, ClassB b)
{
_classA = a;
-classB = b;
}
}
Note that this is a temporary refactor and should be followed by properly
architecting the solution so the class better fits the idea of ClassC
containing all of the properties in question.
Peace and Grace,
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
Twitter: @gbworld
Blog:
http://gregorybeamer.spaces.live.com
My vacation and childhood cancer awareness site:
http://www.crazycancertour.com
*******************************************
| Think outside the box! |
*******************************************