Here is what I'm trying to do.
Public Class Part
End Class
Public Class SpecialPart
Inherits Part
End Class
Public Class FormManagerTest
Public Delegate Function PartGetter(ByVal id As Integer) As Part
Public Function GetPart(ByVal id As Integer) As Part
Return New Part
End Function
Public Function GetSpecialPart(ByVal id As Integer) As SpecialPart
Return New SpecialPart
End Function
End Class
Public Class Test
Sub Main()
Dim fm As New FormManagerTest
Dim getter1 As FormManagerTest.PartGetter
Dim getter2 As FormManagerTest.PartGetter
getter1 = AddressOf fm.GetPart
getter2 = AddressOf fm.GetSpecialPart
getter1.Invoke(1)
getter2.Invoke(1)
End Sub
End Class
Essentially I want to use a Delegate to refer to a function where the
return type of the function is a type derived from the return type of
the delegate. in this example getter2 = AddressOf fm.GetSpecialPart
compains "Error 104 Method 'Public Function GetSpecialPart(id As
Integer) As UnitTests.SpecialPart' does not have the same signature as
delegate 'Delegate Function PartGetter(id As Integer) As Part'."
I know this is probably the way it's supposed to be, but I'm wondering
what I should do in this scenario. Any advice or best practices or
anything? Am I don't something completely braindead, because I am
fairly new to delegates and that could very well be the case
Thanks,
James Thigpen
http://www.jamesthigpen.com
Here too in VB 2005, but works well in VB 2008.
Armin
....Public Delegate Function PartGetter(Of t)(ByVal id As Integer) As t
....Dim getter1 As FormManagerTest.PartGetter(Of Part)
....Dim getter2 As FormManagerTest.PartGetter(Of SpecialPart)
VB 9 may just be silently casting SpecialPart to Part, but If you wanted
to use it as a SpecialPart again you would need to cast back. You could
also solve it by having GetSpecialPart return a Part, not a SpecialPart,
and then convert back when you needed like thus (without generics):
....Public Function GetSpecialPart(ByVal id As Integer) As Part
........' method body
....End Function
....Dim p As Part = getter2.Invoke(1)
....If TypeOf p Is SpecialPart Then
........Dim sp As SpecialPart = DirectCast(p, SpecialPart)
....End If
//Andrew
Hrmm, that's pretty hot. I hadn't considered using Generics. I think
the generics version will work better in this instance. Thanks.
-jt
The feature you are looking for is called delegate covariance. It is not
present in VB 2005 and has been added in VB 2008.
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Echo's again in Austria, Armin wrote that already.
Or are those Echo's 2 hours long.
Cor
:-)
Cor
Why?
Armin
Sry Cor, but I've never heard of "delegate covariance" so far. I thought
it's a compiler bug in VB 2005.
Armin
"Delegate Covariance". Awesome. I love learning the names for new
(to me) things.
-jt
Well, then you may be interested in delegate contravariance too, which means
that a method with signature 'Sub Foo(ByVal x As Animal)' conforms to the
delegate type 'Sub Foo(ByVal x As Dog)', assuming 'Dog' is a subclass of
'Animal'.
:-)
Cor
"Armin Zingler" <az.n...@freenet.de> schreef in bericht
news:es$1VbreI...@TK2MSFTNGP03.phx.gbl...