Multiple Event Handlers for a Single Event - Newbie Question.

A

Andy Cooper

Hi,

If I am not mistaken, it is possible to have multiple event handlers that
deal with a particular event. If more than one event handler exists, is
there a specific order in which the handlers are called? i.e. How do I
determine which handler is called first, second third etc?

Thanks,

Andy
 
J

Jarod_24

Andy Cooper said:
Hi,

If I am not mistaken, it is possible to have multiple event handlers that
deal with a particular event. If more than one event handler exists, is
there a specific order in which the handlers are called? i.e. How do I
determine which handler is called first, second third etc?

Thanks,

Andy


Hm....
Why not make a Public accsessible String that you in each event-handler add
the name of the Called method

Public strString as String

....
'In the event handler you do this
strString = strString & " myMethodName"


This way you'd see wich method was called first.
You'd better run this a few times an check if it's done in the same order
each time, i don't think it will though.
 
J

Jan Tielens

I think it's not possible to determine which event handler ocurs first... In
my opinion this would be agains the event driven model.
 
C

Codemonkey

Andy,

There isn't to my knowlege a way of forcing the runtime to call event
handlers in a specific order.

One way to get around this is to have one event handler and then to call
different satellite functions that used to be other event handlers. e.g.

--------------------

Private Sub Button1_Click(ByVal sender as Object, ByVal e as EventArgs)
handles Button1.Click

DoFunction1()
DoFunction2()
DoFunction3()

End Sub

--------------------

IMHO, this is better than having multiple event handlers - it keeps your
logic a bit tidier and less spread around.

Hope this helps,

Trev.
 
K

Ken Tucker [MVP]

Hi,

If you use addhandler to add multiple event handlers for a single
event they are fired in the order they are added.

AddHandler lblBalance.DataBindings(0).Format, AddressOf Me.FormatBalance

AddHandler lblBalance.DataBindings(0).Format, AddressOf Me.one

AddHandler lblBalance.DataBindings(0).Format, AddressOf Me.two



Ken
 
K

Ken Tucker [MVP]

Hi,

I remember reading it when studing for my vb.net win forms cert. I
unfortunately cant find a link for it right now.

Ken
 
H

Herfried K. Wagner [MVP]

* "Andy Cooper said:
If I am not mistaken, it is possible to have multiple event handlers that
deal with a particular event. If more than one event handler exists, is
there a specific order in which the handlers are called? i.e. How do I
determine which handler is called first, second third etc?

You can add handlers using the 'AddHandler' statement. The handlers are
added to a "list" of delegates and will be executed in the order they
were added.
 
C

Codemonkey

No problem. I still wouldn't trust it without confirmation that the
implementation of how events are called will not change with future versions
of the language. For example, afaik, events are implemented using multicast
delegates. A multicast delegate contains an array of delegates to invoke.
What happens if the array is changed to a hashtable (which doesn't always
return things in the order they were added) in a future version?

IMHO if you need multiple event handlers to be called in a certain order,
use a single handler and manually call satellite functions.

Of course there probably is a big bold sentence somewhere in MSDN stating in
concrete that events will always be called in the order that they are added,
but finding anything in MSDN is no small task :-S

Trev.
 

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