473,378 Members | 1,360 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,378 software developers and data experts.

Using Cases to Instantiate Generic Classes

I have a generic class defined. My "server" code wants to call methods on
the generic class after I've assigned the type to it. I was trying to do
this via a SELECT CASE statement but I can't seem to determine how to declare
the object at the module level (so I can use it in several methods) but
assign the type when I create the instance. Probably this isn't the
appropriate way to use Generics so if there are suggestions, I'm open to
them. Here is a contrived example of what I'm hoping to accomplish:

Public Interface IAnimal
Sub SitDown()
End Interface

Public Class AnimalServer(Of T As iAnimal)
Dim oAnimal As T
Public Sub GiveCommandToSit(ByVal oAnimal As T)
oAnimal.SitDown()
End Sub
End Class

Public Class Dog
Implements IAnimal
Public Sub SitDown() Implements IAnimal.SitDown
MsgBox("Dog sits")
End Sub
End Class

Public Class Cat
Implements IAnimal
Public Sub SitDown() Implements IAnimal.SitDown
MsgBox("Cat sits")
End Sub
End Class

Public Module Program
Dim oAnimalServer As AnimalServer(Of IAnimal) '?????
'commented out TryGeneric because project won't build
Private Sub TryGeneric()
'If 1 = 1 Then
' oAnimalServer = New AnimalServer(Of Dog) 'design-time error
'Else
' oAnimalServer = New AnimalServer(Of Cat) 'design-time error
'End If
'oAnimalServer.GiveCommandToSit()
End Sub

Sub Main()
'works fine
Dim oDogServer As New AnimalServer(Of Dog)
Dim oCatServer As New AnimalServer(Of Cat)
oDogServer.GiveCommandToSit(New Dog)
oCatServer.GiveCommandToSit(New Cat)
TryGeneric()
End Sub
End Module
Nov 8 '07 #1
2 1870
It is not clear to me from your example, why you need to have different
instances of the animalserver for this example. Seems to me that you need
just one instance for all those things that implement Ianimal. Here is your
TryGeneric done a little different...

Dim oAnimalServer As AnimalServer(Of IAnimal) '?????
Private Sub TryGeneric()
Dim oAnimal As IAnimal
If 1 = 1 Then
oAnimal = New Dog
Else
oAnimal = New Cat
End If
oAnimalServer.GiveCommandToSit(oAnimal)
End Sub
--
Terry
"JohnMSyrasoft" wrote:
I have a generic class defined. My "server" code wants to call methods on
the generic class after I've assigned the type to it. I was trying to do
this via a SELECT CASE statement but I can't seem to determine how to declare
the object at the module level (so I can use it in several methods) but
assign the type when I create the instance. Probably this isn't the
appropriate way to use Generics so if there are suggestions, I'm open to
them. Here is a contrived example of what I'm hoping to accomplish:

Public Interface IAnimal
Sub SitDown()
End Interface

Public Class AnimalServer(Of T As iAnimal)
Dim oAnimal As T
Public Sub GiveCommandToSit(ByVal oAnimal As T)
oAnimal.SitDown()
End Sub
End Class

Public Class Dog
Implements IAnimal
Public Sub SitDown() Implements IAnimal.SitDown
MsgBox("Dog sits")
End Sub
End Class

Public Class Cat
Implements IAnimal
Public Sub SitDown() Implements IAnimal.SitDown
MsgBox("Cat sits")
End Sub
End Class

Public Module Program
Dim oAnimalServer As AnimalServer(Of IAnimal) '?????
'commented out TryGeneric because project won't build
Private Sub TryGeneric()
'If 1 = 1 Then
' oAnimalServer = New AnimalServer(Of Dog) 'design-time error
'Else
' oAnimalServer = New AnimalServer(Of Cat) 'design-time error
'End If
'oAnimalServer.GiveCommandToSit()
End Sub

Sub Main()
'works fine
Dim oDogServer As New AnimalServer(Of Dog)
Dim oCatServer As New AnimalServer(Of Cat)
oDogServer.GiveCommandToSit(New Dog)
oCatServer.GiveCommandToSit(New Cat)
TryGeneric()
End Sub
End Module
Nov 8 '07 #2
On Nov 8, 2:51 pm, JohnMSyrasoft
<JohnMSyras...@discussions.microsoft.comwrote:
I have a generic class defined. My "server" code wants to call methods on
the generic class after I've assigned the type to it. I was trying to do
this via a SELECT CASE statement but I can't seem to determine how to declare
the object at the module level (so I can use it in several methods) but
assign the type when I create the instance. Probably this isn't the
appropriate way to use Generics so if there are suggestions, I'm open to
them. Here is a contrived example of what I'm hoping to accomplish:

Public Interface IAnimal
Sub SitDown()
End Interface

Public Class AnimalServer(Of T As iAnimal)
Dim oAnimal As T
Public Sub GiveCommandToSit(ByVal oAnimal As T)
oAnimal.SitDown()
End Sub
End Class

Public Class Dog
Implements IAnimal
Public Sub SitDown() Implements IAnimal.SitDown
MsgBox("Dog sits")
End Sub
End Class

Public Class Cat
Implements IAnimal
Public Sub SitDown() Implements IAnimal.SitDown
MsgBox("Cat sits")
End Sub
End Class

Public Module Program
Dim oAnimalServer As AnimalServer(Of IAnimal) '?????
'commented out TryGeneric because project won't build
Private Sub TryGeneric()
'If 1 = 1 Then
' oAnimalServer = New AnimalServer(Of Dog) 'design-time error
'Else
' oAnimalServer = New AnimalServer(Of Cat) 'design-time error
'End If
'oAnimalServer.GiveCommandToSit()
End Sub

Sub Main()
'works fine
Dim oDogServer As New AnimalServer(Of Dog)
Dim oCatServer As New AnimalServer(Of Cat)
oDogServer.GiveCommandToSit(New Dog)
oCatServer.GiveCommandToSit(New Cat)
TryGeneric()
End Sub
End Module
John, I don't think your use of generic's here is really
appropriate. It sort of appears to me, that your intention is to
create a set of related classes in a generic way. Basically, it looks
to me that you are wanting something like the abstract factory
pattern:

http://www.devcity.net/Articles/15/1/20020211.aspx

The article seems to be ok, but if you need additional information,
there are lots of resources on google.

--
Tom Shelton

Nov 8 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Carlos Ribeiro | last post by:
Hello all, I'm posting this to the list with the intention to form a group of people interested in this type of solution. I'm not going to spam the list with it, unless for occasional and...
7
by: Allan Bruce | last post by:
I have had a look through the FAQ and found that if I am using a class template then I need an argument list. I have tried to add this but it is not quite working - i.e. it doesnt compile. My...
8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
5
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public...
1
by: Anonieko | last post by:
Understanding and Using Exceptions (this is a really long post...only read it if you (a) don't know what try/catch is OR (b) actually write catch(Exception ex) or catch{ }) The first thing I...
14
by: dave.dolan | last post by:
Basically I'd like to implement the composite design pattern with leaves that are either of reference or value types, but even using generics I can't seem to avoid boxing (using ArrayList or...
25
by: David Sanders | last post by:
Hi, As part of a simulation program, I have several different model classes, ModelAA, ModelBB, etc., which are all derived from the class BasicModel by inheritance. model to use, for example...
4
by: Tomas | last post by:
A newbie question: How can I instantiate objects dynamically in VB.NET. E.g. I have the object 'Player' and I would like to instantiate it with the several instances (James, Gunner, etc.), without...
61
by: Sanders Kaufman | last post by:
I'm wondering if I'm doing this right, as far as using another class object as a PHP class property. class my_baseclass { var $Database; var $ErrorMessage; var $TableName; var $RecordSet;...
1
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: 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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.