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 6 1771
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
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
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
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
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 > >
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 > >
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Franco Gustavo |
last post by:
Hi,
Please help me to understand this, because I don't see what I'm missing.
I was reading a lot of examples on Internet that explain that C# doesn't
implement multiple inheritance it...
|
by: Dan |
last post by:
Good Day All,
I have an application that processes several tasks. Depending upon the task
it instantiates the appropriate object to do the actual work. Since I wanted
the application to be...
|
by: zeng.hui.stephen |
last post by:
I download the demo
http://msdn.microsoft.com/msdnmag/issues/02/10/cuttingedge/. I
inherite the demo, and write my code.
I want to use Hook to monitor C++ Edit change. I use a C# form...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |