Re: Inheritance question
- From: "RSH" <way_beyond_oops@xxxxxxxxx>
- Date: Thu, 26 Apr 2007 15:19:21 -0400
Thanks guys!
I was simply trying to get a better understanding of working with
inheritance.
I guess I did need to be a lot clearer on intent. Lets assume that my base
class is a Person Class. The derived classes are Employee,Customer,Vendor.
I was attempting to create a list of Persons which I figured would probably
be a static collection in the base class. But after thinking about it a bit
more would it make more sense to have a static collection in each derived
class that holds only objects of the appropriate type?
I am trying to learn inheritance so i dont really know the questions to ask.
I am just trying to sort through handling a one to many relationship when
inheritance is involved.
Ron
"Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx> wrote in message
news:1331tu911bi5c09@xxxxxxxxxxxxxxxxxxxxx
"RSH" <way_beyond_oops@xxxxxxxxx> wrote in message
news:eCNsYLDiHHA.1216@xxxxxxxxxxxxxxxxxxxxxxx
Peter,
Thanks for the reply. Sorry about that.
I do want to have the objects to be added to the list on
instantiation...I just didn't know how to represent that.
There are a number of ways to implement something like that. Without
knowing more about your specific intent, preferences, etc. I can't offer
advice on a particular implementation to choose. However, here's one
example of doing what you want:
class BaseClass
{
static private List<BaseClass> _grbcInstances;
public BaseClass()
{
_grbcInstances.Add(this);
}
public void Remove()
{
_grbcInstances.Remove(this);
}
}
The BaseClass can then provide methods for manipulating, searching, etc.
the list of objects. Note that you need to provide an explicit "Remove"
method to ensure that objects are actually released when you want them to
be. This puts the onus on you to figure out when you're actually done
with an object, at least as it relates to the master list of objects. I
have seen mentions of something called a "weak reference" that may or may
not help you around this issue; I don't know enough about weak references
to offer you advice on that.
Finally, I agree with Jon's statement about the possibility of using
virtual methods to deal with per-type behaviors. It's unusual to need to
know the actual type of an object and while I took the shortcut of simply
answering the direct question you asked, I agree that it's more likely
that whatever reason you think you need the type for, that can be more
properly addressed using virtual methods.
Pete
.
- Follow-Ups:
- Re: Inheritance question
- From: Peter Duniho
- Re: Inheritance question
- References:
- Inheritance question
- From: RSH
- Re: Inheritance question
- From: Peter Duniho
- Re: Inheritance question
- From: RSH
- Re: Inheritance question
- From: Peter Duniho
- Inheritance question
- Prev by Date: Re: Simple question on constructor
- Next by Date: Re: Executing string from C#
- Previous by thread: Re: Inheritance question
- Next by thread: Re: Inheritance question
- Index(es):
Relevant Pages
|