Hi
using VS 2005 Beta 2, I want to define a base class for any type of
entity, e.g. customer, item, order etc. The entity could have a key of
type String or Integer, so I define the base class as follows:
Public MustInherit Class BaseEntity(Of tKey)
public Key As tKey ' not using properties for simplicity here
Public Name as String
... other fields and methods omitted
End Class
then I define some actual Entities:
Public Class CustomerEntity
Inherits BaseEntity(Of String) ' customer has a String key
End Class
Public Class ItemEntity
Inherits BaseEntity(Of Integer)' item has a numeric key
End Class
this way, if I type
Dim c as New CustomerEntity
c.Key will have the right type (String in this case).
Now, I want to have an array which can hold *all* classes of the
BaseEntity type. I want to iterate through the Name field. How do I do that?
I want to write
Dim arr() As BaseEntity
For each b as BaseEntity in arr
console.write(b.Name)
next
but I cannot... because the compiler insists of having
BaseEntity(Of...), but at this time I don't really want to specify the
Of parameter, because it should be *any* base entity
The only solution I can think of is separating the BaseEntity into
something like
Public MustInherit Class BaseBaseEntity
Public Name as String
... other fields and methods omitted
End Class
Public MustInherit Class BaseEntity(Of tKey)
Inherits BaseBaseEntity
public Key As tKey
End Class
.... but this can quickly become very complicated and seems to be like a
bit of an overkill.
What do you think about this? Is there a better solution?
Urs