By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,903 Members | 2,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,903 IT Pros & Developers. It's quick & easy.

VB 2005 - Genrics And Base Classes

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #2

P: n/a
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/w...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


"Urs Eichmann" <ur***@online.nospam> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
| 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
Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.