Export (0) Print
Expand All

10.5.1 RaiseEvent Statement

Visual Studio .NET 2003

A RaiseEvent statement notifies event handlers that a particular event has occurred. The simple name expression in a RaiseEvent statement is interpreted as a member lookup on Me. Thus, RaiseEvent x is interpreted as if it were RaiseEvent Me.x. The result of the expression must be classified as an event access for an event defined in the class itself; events defined on base types cannot be used in a RaiseEvent statement.

The RaiseEvent statement is processed as a call to the Invoke method of the event's delegate, using the supplied parameters, if any. If there are no arguments, the parentheses may be omitted. For example:

Imports System

Class Raiser
    Public Event Constructed(ByVal Count As Integer)

    Public Sub New()
        Static CreationCount As Integer = 0

        CreationCount += 1
        RaiseEvent Constructed(CreationCount)
    End Sub
End Class

Module Test
    Private WithEvents x As Raiser

    Private Sub Constructed(ByVal Count As Integer) Handles x.Constructed
        Console.WriteLine("Constructed instance #" & Count)
    End Sub

    Public Sub Main()
        x = New Raiser   ' Causes "Constructed instance #1" to be printed.
        x = New Raiser   ' Causes "Constructed instance #2" to be printed.
        x = New Raiser   ' Causes "Constructed instance #3" to be printed.
    End Sub
End Module

The class Raiser above is equivalent to:

Class Raiser
    Public Event Constructed(ByVal Count As Integer)

    Public Sub New()
        Static CreationCount As Integer = 0

        CreationCount += 1
        ConstructedEvent.Invoke(CreationCount)
    End Sub
End Class
RaiseEventStatement ::= RaiseEvent SimpleNameExpression
   [ ( [ ArgumentList ] ) ] StatementTerminator

See Also

10.5.2 AddHandler and RemoveHandler Statements | 10.5 Event Statements | RaiseEvent Statement (Visual Basic Language Reference)

Show:
© 2014 Microsoft