PC Review


Reply
Thread Tools Rate Thread

AddHandler RemoveHandler Question

 
 
hartley_aaron@hotmail.com
Guest
Posts: n/a
 
      25th Mar 2005
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

 
Reply With Quote
 
 
 
 
Marina
Guest
Posts: n/a
 
      25th Mar 2005
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

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Patrick Philippot
Guest
Posts: n/a
 
      25th Mar 2005
(E-Mail Removed) 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


 
Reply With Quote
 
hartley_aaron@hotmail.com
Guest
Posts: n/a
 
      26th Mar 2005
Thank you both for quick replies! I will try out your solutions.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Must call RemoveHandler after AddHandler? Tom Microsoft VB .NET 12 11th Aug 2006 06:54 PM
AddHandler and RemoveHandler Elisa Microsoft Dot NET Compact Framework 1 25th Mar 2004 07:44 PM
AddHandler / RemoveHandler Timo Microsoft Dot NET Framework 3 15th Mar 2004 06:45 PM
AddHandler, RemoveHandler Urs Vogel Microsoft VB .NET 1 20th Oct 2003 02:36 PM
Addhandler/Removehandler Jim Microsoft Dot NET Framework Forms 5 10th Sep 2003 09:00 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:07 AM.