Raise an event from another event

M

Mike

Hi group;





Let say I have an object called Account, that raises an event called
AccountLow with its owns EventArgs, and when this event gets raised, I
will like to raise another custom event called AccountGotLowAmount and
passes AccountLow’s EventArgs along with the new event.





How do I do this?
 
C

Cor Ligthert[MVP]

Hi Mike,

Why don't you just call the method Accou ntGotLowAmound, passing the sender
and the e (event args) to that?

It is much easier, better to mainenance and I thought that you cannot make
your own events because that is a part of Net.

Cor
"Mike" <[email protected]> schreef in bericht
Hi group;


Let say I have an object called Account, that raises an event called
AccountLow with its owns EventArgs, and when this event gets raised, I will
like to raise another custom event called AccountGotLowAmount and passes
AccountLow's EventArgs along with the new event.


How do I do this?
 
M

Michel Posseth [MCP]

Well i guess you can do 2 things

When you raise event AccountLow inmediatly raise AccountGotLowAmount
or crate a local handler for AccountLow ( in the sub new for instance with addhandler ) in the object and from this handler raise AccountGotLowAmount

Note that in both situations AccountLow will always be raised before AccountGotLowAmount is raised


hth

Michel Posseth




"Mike" <[email protected]> schreef in bericht Hi group;





Let say I have an object called Account, that raises an event called AccountLow with its owns EventArgs, and when this event gets raised, I will like to raise another custom event called AccountGotLowAmount and passes AccountLow's EventArgs along with the new event.





How do I do this?
 
C

Cor Ligthert[MVP]

brr,

Thanks Mike, I had read a book about C# during my holiday that did describe
this in another way, however I thought there were more errors in that book.
Of course you can raise an event.

Although, I keep the by me posted solution still better for maintenance.

:)

Cor
"Michel Posseth [MCP]" <[email protected]> schreef in bericht
Well i guess you can do 2 things

When you raise event AccountLow inmediatly raise AccountGotLowAmount
or crate a local handler for AccountLow ( in the sub new for instance with
addhandler ) in the object and from this handler raise AccountGotLowAmount

Note that in both situations AccountLow will always be raised before
AccountGotLowAmount is raised


hth

Michel Posseth




"Mike" <[email protected]> schreef in bericht
Hi group;


Let say I have an object called Account, that raises an event called
AccountLow with its owns EventArgs, and when this event gets raised, I will
like to raise another custom event called AccountGotLowAmount and passes
AccountLow's EventArgs along with the new event.


How do I do this?
 
G

Guest

Hello Mike

In answer to the e-mail you send ( i like to keep it in the group so anyone
can benefit and / or join in the discussion )

here are 2 simple examples

Raiseevent option :
Public Class Form1
Private WithEvents account As New Account
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
account.DosomeCalcOnAccount()
End Sub
Private Sub account_eAccountLow(ByVal Amount As Decimal) Handles
account.eAccountLow
MsgBox("eAccountLow :Amount:" & Amount.ToString)
End Sub
Private Sub account_eAccountGotLowAmount(ByVal Amount As Decimal)
Handles account.eAccountGotLowAmount
MsgBox("eAccountGotLowAmount :Amount:" & Amount.ToString)
End Sub
End Class
Public Class Account
'option 1
Public Event eAccountLow(ByVal Amount As Decimal) 'ofcourse the event
arguments could be anything you like
Public Event eAccountGotLowAmount(ByVal Amount As Decimal)
Public Sub New()
'nothing needed here
End Sub
Public Sub DosomeCalcOnAccount()
'do some stuff in a sub routine
'dummy routine
Dim amount As Decimal = 1000
Do Until amount = 100
amount -= 1
Loop
'hey the account got low :)
'simplest solution ( although i like the next example more as it
binds both events )
RaiseEvent eAccountLow(amount)
RaiseEvent eAccountGotLowAmount(amount)
End Sub
End Class

Private handler option :
Public Class Form1
Private WithEvents account As New Account
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
account.DosomeCalcOnAccount()
End Sub
Private Sub account_eAccountLow(ByVal Amount As Decimal) Handles
account.eAccountLow
MsgBox("eAccountLow :Amount:" & Amount.ToString)
End Sub
Private Sub account_eAccountGotLowAmount(ByVal Amount As Decimal)
Handles account.eAccountGotLowAmount
MsgBox("eAccountGotLowAmount notice that i am raised through the
clas`s handler :Amount:" & Amount.ToString)
End Sub
End Class
Public Class Account
'option 1
Public Event eAccountLow(ByVal Amount As Decimal) 'ofcourse the event
arguments could be anything you like
Public Event eAccountGotLowAmount(ByVal Amount As Decimal)
Public Sub New()
AddHandler eAccountLow, AddressOf sAccountGotLowAmount
End Sub
Private Sub sAccountGotLowAmount(ByVal Amount As Decimal)
RaiseEvent eAccountGotLowAmount(Amount)
End Sub
Public Sub DosomeCalcOnAccount()
'do some stuff in a sub routine
'dummy routine
Dim amount As Decimal = 1000
Do Until amount = 100
amount -= 1
Loop
'hey the account got low :)
RaiseEvent eAccountLow(amount)
End Sub
End Class

when i tested this code it turned out that i was wrong in a previous
asumption
cause in the second example eAccountGotLowAmount is externally raised before
the event eAccountLow.

hth

Michel
 
G

Guest

I already thought you were on a holliday , i honestly missed you ;-)

Thanks Mike, I had read a book about C# during my holiday that did describe
this in another way,

yes probably with delegates ( wich you can also do in VB , even bether
events and raisevents are automaticly converted by the compiler to exact the
same IL constructs as with delegates , i just find the VB syntax so much
easier )

regards

Michel




Cor Ligthert said:
brr,

Thanks Mike, I had read a book about C# during my holiday that did describe
this in another way, however I thought there were more errors in that book.
Of course you can raise an event.

Although, I keep the by me posted solution still better for maintenance.

:)

Cor
"Michel Posseth [MCP]" <[email protected]> schreef in bericht
Well i guess you can do 2 things

When you raise event AccountLow inmediatly raise AccountGotLowAmount
or crate a local handler for AccountLow ( in the sub new for instance with
addhandler ) in the object and from this handler raise AccountGotLowAmount

Note that in both situations AccountLow will always be raised before
AccountGotLowAmount is raised


hth

Michel Posseth




"Mike" <[email protected]> schreef in bericht
Hi group;


Let say I have an object called Account, that raises an event called
AccountLow with its owns EventArgs, and when this event gets raised, I will
like to raise another custom event called AccountGotLowAmount and passes
AccountLow's EventArgs along with the new event.


How do I do this?
 

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