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

    Sander RosselS Offline
    Sander RosselS Offline
    Sander Rossel
    wrote on last edited by
    #6

    I make my applications so that they have only one Event (the ApplicationStartUp Event). In this Event I code ALL logic etc. I do not use Try Catches so this is bound to go wrong somewhere. The application will close itself. When a user calls I say I even automated the UI and that everything actually went as planned (RTFM it's all in there!). My customers are happy and I am happy. The only thing that is not happy is my software, because it cannot feel emotions :D

    It's an OO world.

    W 1 Reply Last reply
    0
    • Sander RosselS Sander Rossel

      I make my applications so that they have only one Event (the ApplicationStartUp Event). In this Event I code ALL logic etc. I do not use Try Catches so this is bound to go wrong somewhere. The application will close itself. When a user calls I say I even automated the UI and that everything actually went as planned (RTFM it's all in there!). My customers are happy and I am happy. The only thing that is not happy is my software, because it cannot feel emotions :D

      It's an OO world.

      W Offline
      W Offline
      wizardzz
      wrote on last edited by
      #7

      Naerling wrote:

      The only thing that is not happy is my software, because it cannot feel emotions

      I wish I could upset or frustrate my software sometimes, then it might have some empathy and stop doing the same to me!

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

      Sander RosselS K 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

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

        Depends. I use anonymous event handlers for events often, b/c it's useful for them to run in the scope of the calling method, and often useful to define the behaviour right there, from a readability stand point. But, for form events, I often factor them out in to a method, again for readability and reuse.

        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
        • W wizardzz

          Naerling wrote:

          The only thing that is not happy is my software, because it cannot feel emotions

          I wish I could upset or frustrate my software sometimes, then it might have some empathy and stop doing the same to me!

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

          Sander RosselS Offline
          Sander RosselS Offline
          Sander Rossel
          wrote on last edited by
          #9

          :laugh:

          It's an OO world.

          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

            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
                                          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