Single event handler for multiple instances of one object

bufer24

Active member
Joined
Mar 27, 2008
Messages
35
Programming Experience
3-5
Hello, is there a way to create a single event handler for multiple instances of one object?
For example, I have a custom class "myObject" that contains one event that is fired through some user input. Then I create an array of objects:

Code:
class myObject
  public event someEvent
end class
'---------------------------
class form1
  dim obj() as myObject
  dim o1 as new myObject
  dim o2 as new myObject

  sub Form1_Load
    redim obj(1)
    obj(0)=obj1
    obj(1)=obj2
  end sub

  sub handleEvent
    'I want to have some code here that reacts to firing "someEvent" event of
    'any of the object instances in the obj() array
  end sub

end class
 

bufer24

Active member
Joined
Mar 27, 2008
Messages
35
Programming Experience
3-5
but what if there are 100 instances of one object? Using comma separated list, I would have to name 100 items explicitly, so that is not the right way for me.
The second way - using handlers - is new for me and I don´t quite understand it :(
 

Stonkie

Well-known member
Joined
Sep 12, 2007
Messages
279
Programming Experience
1-3
Maybe you could use a static event? Use the Shared keyword in front of the event declaration and pass the sender object correctly so you can find it from the event handler... I never tried it, but I guess it should work...
 

Rocksteady

Well-known member
Joined
Jan 22, 2008
Messages
63
Programming Experience
1-3
Sounds like you want to dynamically assign event handlers.

Try AddHandler.

Code:
For Each myObj As myObject in obj
   Addhandler myObj.someEvent, AddressOf handleEvent
Next
 

bufer24

Active member
Joined
Mar 27, 2008
Messages
35
Programming Experience
3-5
this may be what I was looking for. I´m going to try it. Thanks a lot ;)
 
Top Bottom