Results 1 to 4 of 4

Thread: [RESOLVED] [2.0] Delegates and Events

  1. #1

    Thread Starter
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Resolved [RESOLVED] [2.0] Delegates and Events

    OK, having mucked around with VB.NET for a while, I have decided to have a muck around with C# (2005 Express). So far not too bad, getting used to the syntax etc, but I am currently banging my head against the brick wall called 'Delegates and Events'.

    Have read articles on MSDN and elsewhere my head is spinning a bit.

    As far as I can work out, a delegate is some sort of class or container that can contain references to methods. (Yes?)

    So, when you 'wire-up' an event, you pass in the method you want to call when the event fires like so:
    Code:
    obj.MyEvent += new MyEventHandler(MyMethod);  // where MyEventHandler is the delegate
    Is the '+=' basically adding MyMethod to a list of methods that is automatically called when the event fires?

    Am I close, or miles away with my take on this?

    Thanks for helping out a C# newbie!
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2.0] Delegates and Events

    It sounds to me like you got it.

    Are you familiar with doing this in VB?
    vb.net Code:
    1. AddHandler Button1.Click, AddressOf DoSomething

    If so then you already know about this:
    c# Code:
    1. Button1.Click += new EventHandler(DoSomething);

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

    Re: [2.0] Delegates and Events

    A delegate is basically as you described: an object that contains a reference to a method. When you invoke the delegate you are executing the method that it references. Normally you would need a reference to the object of which the method was a member in order to execute it. The delegate allows you to pass a method reference around as an object without having to pass the object of which the method is a member. This will become clearer with an example below.

    An event is basically a collection of EventHandler delegates. By using the "+=" operator you are adding a delegate to the collection. When an object raises an event what it is doing is basically looping through the collection of delegates for that event and invoking each one. In that way each method that is registered to handle that event will be executed when the event is raised.

    Now, consider the situation where you have a form and a button. You declare a method on the form and register it as an event handler for the button's Click event. When the user clicks the button it raises its Click event, thus invoking the EventHandler delegate and executing the method. The button has thus executed a method that is a member of a form that it has no reference to.

    All this happens in VB too but the complexity is hidden behind the Handles, AddHandler and RaiseEvent statements.
    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
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: [2.0] Delegates and Events

    Quote Originally Posted by nmadd
    Are you familiar with doing this in VB?

    Code:
    AddHandler Button1.Click, AddressOf DoSomething
    Yes, I am familiar with AddHandler and AddressOf in VB, but I guess I never thought too deeply about it because I didn't have too. VB hides a lot of stuff away from you, and 'it just works'. When you lay out the two code snippets for VB and C#, it is obvous how similar they are.



    Quote Originally Posted by jmcilhinney
    An event is basically a collection of EventHandler delegates. By using the "+=" operator you are adding a delegate to the collection.
    Thanks for this explanation, it has helped me a lot by making me think of an event as a collection, rather than just one thing.

    When I come to think about it, it does make more sense. A class can raise an event, and a number of methods can be 'wired' to it, so it stands to reason these methods must be stored or listed somewhere, ready to be called when the event fires.


    Thanks, I think there is a bit of light at the end of the tunnel.
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


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