Sanity Check - General Programming

J

Jon Wallace

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,

Regards,
Jon
 
S

Scott M.

What you are asking about is called "inheritance" and what you've
specifically asked (one class that can inherit members from more than one
other class) is called "multiple" inheritance, which C# (and VB .NET) don't
do. You can do "multi-level" inheritance, where one class inherits from
another and that class inherits from yet another.

But, inheritance has its perils, if class "B", which has a public property
called "sData1" were to inherit from class "A", which also had the exact
same property defined, the compiler would not be able to figure out which
one a class user of "B" wants to call when they ask for it. This is where
the concept of overriding comes in.

So, take your modified example:

public class Customer
{
public string Name { get; set; }
public string Address; { get; set; }

public ClassA()
{
}
}

public class NetflixCustomer : Customer
{
public string EmailAddress{ get; set; }
public string UserID{ get; set; }

public ClassB()
{
}
}

The NetflixCustomer class inherits all the members of the Customer class and
so it would actually have 4 properties, even though only two of them were
defined at the NetflixCustomer level.

There's an awful lot to learn about OOP, but keep at it...it'll come.

-Scott
 
R

Registered User

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; }
obvious typo
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();
Yes or you could have
public ClassA cA {get; set;}
public ClassB cB {get; set;}
and make the assignments in the ClassC c'tor
public ClassC()
{
cA = new ClassA();
cB = new ClassB();
}
public string sData1 = { get; set; }
public string sData2 = { get; set; }
This is I don't understand. If the two strings are members of ClassC
then they should be declared in the same manner they're declared in
the other classes.
If the intent is to expose the members of cA and cB you might do this.
public string ClassAData1
{
get { return cA.sData1; }
set { cA.sData1 = value;
}
and so forth. In the example the members cA and cB might not have to
be scoped publicly if the members are exposed as above.

regards
A.G.
 
J

Jon Wallace

Thanks for the reply - that helps...

--

Registered User said:
Yes or you could have
public ClassA cA {get; set;}
public ClassB cB {get; set;}
and make the assignments in the ClassC c'tor
public ClassC()
{
cA = new ClassA();
cB = new ClassB();
}

This is I don't understand. If the two strings are members of ClassC
then they should be declared in the same manner they're declared in
the other classes.
If the intent is to expose the members of cA and cB you might do this.
public string ClassAData1
{
get { return cA.sData1; }
set { cA.sData1 = value;
}
and so forth. In the example the members cA and cB might not have to
be scoped publicly if the members are exposed as above.

regards
A.G.
 
G

Gregory A. Beamer

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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top