次の方法で共有


BC30506:Handles 句には、含んでいる型またはその基本型の 1 つで定義した WithEvents 変数が必要です。

Handles 句に WithEvents 変数を指定していません。 プロシージャ宣言の最後にある Handles キーワードによって、WithEvents キーワードを使用して宣言されたオブジェクト変数によって発生したイベントが処理されるようになります。

エラー ID: BC30506

このエラーを解決するには

必要な WithEvents 変数を指定します。

次の例では、Visual Basic によってコンパイラ エラー BC30506 が生成されます。これは、System.Timers.Timer インスタンスの定義で WithEvents キーワードが使用されていないためです。

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

次の例では、_timer1 変数が WithEvents キーワードで定義されているため、正常にコンパイルされます。

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

関連項目