VB 2005 - Genrics And Base Classes

U

Urs Eichmann

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
 
M

Mattias Sjögren

The only solution I can think of is separating the BaseEntity into
something like


Public MustInherit Class BaseBaseEntity

You can also use an interface instead of an abstract base class.

... but this can quickly become very complicated

What's complicated about it?



Mattias
 
J

Jay B. Harlow [MVP - Outlook]

Urs,
I agree with Mattias.

| Public MustInherit Class BaseBaseEntity
Can be either an abstract base class or an Interface. I suspect an abstract
base class would be "better" as you show Name can be in BaseBaseEntity.
Using an Interface all your code would be in BaseEntity.

| ... but this can quickly become very complicated and seems to be like a
| bit of an overkill.
I don't see how its complicated or overkill... Each layer: (BaseBaseEntity ,
BaseEntity, CustomerEntity) each have their specific responsibilities & are
more specialized then the layer below them...

CustomerEntity is more specialized/specific then BaseEntity, while
BaseEntity is more specialized/specific then BaseBaseEntity, while
BaseBaseEntity is more specialized/specific then Object...

Just look at the layers that make up a form in your project:

MyProject.MainForm
is a System.Windows.Forms.Form
is a System.Windows.Forms.ContainerControl
is a System.Windows.Forms.ScrollableControl
is a System.Windows.Forms.Control
is a System.ComponentModel.Component
is a System.MarshalByRefObject
is a System.Object

http://msdn2.microsoft.com/library/w4bcxb43(en-us,vs.80).aspx

Each layer of MainForm, adds a little more specific functionality to Object
before you finally achieve the MainForm itself. This excludes if your
solution uses form inheritance itself...

Hope this helps
Jay




| 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
 

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