Inheritance/BaseClass

D

DKode

Ok,

here is another question I have that is sort of unrelated to my last
posting about composition.

I have three Collection classes:
EmployeeCollection
HourCollection
InfractionCollection

all three of these classes inherit from ArrayList and will have
Add/Item/Remove methods.

I noticed in the documentation that in the arraylist class, the Item
method returns an object. Would I override the Item method to return
the correct type of class. Then i run into the problem that I notice
that the Add method of the arraylist class takes an input parameter of
type object, so would i also need to override this method to pass the
specific class type that i want to add to the arraylist or would I
just add an item to the arraylist as type object and then cast it to
the particular type when I use the Item method?


I have another example of the following classes:
Hour
Infraction

these two classes will share quite a few identical methods such as:
AddHour, AddInfraction, EditHour, EditInfraction etc..

I have created a base class for these two classes called HIDetailBase
which will contain the Add, Edit methods, now I am in the same boat as
my first question, what is the proper way to override/overload the
methods to get the functionality i require.

Any help would be greatly appreciated. thank you
 
M

Michael Bird

As I see it, you have 3 choices:

1. Just use an ArrayList and be done with it. May not be possible if your
class has a lot of extra functionality.

2. Derive from ArrayList, but don't ceate your own functions for Add and
Remove, or any other methods provided by ArrayList. If you add new methods
for Add and Remove, the original ArrayList methods will still be visible to
the users of your class, and this could cause confusion, and bugs.

3. Don't derive from ArrayList, simple have your class contain an ArrayList.
Then write your Add, Remove, ... with correctly typed arguments instead of
Object.

Personally, I wou do option 1 or 3, depending on whether your class is
simply a collection, or has other enhanced functionality.
 
G

Guest

ms-help://MS.MSDNQTR.2003APR.1033/cpref/html/frlrfsystemcollectionscollectio
nbaseclasstopic.htm


public abstract class CollectionBase : IList, ICollection,
IEnumerable


Cant you just use CollectionBase and roll yer own using ArrayList?
 
D

DKode

I think what I will end up doing is create collection base classes
that derive from system.collections.collectionbase as I used this for
another app i worked on. This will allow my collections to be scalable
in the future as I think they will be. I setup composition
associations in my Static structure diagram between the HourCollection
and InfractionCollection classes and my Employee class. I then have
methods in my Employee class that will get the proper information from
the database and populate the collection classes that are associated
with the employee class.

does this sound correct?
 
M

Michael Lang

(e-mail address removed) (DKode) wrote in
I think what I will end up doing is create collection base classes
that derive from system.collections.collectionbase as I used this for
another app i worked on. This will allow my collections to be scalable
in the future as I think they will be. I setup composition
associations in my Static structure diagram between the HourCollection
and InfractionCollection classes and my Employee class. I then have
methods in my Employee class that will get the proper information from
the database and populate the collection classes that are associated
with the employee class.

does this sound correct?

YES. Use CollectionBase. I have some examples and a generator to make
your own custom collections based on my examples (templates). Feel free to
use one of them.

This is open source, and free of charge...
http://sourceforge.net/projects/colcodegen

Most of my templates allow for indexing by more than just index, but by
some kind of key (string, int, or guid). If you don't want the extra
indexing, just delete the unneeded sections.

Michael Lang, MCSD
 
F

Fergus Cooney

Hi DKode, Michael,

There is another choice to go with the first 3:

4. Derive from ArrayList. Implement your own Add, AddRange,
etc - anything that you want strongly typed. Leave everything else
for ArrayList to handle. Override the ArrayList Add, AddRange,
etc, which use Object - but leave them empty and make them private.

Regards,
Fergus
 
D

DKode

Hello,

Excuse me for sounding like a newb, but what do you mean by strongly typed?

thanks
 
M

Michael Lang

(e-mail address removed) (DKode) wrote in
Excuse me for sounding like a newb, but what do you mean by strongly
typed?

It means the most relevant derived type instead of a base class type (such
as "Object" - everything ultimately derives from object).

Michael Lang
 
F

Fergus Cooney

Hi Michael, DKode

Thanks, Michael.

|| anything that you want strongly typed

Yes, I should have written

'anything that you want strongly typed (ie. in your own type)'.

Regards,
Fergus
 

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

Top