1

I have a piece of code:

innerchannel.Closing += Function(sender, e)
                        RaiseEvent Closing(sender, e)

                        End Function

There's an issue with

innerchannel.Closing 

in which VisualStudio is telling me:

Public Event Closing(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

How do I repair this to work accordingly?

1
  • What is it you're trying to accomplish? Are you attempting to listen to the innerchannel object's Closing event and have it handled by your local function Closing?
    – lsuarez
    Commented Mar 27, 2012 at 20:16

3 Answers 3

7

I assume you want to add a handler to your Closing-event:

AddHandler innerchannel.Closing, AddressOf Closed

If you want to raise a custom event, for example in an UserControl:

Class Channel
    Inherits UserControl
    Public Event Closing(ch As Channel)

    ' this could be for example a button-click handler
    Protected Sub Closed(sender As Object, e As System.EventArgs)
        RaiseEvent Closing(Me)
    End Sub
End Class
0
6

You don't use += in VB to add event handlers. You use AddHandler.

Your code is trying to call Closing as if it was a function and perform an addition to its result.

4

firs you must to attach method to event handler like this:

AddHandler innerchannel.Closing, AddressOf Closed

and use RaiseEvent

note from msdn:

If the event has not been declared within the module in which it is raised, an error occurs. The following fragment illustrates an event declaration and a procedure in which the event is raised.

Not the answer you're looking for? Browse other questions tagged or ask your own question.