Help with inheritance problem

M

Mike

I have several classes that will have a property of type List (of
whatever).



For example, I will have collections named:



Protocols that will have a property named Items as List (of Protocol).

Orders that will have a property named Items as List (of Order).



Etc.



All this classes will have their own Add, Remove, Count, etc, method.



So for example, the Add method of the orders class will be as follows:



Public sub Add(NewOrder as Order)

Me.m_Items.Add(NewOrder)

End Sub



It is very repetitive to create and Add, Remove, etc, method for every
class that will implement this functionality, so I thought about
creating a base class named SpecializedCollectionBase that will contain
an Add, Remove, etc method, and the rest of the classes can inherit from
it.



The only problem that I see with this is that SpecializedCollectionBase
won’t know of what type is the Items property of the class that is
inheriting.



It can be of Order, of Protocol, etc.



I hope you understand my point and can help me to make this more
efficient.



Thanks
 
G

Guest

Mike,

Why not have each collection inherit from List:

Public Class Protocols
Inherits List (Of Protocol)

Then add any specific collection methods to the Protocols class, etc.

Kerry Moorman
 
A

Armin Zingler

Mike said:
I have several classes that will have a property of type List (of
whatever).



For example, I will have collections named:

Protocols that will have a property named Items as List (of
Protocol).
Orders that will have a property named Items as List (of Order).

Etc.

All this classes will have their own Add, Remove, Count, etc,
method.

So for example, the Add method of the orders class will be as
follows:

Public sub Add(NewOrder as Order)

Me.m_Items.Add(NewOrder)

End Sub

It is very repetitive to create and Add, Remove, etc, method for
every class that will implement this functionality, so I thought
about creating a base class named SpecializedCollectionBase that
will contain an Add, Remove, etc method, and the rest of the classes
can inherit from it.

The only problem that I see with this is that
SpecializedCollectionBase won't know of what type is the Items
property of the class that is inheriting.

It can be of Order, of Protocol, etc.

I hope you understand my point and can help me to make this more
efficient.

Thanks


I don't see why you need such a class at all. The List(Of) class already has
Add/Remove/Count members.

You could directly inherit from List(Of)

class Orders
inherits list(Of Order)
end class

class Protocols
inherits list(Of Protocol)
end class

...

dim o as new orders
dim p as new protocols



Armin
 
H

Herfried K. Wagner [MVP]

Kerry Moorman said:
Why not have each collection inherit from List:

Public Class Protocols
Inherits List (Of Protocol)

Then add any specific collection methods to the Protocols class, etc.

Maybe 'System.Collections.ObjectModel.Collection(Of T)' is more suitable
than 'List(Of T)'.
 

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