Just to add some explanations... The thing if on the constructor of ClassB. As the ClassA Event is shared, you just need to add an handler to it. If it fires, any and every handle will catch it.
On the Module1 Sub Main I declare the two classes and set the 'P' property that actually fires the event.
You can add another ReadLine to make the execution stop after the event is fired so you can see the "I catched it!" message:
Sub Main() Dim myClassA As New ClassA Dim myClassB As New ClassB
As you said "Shared" I assume that you're using VB.net. Here's some sample code of a VB Console Project:
Module Module1
Sub Main() Dim myClassA As New ClassA Dim myClassB As New ClassB
Console.ReadLine()
myClassA.P1 = 10 End Sub
End Module
Public Class ClassA
Public Shared Event myClassAEvent()
Private _p1 As Integer = 0 Public Property P1() As Integer Get Return _p1 End Get Set(ByVal value As Integer) _p1 = value RaiseEvent myClassAEvent() End Set End Property
End Class
Public Class ClassB
Public Sub New() AddHandler ClassA.myClassAEvent, AddressOf ClassAEventHandler End Sub
Public Sub ClassAEventHandler() Console.WriteLine("I catched it!") End Sub
End Class
How can instances of ClassB catch and respond to Shared Events in ClassA ?
How can instances of ClassB catch and respond to Shared Events in ClassA ?
D.V.Sridhar
The thing if on the constructor of ClassB.
As the ClassA Event is shared, you just need to add an handler to it. If it fires, any and every handle will catch it.
On the Module1 Sub Main I declare the two classes and set the 'P' property that actually fires the event.
You can add another ReadLine to make the execution stop after the event is fired so you can see the "I catched it!" message:
Sub Main()
Dim myClassA As New ClassA
Dim myClassB As New ClassB
Console.ReadLine()
myClassA.P1 = 10
Console.ReadLine()
End Sub
Justin-M
As you said "Shared" I assume that you're using VB.net.
Here's some sample code of a VB Console Project:
Module Module1
Sub Main()
Dim myClassA As New ClassA
Dim myClassB As New ClassB
Console.ReadLine()
myClassA.P1 = 10
End Sub
End Module
Public Class ClassA
Public Shared Event myClassAEvent()
Private _p1 As Integer = 0
Public Property P1() As Integer
Get
Return _p1
End Get
Set(ByVal value As Integer)
_p1 = value
RaiseEvent myClassAEvent()
End Set
End Property
End Class
Public Class ClassB
Public Sub New()
AddHandler ClassA.myClassAEvent, AddressOf ClassAEventHandler
End Sub
Public Sub ClassAEventHandler()
Console.WriteLine("I catched it!")
End Sub
End Class