Use Interfaces to Hook up Event Handlers

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
 
J

Jay B. Harlow [MVP - Outlook]

Charles,
Have you considered simply using the Interfaces as the event handlers?

Which effectively is Java's "Event Listener" pattern?

Instead of using AddHandler to add each method of the interface to the
Event, save the Interface variable itself in a collection, when you go to
raise the Event, iterate this collection and call the respective method.

Hope this helps
Jay
 
C

Charles Law

Hi Jay

I think I understand what you mean, in principle, but could you maybe give a
simple example, perhaps based on my sample below? One of the things I have
also to take into account, is that a class may implement multiple
interfaces, so I would need to be able to cater for that as well.

Thanks.

Charles
 
J

Jay B. Harlow [MVP - Outlook]

Charles,
You can implement the IEventSinks collection something like:

Public Class EventSinkCollection
Inherits CollectionBase

Public Sub Add(ByVal eventSink As IEventSinks)
Me.InnerList.Add(eventSink)
End Sub

Public Sub Remove(ByVal eventSink As IEventSinks)
Me.InnerList.Remove(eventSink)
End Sub

Public Sub RaiseValueChanged(ByVal sender As Object, ByVal e As
ValueChangedEventArgs)
For Each sink As IEventSinks In Me.InnerList
sink.ValueChanged(sender, e)
Next
End Sub

Public Sub RaiseStateUpdated(ByVal sender As Object, ByVal e As
StateUpdatedEventArgs)
For Each sink As IEventSinks In Me.InnerList
sink.StateUpdated(sender, e)
Next
End Sub

End Class

Then when ever you wanted to raise the ValueChanged event you would call
EventSinkCollection.RaiseValueChanged.

The IEventSinks2 collection would be implemented in a similar manner.

Every object that raised the IEventSinks' events would need to have an
instance of the EventSinkCollection object & delegate methods to it.

Hope this helps
Jay
 
C

Charles Law

Thanks Jay, I get it now.

Charles


Jay B. Harlow said:
Charles,
You can implement the IEventSinks collection something like:

Public Class EventSinkCollection
Inherits CollectionBase

Public Sub Add(ByVal eventSink As IEventSinks)
Me.InnerList.Add(eventSink)
End Sub

Public Sub Remove(ByVal eventSink As IEventSinks)
Me.InnerList.Remove(eventSink)
End Sub

Public Sub RaiseValueChanged(ByVal sender As Object, ByVal e As
ValueChangedEventArgs)
For Each sink As IEventSinks In Me.InnerList
sink.ValueChanged(sender, e)
Next
End Sub

Public Sub RaiseStateUpdated(ByVal sender As Object, ByVal e As
StateUpdatedEventArgs)
For Each sink As IEventSinks In Me.InnerList
sink.StateUpdated(sender, e)
Next
End Sub

End Class

Then when ever you wanted to raise the ValueChanged event you would call
EventSinkCollection.RaiseValueChanged.

The IEventSinks2 collection would be implemented in a similar manner.

Every object that raised the IEventSinks' events would need to have an
instance of the EventSinkCollection object & delegate methods to it.

Hope this helps
Jay

give
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top