Home All Groups Group Topic Archive Search About

AddHandler RemoveHandler Question

Author
25 Mar 2005 5:31 PM
hartley_aaron
Hi,

I was trying to store the address of the my current handler for a
particular event so as to simplify using AddHandler and RemoveHandler
throughout my code.  However, I cannot seem to get any kind of variable
to except the data.  When I tried a Long I got the message "'AddressOf'
expression cannot be converted to 'Long' because 'Long' is not a
delegate type."  I tried other datatypes as well but was not able to
find anything that worked.  Here is a very simply example of what I am
trying to do:


Dim CurrentHandler as Long

....

RemoveHandler MyObject.SomethingHappens, CurrentHandler
CurrentHandler = AddressOf DoThis
AddHandler MyObject.SomethingHappens, CurrentHandler

....

RemoveHandler MyObject.SomethingHappens, CurrentHandler
CurrentHandler = AddressOf DoThat
AddHandler MyObject.SomethingHappens, CurrentHandler

....

RemoveHandler MyObject.SomethingHappens, CurrentHandler
CurrentHandler = AddressOf DoSomethingElse
AddHandler MyObject.SomethingHappens, CurrentHandler

Author
25 Mar 2005 5:46 PM
Marina
Right. You are trying to convert what is essentially a function pointer, to
other types. That can't work.

You need to declare your own delegate type, which is to say a delegate that
can point to a method with a specified signature. Then, you can use it.

Something like:

Public Delegate Sub MyDelegateType(ByVal int1 As Integer)
Public Sub MySub(ByVal myIntParam As Integer)
    ' Do Stuff Here
End Sub
Public Sub MainCodeSub()
    Dim savedPointer As MyDelegateType = AddressOf MySub
End Sub

<hartley_aa***@hotmail.com> wrote in message
Show quoteHide quote
news:1111771903.033251.119270@f14g2000cwb.googlegroups.com...
> Hi,
>
> I was trying to store the address of the my current handler for a
> particular event so as to simplify using AddHandler and RemoveHandler
> throughout my code.  However, I cannot seem to get any kind of variable
> to except the data.  When I tried a Long I got the message "'AddressOf'
> expression cannot be converted to 'Long' because 'Long' is not a
> delegate type."  I tried other datatypes as well but was not able to
> find anything that worked.  Here is a very simply example of what I am
> trying to do:
>
>
> Dim CurrentHandler as Long
>
> ...
>
> RemoveHandler MyObject.SomethingHappens, CurrentHandler
> CurrentHandler = AddressOf DoThis
> AddHandler MyObject.SomethingHappens, CurrentHandler
>
> ...
>
> RemoveHandler MyObject.SomethingHappens, CurrentHandler
> CurrentHandler = AddressOf DoThat
> AddHandler MyObject.SomethingHappens, CurrentHandler
>
> ...
>
> RemoveHandler MyObject.SomethingHappens, CurrentHandler
> CurrentHandler = AddressOf DoSomethingElse
> AddHandler MyObject.SomethingHappens, CurrentHandler
>
Author
25 Mar 2005 5:55 PM
Patrick Philippot
hartley_aa***@hotmail.com wrote:
>  When I tried a Long I got the message
> "'AddressOf' expression cannot be converted to 'Long' because 'Long'
> is not a delegate type."

Hi,

If you look at the documentation relative to AddressOf, you'll see that
AddressOf returns a Delegate object. So your variable should be of type
Delegate.

Dim CurrentHandler as [Delegate]

(the brackets are here because Delegate is also a reserved keyword).

--
Patrick Philippot - Microsoft MVP
MainSoft Consulting Services
www.mainsoft.fr
Author
26 Mar 2005 3:38 AM
hartley_aaron
Thank you both for quick replies! I will try out your solutions.