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:

VB.NET:
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
 
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 :(
 
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...
 
Sounds like you want to dynamically assign event handlers.

Try AddHandler.

VB.NET:
For Each myObj As myObject in obj
   Addhandler myObj.someEvent, AddressOf handleEvent
Next
 
Back
Top