By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,617 Members | 1,792 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,617 IT Pros & Developers. It's quick & easy.

Use Interfaces to Hook up Event Handlers

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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

"Charles Law" <bl***@nowhere.com> wrote in message
news:ey**************@tk2msftngp13.phx.gbl...
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

Nov 20 '05 #2

P: n/a
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
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OK**************@tk2msftngp13.phx.gbl...
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

"Charles Law" <bl***@nowhere.com> wrote in message
news:ey**************@tk2msftngp13.phx.gbl...
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


Nov 20 '05 #3

P: n/a
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

"Charles Law" <bl***@nowhere.com> wrote in message
news:ON*************@tk2msftngp13.phx.gbl...
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
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OK**************@tk2msftngp13.phx.gbl...
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

"Charles Law" <bl***@nowhere.com> wrote in message
news:ey**************@tk2msftngp13.phx.gbl...
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



Nov 20 '05 #4

P: n/a
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

"Charles Law" <bl***@nowhere.com> wrote in message
news:ON*************@tk2msftngp13.phx.gbl...
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
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OK**************@tk2msftngp13.phx.gbl...
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

"Charles Law" <bl***@nowhere.com> wrote in message
news:ey**************@tk2msftngp13.phx.gbl...
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



Nov 20 '05 #5

P: n/a
Thanks Jay, I get it now.

Charles
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
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

"Charles Law" <bl***@nowhere.com> wrote in message
news:ON*************@tk2msftngp13.phx.gbl...
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
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:OK**************@tk2msftngp13.phx.gbl...
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

"Charles Law" <bl***@nowhere.com> wrote in message
news:ey**************@tk2msftngp13.phx.gbl...
> 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
>
>



Nov 20 '05 #6

P: n/a
Thanks Jay, I get it now.

Charles
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
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

"Charles Law" <bl***@nowhere.com> wrote in message
news:ON*************@tk2msftngp13.phx.gbl...
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
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:OK**************@tk2msftngp13.phx.gbl...
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

"Charles Law" <bl***@nowhere.com> wrote in message
news:ey**************@tk2msftngp13.phx.gbl...
> 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
>
>



Nov 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.