Event with response?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • govolsbaby@gmail.com

    #1

    Event with response?

    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 SomethingsGoing On()
    Private Sub SomethingHappen ed(ByVal sender As Object, ByVal e As
    System.EventArg s) Handles Something.Happe ned
    'code here changes some properties of myClass
    RaiseEvent SomethingsGoing On()
    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(_m yClass)
    AddHandler _myClass.Someth ingsGoingOn, 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).

  • Patrice

    #2
    Re: Event with response?

    By using something else than System.EventArg s 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

    <govolsbaby@gma il.coma écrit dans le message de groupe de discussion :
    4a13da69-a1a5-4701-85ac-6cc70de5738c...le groups.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 SomethingsGoing On()
    Private Sub SomethingHappen ed(ByVal sender As Object, ByVal e As
    System.EventArg s) Handles Something.Happe ned
    'code here changes some properties of myClass
    RaiseEvent SomethingsGoing On()
    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(_m yClass)
    AddHandler _myClass.Someth ingsGoingOn, 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).
    >

    Comment

    • govolsbaby@gmail.com

      #3
      Re: Event with response?

      On Sep 18, 9:37 am, "Patrice" <http://www.chez.com/scribe/wrote:
      By using something else than System.EventArg s 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.

      Comment

      • rowe_newsgroups

        #4
        Re: Event with response?

        On Sep 18, 10:23 am, govolsb...@gmai l.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 SomethingsGoing On()
            Private Sub SomethingHappen ed(ByVal sender As Object, ByVal e As
        System.EventArg s) Handles Something.Happe ned
                'code here changes some properties of myClass
                RaiseEvent SomethingsGoing On()
            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(_m yClass)
                AddHandler _myClass.Someth ingsGoingOn, 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]

        Comment

        • Patrice

          #5
          Re: Event with response?

          What I had in mind is mostly the On<Eventpattern rather than raising
          directly the event where needed. This way it's easy to change something
          before/after the event is raised...

          --
          Patrice

          <govolsbaby@gma il.coma écrit dans le message de groupe de discussion :
          54f02120-eec2-4c37-bcd8-94729667ec2d...l egroups.com...
          On Sep 18, 9:37 am, "Patrice" <http://www.chez.com/scribe/wrote:
          >By using something else than System.EventArg s 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.

          Comment

          • Phill W.

            #6
            Re: Event with response?

            govolsbaby@gmai l.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 SomethingsGoing On()
            .. . .

            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.EventArg s)
            RaiseEvent XHappening( Me, e )
            End Sub

            Protected Sub OnXHappened(e As System.EventArg s)
            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(_m yClass)
            AddHandler _myClass.Someth ingsGoingOn, AddressOf myHandler
            End Sub

            Public Sub myHandler(sende r 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.

            Comment

            Working...