none
RemoveHandler Error RRS feed

  • Question

  • When I've used the remove handler method in my code, I get this warning:


    The 'AddressOf' expression has no effect in this context because the method argument to 'AddressOf' requires a relaxed conversion to the delegate type of the event. Assign the 'AddressOf' expression to a variable, and use the variable to add or remove the method as the handler.   


    What is it? Why do I have to assign the "AddressOf" expression to a variable? And how?
    Wednesday, January 14, 2009 7:43 PM

Answers

  • Dim myEventHandler As New EventHandler(AddressOf mysub)  'declare a variable with addressof your sub
            RemoveHandler mycontrol.myevent, myEventHandler  'remove it


    Just a guess from the error message

    Arjun Paudel
    • Marked as answer by Justin.NET Wednesday, January 14, 2009 9:19 PM
    Wednesday, January 14, 2009 8:35 PM

All replies

  • The second argument in RemoveHandler refers to the event handler sub routine not to a data type or an object

    Public Class Form1  
        Private Sub Button_Click(ByVal sender As ObjectByVal e As EventArgs)  
     
        End Sub 
          
        Private Sub Form1_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load  
            Dim NewButton As New Button  
     
            'add event handler  
            AddHandler NewButton.Click, AddressOf Button_Click  
     
            'remove event handler  
            RemoveHandler NewButton.Click, AddressOf Button_Click  
     
        End Sub 
    End Class 
    Wednesday, January 14, 2009 8:26 PM
  • true but you still need to use the AddressOf
    Wednesday, January 14, 2009 8:32 PM
  • Dim myEventHandler As New EventHandler(AddressOf mysub)  'declare a variable with addressof your sub
            RemoveHandler mycontrol.myevent, myEventHandler  'remove it


    Just a guess from the error message

    Arjun Paudel
    • Marked as answer by Justin.NET Wednesday, January 14, 2009 9:19 PM
    Wednesday, January 14, 2009 8:35 PM
  • PainterEnterprises said:

    AHA!  I believe Arjun has hit the nail on the head.

    For my own information please:

    Is the Handler you're trying to remove actually declared at runtime by AddHandler...

    Or is the Handler you're trying to remove actually declared in your Class with the Handles Keyword?


    At runtime, yes. And thanks Arjun. That works.
    • Proposed as answer by ebol2000 Tuesday, August 30, 2011 7:23 PM
    Wednesday, January 14, 2009 9:17 PM
  • I ran into the same problem and this solution from Arjun worked for me as well.  However, the mystery to me is we use dynamic AddHandler and RemoveHandler all over our code and I've never seen this problem before.  To me this is still a mystery and perhaps a compiler bug.

     

    Update:  In my case, I had a parameterless subroutine I was calling.  When I added the parameter...

        Private Sub OnSomethingHappened(thing As Object)

    ... and then hovered over the error message, I got the same thing.  HOWEVER I then set:

    Option Explicit Off

    ... and again hover, I get the real error message that it needs the signature to be :

    Private Sub OnSomethingHappened(thing As Object, e As System.EventArgs)

    So in my case, I was able to fix this issue simply by putting in the obligatory arguments.  I'm definitely going with compiler bug.  I'm using VS 2010 sp1 and .net 3.5.

     


    Ericb
    Tuesday, August 30, 2011 7:03 PM
  • You are correct, this was my configuration. Upon removing the "treat all warnings as errors" it does revert to a simple warning.  What was throwing me was that the example where I found it "working" without any warning another dev had declared option Strict off (unbeknownst to me) and I had missed that fact.  

    So the other thing that threw me was that the warning did not show up as a warning.  In fact I had two errors... the original one above and then the secondary one informing me that a warning was considered an error.  I was surprised by this because I have seen other warnings in our solutions... likely caused by that particular project not having the same elevate warnings settings.  I saw "threw" me because I would expect the elevate setting to do the following:

    1. still leave a warning as a warning

    2. add the separate error indicating the warning was elevated

    ... thus leaving me with now 1 warning and 1 resulting error.  However, I end up with what looks like two errors.

     

    Anyway, thanks for the point Andrew.  Always something new to learn :).  

     


    Ericb
    Wednesday, August 31, 2011 3:30 PM
  • You shouldn't assign a variable. The IDE's advice is often a primitive workaround against your real purpose. The warning is because your method signature doesn't match that of the event. Check the arguments of the method - it should be exactly the same as the event defined.
    Monday, July 24, 2017 5:31 AM
  • Hi

    Replying to such an old post may not gain the OP attention :)


    Regards Les, Livingston, Scotland

    Monday, July 24, 2017 5:47 AM
  • Yes, but the OP is not the only one reading this post.

    I had this same issue (got barely decipherable warning about an Event I was trying to remove) and this answer is the best one.

    While it's true that declaring a var just to do the RemoveHandler will work, the problem is solved more cleanly by simply making sure the Event and its Handler have the same arguments. Once I fixed that, the warning went away.

    WATYF

    Thursday, January 10, 2019 9:21 PM