By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,218 Members | 1,194 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,218 IT Pros & Developers. It's quick & easy.

AddHandler RemoveHandler Question

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

<ha***********@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.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

Nov 21 '05 #2

P: n/a
ha***********@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
Nov 21 '05 #3

P: n/a
Thank you both for quick replies! I will try out your solutions.

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.