Joseph Thomas Lavoie
Joseph Thomas Lavoie

Reputation: 82

vb.net, cannot call event directly

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?

Upvotes: 1

Views: 4365

Answers (3)

Tim Schmelter
Tim Schmelter

Reputation: 460380

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

Upvotes: 7

GSerg
GSerg

Reputation: 78210

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.

Upvotes: 6

pylover
pylover

Reputation: 8075

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.

Upvotes: 4

Related Questions