How to pass more than two parameters in the event handler

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?U3dhcHB5?=

    How to pass more than two parameters in the event handler

    Can anyone suggest me to pass more parameters other than two parameter for
    events like the following?

    Event:
    Onbutton_click( object sender, EventArgs e)"

    Event handler:
    button.Click += new EventHandler(On button_click);

    I want to pass more information related that event. & want to use that
    information in the event.
  • Jon Skeet [C# MVP]

    #2
    Re: How to pass more than two parameters in the event handler

    Swappy <Swappy@discuss ions.microsoft. comwrote:
    Can anyone suggest me to pass more parameters other than two parameter for
    events like the following?
    >
    Event:
    Onbutton_click( object sender, EventArgs e)"
    >
    Event handler:
    button.Click += new EventHandler(On button_click);
    >
    I want to pass more information related that event. & want to use that
    information in the event.
    The normal way of passing more information is to make a richer type
    derived from EventHandler.

    --
    Jon Skeet - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon_skeet
    C# in Depth: http://csharpindepth.com

    Comment

    • Peter Duniho

      #3
      Re: How to pass more than two parameters in the event handler

      On Wed, 16 Jul 2008 22:47:01 -0700, Swappy
      <Swappy@discuss ions.microsoft. comwrote:
      Can anyone suggest me to pass more parameters other than two parameter
      for
      events like the following?
      >
      Event:
      Onbutton_click( object sender, EventArgs e)"
      >
      Event handler:
      button.Click += new EventHandler(On button_click);
      Minor nit: I think you have your labels reversed. :) The "Event" is
      "button.Cli ck" and the "Event handler" is the method itself.
      I want to pass more information related that event. & want to use that
      information in the event.
      What kind of information? Where would it come from?

      The usual approach is to use an anonymous method with an event handler
      that has your modified signature. For example:

      void Onbutton_click( object sender, EventArgs e, int i) { ... }

      button.Click += delegate(object sender, EventArgs e)
      { Onbutton_click( sender, e, 172); };

      Of course, you don't have to pass in 172, or even make the third parameter
      an int. :)

      But whether that works depends on what information you're trying to pass..
      If the above example doesn't lead to an immediate solution for you, you
      might want to provide more details as to what you're actually trying to do.

      Pete

      Comment

      • =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=

        #4
        RE: How to pass more than two parameters in the event handler

        Hey,
        Jon Skeet is right with his suggestion about deriving from event args and
        making you own delegate for the event. That is the standard way .NET does it
        and there is necessarily anything wrong with it.
        There is a bit of debate about it though as some people suggest that it is
        not ideal to parse a reference to the sender and an EventArgs derived class
        to a set of event handlers that only need to know a boolean or and int. They
        believe that having lots of derivations of EventArgs introduces unnecessary
        code bloat and is a waste of time.
        I am personally undecided on the matter as although I like the idea of
        having a standard 'look' for events, I also agree with the simplicity of just
        parsing the needed data for events. The only down side of this is if you do
        decide in the future to add a second piece of data, you need to change the
        event and therefore all the subscribers. EventArgs does get round that for
        you. For futher information on this argument and events in general, there was
        recently a dotnetrocks episode on them:


        I will leave it to you to come up with the solution yourself. Just thought I
        would make you aware of the two sides to the debate.
        --
        Ciaran O''Donnell
        try{ Life(); } catch (TooDifficultException) { throw Toys(); }



        "Swappy" wrote:
        Can anyone suggest me to pass more parameters other than two parameter for
        events like the following?
        >
        Event:
        Onbutton_click( object sender, EventArgs e)"
        >
        Event handler:
        button.Click += new EventHandler(On button_click);
        >
        I want to pass more information related that event. & want to use that
        information in the event.

        Comment

        • raylopez99

          #5
          Re: How to pass more than two parameters in the event handler

          On Jul 16, 10:47 pm, Swappy <Swa...@discuss ions.microsoft. comwrote:

          Hey Swappy, I see three MVP's (Peter is also an MVP but he doesn't
          like to talk about it) have answered your question, so far be it for
          me to add my 0.02 dollars, but I would say that you should think twice
          about passing parameters through Events other than as provided by
          MSFT's wizards. Rule of thumb: don't mess with wizard generated
          code, ever.

          So I guess (if I read correctly) that I'm with Ciaran when he said:
          "there is a bit of debate about it though as some people suggest that
          it is not ideal to parse a reference to the sender and an EventArgs
          derived class
          to a set of event handlers that only need to know a boolean or and
          int. They believe that having lots of derivations of EventArgs
          introduces unnecessary code bloat and is a waste of time". Plus it's
          dangerous--you mess up the library you have to reinstall Visual Studio
          and that takes several hours.

          So, I would set up a user defined "Event" using a publisher-subscriber
          model--I posted recently in this forum an excellent console mode
          example of this--rather than using the Event handler stuff generated
          by the code wizard.

          RL

          ( a newbie with a good month's worth--but a solid month--of C#
          experience)

          Comment

          • Chris Dunaway

            #6
            Re: How to pass more than two parameters in the event handler

            On Jul 17, 8:05 am, raylopez99 <raylope...@yah oo.comwrote:
            On Jul 16, 10:47 pm, Swappy <Swa...@discuss ions.microsoft. comwrote:
            int. They believe that having lots of derivations of EventArgs
            introduces unnecessary code bloat and is a waste of time". Plus it's
            dangerous--you mess up the library you have to reinstall Visual Studio
            and that takes several hours.
            I'm curious, how could deriving your own class from EventArgs possibly
            "mess up the library" causing you to have to re-install VS?

            Chris

            Comment

            • =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=

              #7
              Re: How to pass more than two parameters in the event handler

              Comments:
              Hey Swappy, I see three MVP's (Peter is also an MVP but he doesn't
              like to talk about it) have answered your question, so far be it for
              me to add my 0.02 dollars,
              errrrm.
              I am NOT an MVP, and according to the Microsoft MVP awardies page, Peter
              Duniho is not a current MVP holder
              (https://mvp.support.microsoft.com/co...=peter+duniho).
              but I would say that you should think twice
              about passing parameters through Events other than as provided by
              MSFT's wizards. Rule of thumb: don't mess with wizard generated
              code, ever.
              Events in .NET are not normally generated by wizards. They are made with
              code like:
              public delegate void MyEventHandler( object sender, MyEventEventArg s e);
              public event MyEventHandler MyEvent;
              There is nothing autogenerated about the above code. It is just C# written
              by hand in this windows to create an delegate type, and an event.
              >
              So I guess (if I read correctly) that I'm with Ciaran when he said:
              "there is a bit of debate about it though as some people suggest that
              it is not ideal to parse a reference to the sender and an EventArgs
              derived class
              to a set of event handlers that only need to know a boolean or and
              int. They believe that having lots of derivations of EventArgs
              introduces unnecessary code bloat and is a waste of time".
              This was a point of view that I raised as Devils Advocate rather than
              voicing me own opinion. I tend to think of the phrase "Horses for courses",
              as in, nothing is applicable everywhere.
              >Plus it's
              dangerous--you mess up the library you have to reinstall Visual Studio
              and that takes several hours.
              Nothing you do in the code editor can mess this up unless you mean
              attempting to edit Visual Studio's resource files which is not at all
              something somebody should be doing. Creating events is a simple thing you do
              in code.

              So, I would set up a user defined "Event" using a publisher-subscriber
              model--I posted recently in this forum an excellent console mode
              example of this--rather than using the Event handler stuff generated
              by the code wizard.
              If this was your "UnAwareCla ss" based example then I would be concerned
              reading that by the comment chain posted in that thread, that you are still
              trying to push it as an "excellent" example. No offence to yourself, as I
              think your willingness to take part in the community is respectable, but if
              clever people like MVP's have so much to say on your example, maybe it isnt
              "excellent" .

              --
              Ciaran O''Donnell
              try{ Life(); } catch (TooDifficultException) { throw Toys(); }



              "raylopez99 " wrote:
              On Jul 16, 10:47 pm, Swappy <Swa...@discuss ions.microsoft. comwrote:
              >
              Hey Swappy, I see three MVP's (Peter is also an MVP but he doesn't
              like to talk about it) have answered your question, so far be it for
              me to add my 0.02 dollars, but I would say that you should think twice
              about passing parameters through Events other than as provided by
              MSFT's wizards. Rule of thumb: don't mess with wizard generated
              code, ever.
              >
              So I guess (if I read correctly) that I'm with Ciaran when he said:
              "there is a bit of debate about it though as some people suggest that
              it is not ideal to parse a reference to the sender and an EventArgs
              derived class
              to a set of event handlers that only need to know a boolean or and
              int. They believe that having lots of derivations of EventArgs
              introduces unnecessary code bloat and is a waste of time". Plus it's
              dangerous--you mess up the library you have to reinstall Visual Studio
              and that takes several hours.
              >
              So, I would set up a user defined "Event" using a publisher-subscriber
              model--I posted recently in this forum an excellent console mode
              example of this--rather than using the Event handler stuff generated
              by the code wizard.
              >
              RL
              >
              ( a newbie with a good month's worth--but a solid month--of C#
              experience)
              >

              Comment

              • raylopez99

                #8
                Re: How to pass more than two parameters in the event handler

                On Jul 17, 6:34 am, Ciaran O''Donnell
                <CiaranODonn... @discussions.mi crosoft.comwrot e:
                Comments:
                >
                Hey Swappy, I see three MVP's (Peter is also an MVP but he doesn't
                like to talk about it) have answered your question, so far be it for
                me to add my 0.02 dollars,
                >
                errrrm.
                I am NOT an MVP, and according to the Microsoft MVP awardies page, Peter
                Duniho is not a current MVP holder
                (https://mvp.support.microsoft.com/co...=peter+duniho).
                >
                I KNEW IT--PETER DUNIHO IS A FRAUD! HAHAHAHAHA THANKS CIARAN! HAHAHA
                A FRAUD. But he's more knowledgeable than most about C# so let's not
                rub it in. Love it. A fake. Like that guy on Wikipedia that claimed
                to be a college professor on some subject and was unemployed trailer
                park trash.
                but I would say that you should think twice
                about passing parameters through Events other than as provided by
                MSFT's wizards.  Rule of thumb:  don't mess with wizard generated
                code, ever.
                >
                Events in .NET are not normally generated by wizards. They are made with
                code like:
                public delegate void MyEventHandler( object sender, MyEventEventArg s e);
                public event MyEventHandler MyEvent;
                There is nothing autogenerated about the above code. It is just C# written
                by hand in this windows to create an delegate type, and an event.
                Sorry, I was not clear. I am saying the same thing, but saying that
                when you click on an event handler like "_OnClick" using the Wizards,
                the Events skeleton code is automatically generated by the Wizards,
                that's all. I completely agree with you.

                Wizards are great BTW-I'm using one now to create a MDI parent window
                and child windows under Forms--great stuff.
                >
                This was a point of view that I raised as Devils Advocate rather than
                voicing me own opinion. I tend to think of the phrase "Horses for courses",
                as in, nothing is applicable everywhere.
                So that's what that phrase means. Always wondered about that.
                >
                Plus it's
                dangerous--you mess up the library you have to reinstall Visual Studio
                and that takes several hours.
                >
                Nothing you do in the code editor can mess this up unless you mean
                attempting to edit Visual Studio's resource files which is not at all
                something somebody should be doing. Creating events is a simple thing youdo
                in code.
                Right. I meant if you mess with the resource files and/or .h headers
                in C++ that are in the library--don't go there.
                >
                So, I would set up a user defined "Event" using a publisher-subscriber
                model--I posted recently in this forum an excellent console mode
                example of this--rather than using the Event handler stuff generated
                by the code wizard.
                >
                If this was your "UnAwareCla ss" based example then I would be concerned
                reading that by the comment chain posted in that thread, that you are still
                trying to push it as an "excellent" example. No offence to yourself, as I
                think your willingness to take part in the community is respectable, but if
                clever people like MVP's have so much to say on your example, maybe it isnt
                "excellent" .
                But some of them are imposter MVP's. That don't count. Is Jon Skeet
                an MVP? Lemme check...he's been posting forever, I see his name from
                eight years ago on the net...well I'll be damned, not only is he an
                MVP, he's almost famous: "Jon is primarily a C# and Java developer in
                Reading, England. In 2008 Manning published his book, "C# in Depth."
                He has three young sons, and is currently working at Google".
                Google, man I remember when they went public, and how the smart money
                in Silcon Valley where I was at the time piled into those restricted
                shares--they knew a good thing and made off like bandits.

                RL

                [N00b C# MVP, with a solid 30 days coding experience]


                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: How to pass more than two parameters in the event handler

                  raylopez99 <raylopez99@yah oo.comwrote:
                  errrrm.
                  I am NOT an MVP, and according to the Microsoft MVP awardies page, Peter
                  Duniho is not a current MVP holder
                  (https://mvp.support.microsoft.com/co...=peter+duniho).
                  >
                  I KNEW IT--PETER DUNIHO IS A FRAUD! HAHAHAHAHA THANKS CIARAN! HAHAHA
                  A FRAUD. But he's more knowledgeable than most about C# so let's not
                  rub it in. Love it. A fake. Like that guy on Wikipedia that claimed
                  to be a college professor on some subject and was unemployed trailer
                  park trash.
                  Nope, that's just not a reliable way of deciding if someone's an MVP or
                  not. It just means that if Peter is an MVP (which I happen to know he
                  is, or at the very least *was*) then he's made his profile non-public.

                  It's easy to demonstrate that that's the case. You've just posted about
                  my public profile - go and see if you can find it again. I've just made
                  it non-public, and I think you'll find that if you look now, you won't
                  see it. The awardee directory does state fairly plainly: "The Microsoft
                  MVP Awardee directory contains a listing of all the MVPs that want to
                  share their information publicly."

                  Let me know when you've confirmed that, and I'll make my profile public
                  again...

                  It's a shame there isn't a "verify that XYZ is an MVP without showing a
                  profile" option, but that's just the way it goes.

                  My complete *guess* is that Peter won't go to the trouble of making his
                  profile public just to make a point, but I sincerely hope that if he
                  does so, you are ready to apologise for the insults quoted above.

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  Web site: http://www.pobox.com/~skeet
                  Blog: http://www.msmvps.com/jon_skeet
                  C# in Depth: http://csharpindepth.com

                  Comment

                  • Peter Duniho

                    #10
                    Re: How to pass more than two parameters in the event handler

                    On Thu, 17 Jul 2008 06:34:01 -0700, Ciaran O''Donnell
                    <CiaranODonnell @discussions.mi crosoft.comwrot e:
                    errrrm.
                    I am NOT an MVP, and according to the Microsoft MVP awardies page, Peter
                    Duniho is not a current MVP holder
                    (https://mvp.support.microsoft.com/co...=peter+duniho).
                    Just FYI: that directory includes only those MVPs who have specifically
                    enabled their profiles as being publicly visible, which I haven't (there's
                    not actually any useful information in my profile, and it's not like I
                    feel a need to _prove_ to anyone I'm an MVP, so I never bothered to enable
                    it).

                    You seem to have covered everything else. :)

                    Pete

                    Comment

                    • Peter Duniho

                      #11
                      Re: How to pass more than two parameters in the event handler

                      On Thu, 17 Jul 2008 01:38:01 -0700, Ciaran O''Donnell
                      <CiaranODonnell @discussions.mi crosoft.comwrot e:
                      Hey,
                      Jon Skeet is right with his suggestion about deriving from event args and
                      making you own delegate for the event.
                      Just to clarify: that's the normal approach if your class declares the
                      event and you have control over it. Otherwise, while you certainly could
                      proxy the event and copy the base EventArgs sub-class instance to a new
                      more-derived sub-class instance, IMHO it'd be better to just add a
                      parameter and pass the original EventArgs sub-class instance untouched.

                      The ambiguity is the reason I asked the OP what information he's trying to
                      pass and where it would come from. Basically, we don't know enough about
                      the OP's actual problem. If he's handling the Control.Click event,
                      creating a new EventArgs sub-class probably isn't the right way to go.

                      Pete

                      Comment

                      • =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=

                        #12
                        Re: How to pass more than two parameters in the event handler

                        Sorry about the mistake Peter. I'll add it to the list of many times I should
                        have read the small print to avoid an embarassing or costly mistake.

                        Sincerest Apologies

                        --
                        Ciaran O''Donnell
                        try{ Life(); } catch (TooDifficultException) { throw Toys(); }



                        "Peter Duniho" wrote:
                        On Thu, 17 Jul 2008 06:34:01 -0700, Ciaran O''Donnell
                        <CiaranODonnell @discussions.mi crosoft.comwrot e:
                        >
                        errrrm.
                        I am NOT an MVP, and according to the Microsoft MVP awardies page, Peter
                        Duniho is not a current MVP holder
                        (https://mvp.support.microsoft.com/co...=peter+duniho).
                        >
                        Just FYI: that directory includes only those MVPs who have specifically
                        enabled their profiles as being publicly visible, which I haven't (there's
                        not actually any useful information in my profile, and it's not like I
                        feel a need to _prove_ to anyone I'm an MVP, so I never bothered to enable
                        it).
                        >
                        You seem to have covered everything else. :)
                        >
                        Pete
                        >

                        Comment

                        • raylopez99

                          #13
                          Re: How to pass more than two parameters in the event handler

                          On Jul 17, 1:58 pm, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
                          My complete *guess* is that Peter won't go to the trouble of making his
                          profile public just to make a point, but I sincerely hope that if he
                          does so, you are ready to apologise for the insults quoted above.
                          >
                          I confirmed your public profile was set to private, so you can turn it
                          back on now.

                          Peter doesn't want my apology--he's a tough old coot and enjoys the
                          'intellectual repartee' of sparring with somebody who is at least his
                          equal in smarts, and in coding maybe will be some day!

                          RL

                          Comment

                          • Jon Skeet [C# MVP]

                            #14
                            Re: How to pass more than two parameters in the event handler

                            On Jul 18, 10:39 am, raylopez99 <raylope...@yah oo.comwrote:
                            My complete *guess* is that Peter won't go to the trouble of making his
                            profile public just to make a point, but I sincerely hope that if he
                            does so, you are ready to apologise for the insults quoted above.
                            >
                            I confirmed your public profile was set to private, so you can turn it
                            back on now.
                            Thank you.
                            Peter doesn't want my apology--he's a tough old coot and enjoys the
                            'intellectual repartee' of sparring with somebody who is at least his
                            equal in smarts, and in coding maybe will be some day!
                            Leaving your assertion of being "at least [Peter's] equal in smarts"
                            to one side, are you seriously happy to call someone a fraud and not
                            apologise for it when you know you're wrong? Part of having a
                            civilised debate is respecting those you're debating with - and part
                            of showing respect is apologising when you're wrong. That's true even
                            for technical inaccuracies, but I'd say it's doubly important when
                            you've made inaccurate personal assertions.

                            Jon

                            Comment

                            • Peter Duniho

                              #15
                              Re: How to pass more than two parameters in the event handler

                              On Fri, 18 Jul 2008 02:39:52 -0700, raylopez99 <raylopez99@yah oo.com>
                              wrote:
                              On Jul 17, 1:58 pm, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
                              >
                              >My complete *guess* is that Peter won't go to the trouble of making his
                              >profile public just to make a point, but I sincerely hope that if he
                              >does so, you are ready to apologise for the insults quoted above.
                              >>
                              >
                              I confirmed your public profile was set to private, so you can turn it
                              back on now.
                              >
                              Peter doesn't want my apology
                              Frankly Ray, it's not really a matter of what I want. It's true that I
                              have no real need of an apology from you, nor did I -- given your past
                              behavior -- actually expect one. But it's telling that given the
                              opportunity to offer one, you make a choice based on what you assert I
                              need or want, rather than what's the "right thing to do".

                              Given your participation in this newsgroup, my responses have been
                              targeted at just two goals: correcting misinformation, and trying to help
                              you improve your own communication skills (especially as they relate to
                              you apparently viewing yourself as a teacher). On the other hand, you
                              seem to revel in your own goal of prodding me with personal attacks.

                              I'm happy to ignore those attacks; after all, they say a lot more about
                              you than they do about me. But a person deserving of community respect
                              knows when they've crossed the line and _sincerely_ apologizes for it.

                              Now, I have my own personal thoughts on whether that really describes you
                              or not, but the fact is, if you're looking for credibility here, keeping
                              the bluster up even when you've made wildly false, personal accusations of
                              an individual is counter-productive to say the least. You really ought to
                              avoid the bluster altogether, but if you must engage in it, you ought to
                              at least equip yourself with the useful practice of apologizing when it's
                              gone wrong.

                              And again...I say this not for my own benefit, but for yours. I realize
                              it's difficult for you to see, but the two goals I described above really
                              do cover the extent of my intent. You have a determined interest in C#
                              programming and apparently the time and motivation to explore the language
                              and the framework. _If_ you could just take to heart some of the
                              criticisms and harness your powers for good, I think you could make some
                              real contributions here.

                              Pete

                              Comment

                              Working...