Delegate Return Type is Derived Type

J

James.R.Thigpen

Hello,

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
 
A

Armin Zingler

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'."

Here too in VB 2005, but works well in VB 2008.


Armin
 
A

Andrew Backer

Not sure if this is what you are looking for, but I was able to do it pretty
simply with generics.

.....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
 
J

James.R.Thigpen

Not sure if this is what you are looking for, but I was able to do it pretty
simply with generics.

....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
 
H

Herfried K. Wagner [MVP]

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

The feature you are looking for is called delegate covariance. It is not
present in VB 2005 and has been added in VB 2008.
 
C

Cor Ligthert[MVP]

Herfried,

Echo's again in Austria, Armin wrote that already.

Or are those Echo's 2 hours long.

Cor
 
A

Armin Zingler

Cor Ligthert said:
Herfried,

Echo's again in Austria, Armin wrote that already.

Or are those Echo's 2 hours long.


Sry Cor, but I've never heard of "delegate covariance" so far. I thought
it's a compiler bug in VB 2005.


Armin
 
J

James.R.Thigpen

The feature you are looking for is called delegate covariance. It is not
present in VB 2005 and has been added in VB 2008.

"Delegate Covariance". Awesome. I love learning the names for new
(to me) things.

-jt
 
H

Herfried K. Wagner [MVP]

"Delegate Covariance". Awesome. I love learning the names for new
(to me) things.

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'.
 
C

Cor Ligthert[MVP]

cryptic and as you know, I like that, you saw even Herfried did not
understand it.

:)

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top