C
Charles Law
I have a class, which implements an interface. Let's say, that the interface
looks something like
Public Interface IEventSinks
Sub ValueChanged(sender As Object, e As ValueChangedEventArgs)
Sub StateUpdated(sender As Object, e As StateUpdatedEventArgs)
End Interface
In practice, the interface contains many more event handlers like this, but
you get the picture.
I have another interface like this
Public Interface IEventSinks2
Sub SomethingElseHappened(sender As Object, e As
SomethingElseHappenedEventArgs)
...
End Interface
Then, I have my classes:
Public Class ClassA
Inherits ClassBase
Implements IEventSinks
...
End Class
Public Class ClassB
Inherits ClassBase
Implements IEventSinks2
...
End Class
Now, in my main code, I have
Dim obj1 As ClassBase = New ClassA
Dim obj2 As ClassBase = New ClassB
If TypeOf obj1 Is IEventSinks Then
AddHandler ValueChangedEventSource, AddressOf obj1.ValueChanged
AddHandler StateUpdatedEventSource, AddressOf obj1.StateUpdated
ElseIf TypeOf obj1 Is IEventSinks2 Then
AddHandler SomethingElseHappenedEventSource, AddressOf
obj1.SomethingElseHappened
Endif
As you can see, even for a couple of event handlers it starts to get a bit
messy, and hard to maintain. In reality, I have seven different object types
and eight different interfaces that may or may not be implemented. Depending
on the interfaces implemented, I need to notify my classes of different
events.
Is there a more convenient way to hook up handlers based on interfaces
implemented? For example, if my event source class and event handler class
both implement a particular interface, it would be great if I could just
write one line that says "ClassX handles all events raised by ClassY based
on a common interface".
As Cor has already observed, I try to simplify my examples initially and
build them up later, so if what I am trying to do looks odd it could be
because I am paraphrasing.
TIA
Charles
looks something like
Public Interface IEventSinks
Sub ValueChanged(sender As Object, e As ValueChangedEventArgs)
Sub StateUpdated(sender As Object, e As StateUpdatedEventArgs)
End Interface
In practice, the interface contains many more event handlers like this, but
you get the picture.
I have another interface like this
Public Interface IEventSinks2
Sub SomethingElseHappened(sender As Object, e As
SomethingElseHappenedEventArgs)
...
End Interface
Then, I have my classes:
Public Class ClassA
Inherits ClassBase
Implements IEventSinks
...
End Class
Public Class ClassB
Inherits ClassBase
Implements IEventSinks2
...
End Class
Now, in my main code, I have
Dim obj1 As ClassBase = New ClassA
Dim obj2 As ClassBase = New ClassB
If TypeOf obj1 Is IEventSinks Then
AddHandler ValueChangedEventSource, AddressOf obj1.ValueChanged
AddHandler StateUpdatedEventSource, AddressOf obj1.StateUpdated
ElseIf TypeOf obj1 Is IEventSinks2 Then
AddHandler SomethingElseHappenedEventSource, AddressOf
obj1.SomethingElseHappened
Endif
As you can see, even for a couple of event handlers it starts to get a bit
messy, and hard to maintain. In reality, I have seven different object types
and eight different interfaces that may or may not be implemented. Depending
on the interfaces implemented, I need to notify my classes of different
events.
Is there a more convenient way to hook up handlers based on interfaces
implemented? For example, if my event source class and event handler class
both implement a particular interface, it would be great if I could just
write one line that says "ClassX handles all events raised by ClassY based
on a common interface".
As Cor has already observed, I try to simplify my examples initially and
build them up later, so if what I am trying to do looks odd it could be
because I am paraphrasing.
TIA
Charles