473,320 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

VB 2005 - Genrics And Base Classes

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
2 1486
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Chun Wang | last post by:
1. When writing genric classes or methods, can I use specifc type (instead of generic type) as type parameter? For example: pulic class MyStack<T, int> { ..... }
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.