Results 1 to 5 of 5

Thread: Events and Methods

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Events and Methods

    Hi

    I know what an event is and what a method is (duh!) but something just came into my mind - one of those stupid questions.

    I know that Events are pretty useful when raising an event from a thread that did not create the event or does not contain certain controls (UI controls for example)

    but arent events almost the same as methods?

    you have a method like so:

    Code:
    private void DoShowHello()
    {
       MessageBox.Show("Hi!");
    }
    You can also have an event that does the same thing - so really, what is the difference between calling a method or raising an event?

    To me, Events are probably more thread safe than calling a method from which the method does not exist, in other words - calling a method from another thread. Events are safer when talking about threading - am I correcy?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Events and Methods

    I think that you're confusing an event with an event handler. When you define an event in a class you are defining a type, and each event you raise is an instance of that type. An event handler, on the other hand, IS a method that gets called when a particular event is raised. Basically, events are raised from property setters. Note that basically every event corrresponds to a property changing. Normally you raise an event, i.e. create an instance of that event type, when the value of a property is changed. That event object is then available to every object that has a reference to the object that raised it, which can then choose to handle that event or not, where handling means calling a method designated as the event handler.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Events and Methods

    Maybe I'm giving you less credit than you deserve and you already know everything I posted before. Maybe you mean why can't an object just call a method directly to do something rather than raise an event and have its handler do something. The thing is, there may be an unlimited number of references to an object and the holders of every one of those references has a chance to handle any event raised by that object. In contrast, the object certainly wouldn't have a reference to all those other objects, so it couldn't directly call a method of each of them. It works very much like nature. If a mosquito bites you leg, does your leg tell your hand to scratch it? No, because your leg doesn't know about your hand. Your leg sends a message to your brain (the event object) and you brain then handles the event by calling the ScratchLeg method of your hand. There are certainly holes in this analogy but you get the idea. Raising an event allows you to indirectly affect objects that you know nothing about, but it is up to those objects what that affect will be.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Events and Methods

    excellent, just what I wanted to clear up thanks!

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Events and Methods

    Quote Originally Posted by jmcilhinney
    If a mosquito bites you leg, does your leg tell your hand to scratch it? No, because your leg doesn't know about your hand. Your leg sends a message to your brain (the event object) and you brain then handles the event by calling the ScratchLeg method of your hand. There are certainly holes in this analogy but you get the idea.
    I think that is a great analogy!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width