views:

110

answers:

2

Hello...

I just go the answer on how to pass a generic delegate as a parameter. Thanks for the help. Now I need to know how to ASSIGN the delegate to another delegate declarartion. Can this be done?

Public Class MyClass  
    Public Delegate Function Getter(Of TResult)() As TResult    

    ''#the following code works.
    Public Shared Sub MyMethod(Of TResult)(ByVal g As Getter(Of TResult))
        ''# I want to assign Getter = g  ''#how do you do it.
    End Sub
End Class

Notice that Getter is now private. How can I ASSIGN Getter = G

When I try Getter = g 'I get too few type arguments compile error. When I try Getter(Of TResult) = g 'I get Getter is a type and cannot be used as an expression.

How do you do it?

Seth

+2  A: 

I'm not quite sure what you're trying to do - in your code, Getter is a name of a type (a delegate type), not an instance field of the class that you could assign something to.

Did you intend to declare an event (that other users can registre handlers to)? If that's the case, see for example this article at MSDN.

Tomas Petricek
I have simplified a little bit. The call to MyClass.MyMethod will look like this. MyClass.MyMethod(AddressOF SomeOtherProc) I have a class level delegate (Getter) with the exact same sig as the method parameter delegate (g). The END RESULT of what I want is for Getter to be set to the AddressOf g which is set in the call to MyMethod. I am not dealing with events here.Seth
Seth Spearman
@Seth: If you don't need a full event, then you need a field of a delegate type (as Joel shows).
Tomas Petricek
+1  A: 

You can't yet. Your Getter(Of TResult)() delegate is not a variable that you can assign things to. It's more like a class definition. So first you need to also declare a delegate variable you can use:

Public Class MyClass  
    Public Delegate Function Getter(Of TResult)() As TResult    

    Private MyGetter As Getter(Of TResult)

    Public Shared Sub MyMethod(Of TResult)(ByVal g As Getter(Of TResult))
        MyGetter = g  
    End Sub
End Class

Note that you still have an issue with the generic type, because the generic type you declared for MyMethod(Of TResult) is not connected to the type of the delegate variable I just added here. You have to make the entire class generic instead of the just the method for that to work.

I'm also wondering why you don't just use the existing Func(Of T) delegate?

Joel Coehoorn
Func (Of T) is not available in NET 2.
Seth Spearman
@Seth Okay. Updated your question tags so we can know that part.
Joel Coehoorn
Thanks...I also updated the title to reflect that.
Seth Spearman