PC Review


Reply
Thread Tools Rate Thread

Event with response?

 
 
govolsbaby@gmail.com
Guest
Posts: n/a
 
      18th Sep 2008
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).

 
Reply With Quote
 
 
 
 
govolsbaby@gmail.com
Guest
Posts: n/a
 
      18th Sep 2008
On Sep 18, 9:37*am, "Patrice" <http://www.chez.com/scribe/> wrote:
> 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 ?
>
> --
> Patrice
>


Thanks. No, I don't think I need a sample. Sometimes I just need a
little push in the right direction.
 
Reply With Quote
 
 
 
 
rowe_newsgroups
Guest
Posts: n/a
 
      18th Sep 2008
On Sep 18, 10:23*am, govolsb...@gmail.com wrote:
> 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...rom-event.html
for a quick rundown and sample.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
Reply With Quote
 
Phill W.
Guest
Posts: n/a
 
      19th Sep 2008
(E-Mail Removed) wrote:
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Event ID: 1003, Event Type: Error ,Event Source: System Error,Event Category: (102) BoazBoaz Windows XP General 0 21st Jun 2006 05:39 PM
I need to clear temporary the event sinks from another event and then restore them, but I don't know which methods signed up for that event Serge Shimanovsky Microsoft Dot NET 1 14th Apr 2005 05:37 AM
I need to clear temporary the event sinks from another event and then restore them, but I don't know which methods signed up for that event Serge Shimanovsky Microsoft Dot NET Framework Forms 14 7th Feb 2005 09:48 PM
Event Procedures: Event on Worksheet to fire Event on another Worksheet Kathryn Microsoft Excel Programming 2 7th Apr 2004 07:35 PM
Event 29, Event 40961 and Event 1054 Joe Windows XP Networking 0 30th Oct 2003 03:20 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:21 PM.