events with array variable?

T

ThunderMusic

Hi,
I have a class let's say class1 that raises an event, let's say OnEvent.
In another class, let's say class2, I have an array of Class1. I want to be
able to trap the events fired by class1 within class2 and know (if possible)
the index of the class1 that fired the event. Is there a way? because
withevents does not work.

Here is some code you could rely on if you don't understand what I just
explain :

Class Class1
public event OnEvent()
End Class

Class Class2
private m_MyArray(10) as Class1 'WithEvents doesn't work, so I don't
write it
End Class

So, what should I do from there to catch the events fired by class1 into
class2? I figured it could be with AddHandler, but I'd have to store the
index in Class1 and I don't want to do that if possible. If it's the only
way, I'll do it this way, but I want to know if there is a better way.

Thanks

ThunderMusic
 
S

Samuel R. Neff

You can store the array of classes in a SortedList and use IndexOfKey
to determine the index of the class when the event is broadcasted.

Or you can use a third class that just stores the index and proxies
the event along with the index.

HTH,

Sam
 
G

Guest

You're right. Do it with AddHandler.
For the index problem, Write:
m_MyArray.IndexOf(m_MyArray, sender)
In the sub where you catch the event.
 
T

ThunderMusic

ok, but I'd still have to use the AddHandler instead of WithEvents. I'm I
right?

thanks for quick answer
 
T

ThunderMusic

thanks for the quick answer

Amiram Korach said:
You're right. Do it with AddHandler.
For the index problem, Write:
m_MyArray.IndexOf(m_MyArray, sender)
In the sub where you catch the event.
 

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