BC30506: Handles clause requires a WithEvents variable defined in the containing type or one of its base types

You did not supply a WithEvents variable in your Handles clause. The Handles keyword at the end of a procedure declaration causes it to handle events raised by an object variable declared using the WithEvents keyword.

Error ID: BC30506

To correct this error

Supply the necessary WithEvents variable.

Example

In the following example, Visual Basic generates compiler error BC30506 because the WithEvents keyword is not used in the definition of the System.Timers.Timer instance.

Imports System.Timers

Module Module1
    Private _timer1 As New Timer() With {.Interval = 1000, .Enabled = True}

    Sub Main()
        Console.WriteLine("Press any key to start the timer...")
        Console.ReadKey()
        _timer1.Start()
        Console.ReadKey()
    End Sub

    Private Sub Timer1_Tick(sender As Object, args As EventArgs) Handles _timer1.Elapsed
        Console.WriteLine("Press any key to terminate...")
    End Sub
End Module

The following example compiles successfully because the _timer1 variable is defined with the WithEvents keyword:

Imports System.Timers

Module Module1
    Private WithEvents _timer1 As New Timer() With {.Interval = 1000}

    Sub Main()
        Console.WriteLine("Press any key to start the timer...")
        Console.ReadKey()
        _timer1.Start()
        Console.ReadKey()
    End Sub

    Private Sub Timer1_Tick(sender As Object, args As EventArgs) Handles _timer1.Elapsed
        Console.WriteLine("Press any key to terminate...")
    End Sub

End Module

See also