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

Using Cases to Instantiate Generic Classes

P: n/a
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
Share this Question
Share on Google+
2 Replies


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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.