Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Calling functions from Events

Calling functions from Events

Scheduled Pinned Locked Moved The Lounge
csharpquestion
56 Posts 32 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W wizardzz

    Not a question, more of a survey. How many people here always separate code from event handlers, especially Form events. I always do it, but it is driving me mad looking at and updating code that doesn't do it, written by a senior engineer. All my event handlers call functions, nothing more.

    Craigslist Troll: litaly@comcast.net "I have a theory that the truth is never told during the nine-to-five hours. " — Hunter S. Thompson

    N Offline
    N Offline
    Nish Nishant
    wrote on last edited by
    #10

    It's all relative really. You fume when you see event handlers with many lines of code. An MVC purist would cringe if he sees you calling a class method from the handler, since he would expect the call to be routed somehow to a controller or view-model. An MVVM purist would cringe at the event handler - since he'd want to see an event bound to a VM command in XAML. So don't judge the people who wrote that code because others will judge you just as sharply and just as unreasonably.

    Regards, Nish


    Are you addicted to CP? If so, check this out: The Code Project Forum Analyzer : Find out how much of a life you don't have! My technology blog: voidnish.wordpress.com

    1 Reply Last reply
    0
    • W wizardzz

      Not a question, more of a survey. How many people here always separate code from event handlers, especially Form events. I always do it, but it is driving me mad looking at and updating code that doesn't do it, written by a senior engineer. All my event handlers call functions, nothing more.

      Craigslist Troll: litaly@comcast.net "I have a theory that the truth is never told during the nine-to-five hours. " — Hunter S. Thompson

      R Offline
      R Offline
      realJSOP
      wrote on last edited by
      #11

      I only do it if the same code can be called from non-event code.

      ".45 ACP - because shooting twice is just silly" - JSOP, 2010
      -----
      You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
      -----
      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

      A 1 Reply Last reply
      0
      • W wizardzz

        Not a question, more of a survey. How many people here always separate code from event handlers, especially Form events. I always do it, but it is driving me mad looking at and updating code that doesn't do it, written by a senior engineer. All my event handlers call functions, nothing more.

        Craigslist Troll: litaly@comcast.net "I have a theory that the truth is never told during the nine-to-five hours. " — Hunter S. Thompson

        R Offline
        R Offline
        Rama Krishna Vavilala
        wrote on last edited by
        #12

        Overall it is a good idea to call functions, that way you can change the logic pretty easily and also make the code readable (assuming functions have good names). It is pretty easy these days with refactoring support to extract the code into separate methods. For instance,

        void serverName_changed(object sender, EventArgs e)
        {
        ResetUserNameAndPassword();
        }

        is more readable then

        void serverName_changed(object sender, EventArgs e)
        {
        Username.Text = "";
        Password.Text = ""
        // Some other code usually gets very long
        //

        }

        C S 2 Replies Last reply
        0
        • W wizardzz

          Not a question, more of a survey. How many people here always separate code from event handlers, especially Form events. I always do it, but it is driving me mad looking at and updating code that doesn't do it, written by a senior engineer. All my event handlers call functions, nothing more.

          Craigslist Troll: litaly@comcast.net "I have a theory that the truth is never told during the nine-to-five hours. " — Hunter S. Thompson

          Steve EcholsS Offline
          Steve EcholsS Offline
          Steve Echols
          wrote on last edited by
          #13

          I'll second JSOP's method. Why cause function call overhead if you don't need to? Probably a moot point, especially if it's a button click, but something like a resize or repaint event could be called thousands of times.


          - S 50 cups of coffee and you know it's on! Code, follow, or get out of the way.

          • S
            50 cups of coffee and you know it's on!
            Code, follow, or get out of the way.
          D P 2 Replies Last reply
          0
          • R Rama Krishna Vavilala

            Overall it is a good idea to call functions, that way you can change the logic pretty easily and also make the code readable (assuming functions have good names). It is pretty easy these days with refactoring support to extract the code into separate methods. For instance,

            void serverName_changed(object sender, EventArgs e)
            {
            ResetUserNameAndPassword();
            }

            is more readable then

            void serverName_changed(object sender, EventArgs e)
            {
            Username.Text = "";
            Password.Text = ""
            // Some other code usually gets very long
            //

            }

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #14

            The worst is when you see code that calls an event handler, passing EventArgs.Empty, in order to trigger the same behaviour.

            Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

            M P 2 Replies Last reply
            0
            • W wizardzz

              Not a question, more of a survey. How many people here always separate code from event handlers, especially Form events. I always do it, but it is driving me mad looking at and updating code that doesn't do it, written by a senior engineer. All my event handlers call functions, nothing more.

              Craigslist Troll: litaly@comcast.net "I have a theory that the truth is never told during the nine-to-five hours. " — Hunter S. Thompson

              J Offline
              J Offline
              Joe Woodbury
              wrote on last edited by
              #15

              I'm with JSOP.

              1 Reply Last reply
              0
              • C Christian Graus

                The worst is when you see code that calls an event handler, passing EventArgs.Empty, in order to trigger the same behaviour.

                Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                M Offline
                M Offline
                Mycroft Holmes
                wrote on last edited by
                #16

                Interesting, I did not even realise there was an EventArgs.Empty, see the things you learn at CP

                Never underestimate the power of human stupidity RAH

                C 1 Reply Last reply
                0
                • M Mycroft Holmes

                  Interesting, I did not even realise there was an EventArgs.Empty, see the things you learn at CP

                  Never underestimate the power of human stupidity RAH

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #17

                  It's amusingly close to useles, b/c there's no MouseEventArgs.Empty, etc, which means you can only use it where an event takes the base class, not one of the derived ones.

                  Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                  1 Reply Last reply
                  0
                  • Steve EcholsS Steve Echols

                    I'll second JSOP's method. Why cause function call overhead if you don't need to? Probably a moot point, especially if it's a button click, but something like a resize or repaint event could be called thousands of times.


                    - S 50 cups of coffee and you know it's on! Code, follow, or get out of the way.

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #18

                    Yeah, but I'm usually painting custom controls in layers, so it makes more sense to put each layer in its own method. Then you can turn those layers on and off at will and replace them quite easily. Chances are you're not going to paint anything at 30 frames a second so it doesn't matter that much. If you wanted that kind of performance, I'd look to something other than GDI/GDI+.

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak

                    1 Reply Last reply
                    0
                    • I Ian Shlasko

                      Depends on how much they're doing. If it's just a couple lines, and won't be used anywhere else, I stick it in the event handler. If it's more substantial than that, or looks like it belongs in the model layer, or will be called from elsewhere, then I move it to a separate function.

                      Proud to have finally moved to the A-Ark. Which one are you in?
                      Author of the Guardians Saga (Sci-Fi/Fantasy novels)

                      A Offline
                      A Offline
                      Albert Holguin
                      wrote on last edited by
                      #19

                      i agree... couple of lines, not worth the effort... a lot of lines, probably will make the code a lot cleaner

                      1 Reply Last reply
                      0
                      • R realJSOP

                        I only do it if the same code can be called from non-event code.

                        ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                        -----
                        You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                        -----
                        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

                        A Offline
                        A Offline
                        Albert Holguin
                        wrote on last edited by
                        #20

                        good point

                        1 Reply Last reply
                        0
                        • W wizardzz

                          Not a question, more of a survey. How many people here always separate code from event handlers, especially Form events. I always do it, but it is driving me mad looking at and updating code that doesn't do it, written by a senior engineer. All my event handlers call functions, nothing more.

                          Craigslist Troll: litaly@comcast.net "I have a theory that the truth is never told during the nine-to-five hours. " — Hunter S. Thompson

                          P Offline
                          P Offline
                          PIEBALDconsult
                          wrote on last edited by
                          #21

                          Yeah, only if large, have common code, or need to be on a separate thread, otherwise I don't sweat it.

                          wizardzz wrote:

                          All my event handlers call functions, nothing more.

                          That sounds needless, why not simply rename the handlers to whatever the methods are named?

                          1 Reply Last reply
                          0
                          • C Christian Graus

                            The worst is when you see code that calls an event handler, passing EventArgs.Empty, in order to trigger the same behaviour.

                            Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                            P Offline
                            P Offline
                            PIEBALDconsult
                            wrote on last edited by
                            #22

                            I pass null instead.

                            C 1 Reply Last reply
                            0
                            • W wizardzz

                              Not a question, more of a survey. How many people here always separate code from event handlers, especially Form events. I always do it, but it is driving me mad looking at and updating code that doesn't do it, written by a senior engineer. All my event handlers call functions, nothing more.

                              Craigslist Troll: litaly@comcast.net "I have a theory that the truth is never told during the nine-to-five hours. " — Hunter S. Thompson

                              A Offline
                              A Offline
                              Andy Brummer
                              wrote on last edited by
                              #23

                              There isn't any one way to split this stuff up and everyone has their own way of understanding it. My rule of thumb is that every chunk of code (function, event handler, whatever) is relatively simple to understand on it's own. Code with too many function calls can be harder to understand then 3,000 line nested if and switch statement monstrosities. If you have to go poking around in 5 or 6 files just to understand what one method does, that's a complete mess. As long as you stay away from the super long or super short clever function extreme, you should be good. As far as your team is concerned, the most important thing is that you guys roughly agree on the basics.

                              Curvature of the Mind now with 3D

                              1 Reply Last reply
                              0
                              • W wizardzz

                                Not a question, more of a survey. How many people here always separate code from event handlers, especially Form events. I always do it, but it is driving me mad looking at and updating code that doesn't do it, written by a senior engineer. All my event handlers call functions, nothing more.

                                Craigslist Troll: litaly@comcast.net "I have a theory that the truth is never told during the nine-to-five hours. " — Hunter S. Thompson

                                R Offline
                                R Offline
                                Ravi Bhavnani
                                wrote on last edited by
                                #24

                                wizardzz wrote:

                                it is driving me mad looking at and updating code that doesn't do it

                                Perhaps a refactor is in order?  The senior engineer would learn from it and it would reduce your angst.  Double win.

                                wizardzz wrote:

                                All my event handlers call functions, nothing more.

                                Excellent.  My WinForms event handlers call methods in the Form's view model. /ravi

                                My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                                1 Reply Last reply
                                0
                                • W wizardzz

                                  Not a question, more of a survey. How many people here always separate code from event handlers, especially Form events. I always do it, but it is driving me mad looking at and updating code that doesn't do it, written by a senior engineer. All my event handlers call functions, nothing more.

                                  Craigslist Troll: litaly@comcast.net "I have a theory that the truth is never told during the nine-to-five hours. " — Hunter S. Thompson

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #25

                                  Always always always... Well - sometimes while developing and testing I don't immediately - but I always try and move code out of the handler because otherwise some goose will come along and call the handler in order to execute the code

                                  MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

                                  1 Reply Last reply
                                  0
                                  • W wizardzz

                                    Not a question, more of a survey. How many people here always separate code from event handlers, especially Form events. I always do it, but it is driving me mad looking at and updating code that doesn't do it, written by a senior engineer. All my event handlers call functions, nothing more.

                                    Craigslist Troll: litaly@comcast.net "I have a theory that the truth is never told during the nine-to-five hours. " — Hunter S. Thompson

                                    L Offline
                                    L Offline
                                    Lost User
                                    wrote on last edited by
                                    #26

                                    I use the MVP pattern and forms or user controls as views. The views are held 'dumb', meaning that they barely 'know' how to show the data sent by the presenter. Event handlers don't do very much more than pass input on to the presenter, but that's just another variant of wrapping up all logic in methods.

                                    "I just exchanged opinions with my boss. I went in with mine and came out with his." - me, 2011 ---
                                    I am endeavoring, Madam, to construct a mnemonic memory circuit using stone knives and bearskins - Mr. Spock 1935 and me 2011

                                    1 Reply Last reply
                                    0
                                    • P PIEBALDconsult

                                      I pass null instead.

                                      C Offline
                                      C Offline
                                      Christian Graus
                                      wrote on last edited by
                                      #27

                                      I doubt you can. You can pass null for the sender, but not the eventargs, I don't think.

                                      Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                                      P D 2 Replies Last reply
                                      0
                                      • W wizardzz

                                        Not a question, more of a survey. How many people here always separate code from event handlers, especially Form events. I always do it, but it is driving me mad looking at and updating code that doesn't do it, written by a senior engineer. All my event handlers call functions, nothing more.

                                        Craigslist Troll: litaly@comcast.net "I have a theory that the truth is never told during the nine-to-five hours. " — Hunter S. Thompson

                                        J Offline
                                        J Offline
                                        John Stewien
                                        wrote on last edited by
                                        #28

                                        I don't even have event handlers, I inline my event handling code in an anonymous delegate ;P

                                        1 Reply Last reply
                                        0
                                        • W wizardzz

                                          Not a question, more of a survey. How many people here always separate code from event handlers, especially Form events. I always do it, but it is driving me mad looking at and updating code that doesn't do it, written by a senior engineer. All my event handlers call functions, nothing more.

                                          Craigslist Troll: litaly@comcast.net "I have a theory that the truth is never told during the nine-to-five hours. " — Hunter S. Thompson

                                          P Offline
                                          P Offline
                                          peterchen
                                          wrote on last edited by
                                          #29

                                          Mostly the same, here, it may involve a condition check such as (!m_bLalalalaIcannotHearYou) or determining parameters for the call.


                                          What do you think of this?

                                          this.listBox1.DoubleClick +=(x, y) => m_ipl.BeginEdit();

                                          Just discovered that code (written a few month agon, rotting away...)

                                          FILETIME to time_t
                                          | FoldWithUs! | sighist | WhoIncludes - Analyzing C++ include file hierarchy

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups