Event with response?

  • Thread starter Thread starter govolsbaby
  • Start date Start date
G

govolsbaby

Not sure how to ask this so I'll try a couple ways.

So I have a class with an event...

Public Class myClass
Public Event SomethingsGoingOn()
Private Sub SomethingHappened(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Something.Happened
'code here changes some properties of myClass
RaiseEvent SomethingsGoingOn()
End Sub
End Class

Then I've got this other class with an instance of the first class
with a handler for the event...

Public Class myOtherClass
Private _myClass As myClass
Public Sub New()
_myClass = New myClass
Controls.Add(_myClass)
AddHandler _myClass.SomethingsGoingOn, AddressOf myHandler
End Sub
Public Sub myHandler()
'the code here depends on what happened, depends on the
properties of myClass
End Sub
End Class

So everything works fine. An event is raised in myClass that is
handled by myOtherClass. But suppose I'd like to myClass to do
something else based upon myOtherClass's reaction to what myClass did
in the first place. How do I get the reaction back to myClass so it
can do whatever it needs to do???

Here's a bad analogy:

I'm an instance of Dad class and I hold an instance of Kid class and
the Kid has a Wallet property. When Kid class brings home a report
card, I want to know about it (that's the event) But all I (Dad
class) want to do is look at the report card and then give the Kid
permission to go get 5 bucks out of my sock drawer if I like what I
see (that's the handler). And then the Kid's Wallet property may or
may not increase by 5 bucks. But I (Dad class) don't want to go to
the sock drawer, I want the Kid to do it (his Wallet property is
private and Dad can't get it or set it).
 
By using something else than System.EventArgs for the e argument.

For example see the FormClosing event. Its argument provides a Cancel
property you can use if the event handler wants to cancel the close.

Does it sounds like what you are looking for ? Do you need a sample ?

Thanks. No, I don't think I need a sample. Sometimes I just need a
little push in the right direction.
 
Not sure how to ask this so I'll try a couple ways.

So I have a class with an event...

Public Class myClass
    Public Event SomethingsGoingOn()
    Private Sub SomethingHappened(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Something.Happened
        'code here changes some properties of myClass
        RaiseEvent SomethingsGoingOn()
    End Sub
End Class

Then I've got this other class with an instance of the first class
with a handler for the event...

Public Class myOtherClass
    Private _myClass As myClass
    Public Sub New()
        _myClass = New myClass
        Controls.Add(_myClass)
        AddHandler _myClass.SomethingsGoingOn, AddressOf myHandler
    End Sub
    Public Sub myHandler()
        'the code here depends on what happened, depends on the
properties of myClass
    End Sub
End Class

So everything works fine.  An event is raised in myClass that is
handled by myOtherClass.  But suppose I'd like to myClass to do
something else based upon myOtherClass's reaction to what myClass did
in the first place.  How do I get the reaction back to myClass so it
can do whatever it needs to do???

Here's a bad analogy:

I'm an instance of Dad class and I hold an instance of Kid class and
the Kid has a Wallet property.  When Kid class brings home a report
card, I want to know about it (that's the event)  But all I (Dad
class) want to do is look at the report card and then give the Kid
permission to go get 5 bucks out of my sock drawer if I like what I
see (that's the handler).  And then the Kid's Wallet property may or
may not increase by 5 bucks.  But I (Dad class) don't want to go to
the sock drawer, I want the Kid to do it (his Wallet property is
private and Dad can't get it or set it).

One interesting thing you can do is define a return type on the
delegate that drives the event. I'm not sure if you can do this in VB,
but I know it's valid in C#.

Please see: http://sethrowe.blogspot.com/2008/09/returning-values-from-event.html
for a quick rundown and sample.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
Not sure how to ask this so I'll try a couple ways.

So I have a class with an event...

Public Class myClass
Public Event SomethingsGoingOn()
.. . .

More usually, that's written as :

Public Class myClass

Public Event XHappening( sender as Object, e as EventArgs )

Public Event XHappened( sender as Object, e as EventArgs )

Protected Sub OnXHappening(e As System.EventArgs)
RaiseEvent XHappening( Me, e )
End Sub

Protected Sub OnXHappened(e As System.EventArgs)
RaiseEvent XHappened( Me, e )
End Sub

End Class

You'd often have custom EventArgs classes for each event; the *ing ones
should have a way of "cancelling" the event, the *ed ones need not have.
(that's a gross generalisation, but I've found it useful).
Then I've got this other class with an instance of the first class
with a handler for the event...

Public Class myOtherClass
Private _myClass As myClass
Public Sub New()
_myClass = New myClass
Controls.Add(_myClass)
AddHandler _myClass.SomethingsGoingOn, AddressOf myHandler
End Sub

Public Sub myHandler(sender as Object, e as EventArgs)
' the code here depends on what happened, depends on the
' properties of myClass

If TypeOf sender is myClass Then
With DirectCast( sender, myClass )
... use whatever properties you like here
End With
End If

End Sub
End Class

HTH,
Phill W.
 
Back
Top