Delegate casting problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • PJ6

    #1

    Delegate casting problem

    I have a generalized delegate,

    Public Delegate Function DataRetreivingD elegate() as Object

    with an associated property in a certain class. Almost never will I actually
    be wanting to set the property of this delegate type to a method that
    returns an Object - rather I want to point it to methods that will be
    returning strings, integers, decimals, whatever.

    So I tried this -

    Public Shared Function GetSomething() as String

    End Function

    DirectCast(Addr essOf GetSomething, DataRetreivingD elegate)

    Only this doesn't work. Why doesn't the Framework allow method signature
    return types to be up-casted?

    Anyway, what's the best way around this without having to physically code
    additional methods that return Objects? I know I can just use a raw
    MethodInfo, but I don't want to complicate this any more than I have to
    since performance will be a consideration.

    TIA,
    Paul


  • Ken Tucker [MVP]

    #2
    Re: Delegate casting problem

    Hi,

    If you are using vb 2005 try this.

    Public Delegate Function DataRetreivingD elegate(Of T)() As T

    Public Function GetSomething() As String
    Return "test"
    End Function


    Dim d As New DataRetreivingD elegate(Of String)(Address Of
    GetSomething)
    Label1.Text = d.Invoke()


    Ken
    ---------------------
    "PJ6" <noone@nowhere. net> wrote in message
    news:%23ysekKXj GHA.3780@TK2MSF TNGP03.phx.gbl. ..[color=blue]
    >I have a generalized delegate,
    >
    > Public Delegate Function DataRetreivingD elegate() as Object
    >
    > with an associated property in a certain class. Almost never will I
    > actually be wanting to set the property of this delegate type to a method
    > that returns an Object - rather I want to point it to methods that will be
    > returning strings, integers, decimals, whatever.
    >
    > So I tried this -
    >
    > Public Shared Function GetSomething() as String
    >
    > End Function
    >
    > DirectCast(Addr essOf GetSomething, DataRetreivingD elegate)
    >
    > Only this doesn't work. Why doesn't the Framework allow method signature
    > return types to be up-casted?
    >
    > Anyway, what's the best way around this without having to physically code
    > additional methods that return Objects? I know I can just use a raw
    > MethodInfo, but I don't want to complicate this any more than I have to
    > since performance will be a consideration.
    >
    > TIA,
    > Paul
    >[/color]


    Comment

    Working...