Thread: Events

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246

    Events

    I am a little confuzed with events.
    To use them first you define a delegate signature. Like:
    Code:
    public delegate void ButtonEventHandler();
    Then we make a delegate (event) in our class. Like:
    Code:
    class TestButton 
    {
        public event ButtonEventHandler OnClick;
    
        // A method that triggers the event:
        public void Click()
        {
            OnClick();
        }
    }

    The class has a method or methods that trigger the event. The event is just a delegate that Click() invokes.

    For handling events we subscribe handlers to events. That is exactly adding more methods to a delegate (Except when accessors are used). Like:
    Code:
     TestButton mb = new TestButton();
            
     // Specify the method that will be triggered by the OnClick event.
     mb.OnClick += new ButtonEventHandler(TestHandler);
    
     // Specify an additional anonymous method.
     mb.OnClick += delegate { System.Console.WriteLine("Hello, World!"); };
    And event will be triggered by calling Click() method. Like:
    Code:
     // Trigger the event
     mb.Click();
    Everything is going fine. By raising the event, Click() method invokes OnClick event and because it is like a delegate it calls subscribed methods that are actually event handlers.
    Now my question is: What is the difference between a delegate and an event? A delegate has the same functionality of an event. What will happen if we don't write event keyword in the class?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    I have also another question: Why MSDN writes this:
    Code:
    m_list.Changed += new ChangedEventHandler(ListChanged);
    When it can be written as this:
    Code:
    m_list.Changed += ListChanged;
    ???
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sending events with JScript?
    By IcyDeath in forum Networking/Device Communication
    Replies: 1
    Last Post: 12-28-2004, 06:15 PM
  2. Loading cursors on events
    By cfrost in forum C++ Programming
    Replies: 1
    Last Post: 05-13-2004, 09:07 PM
  3. seismic events program
    By santaclaus in forum C Programming
    Replies: 16
    Last Post: 11-23-2003, 03:23 PM
  4. ATL: fireing events from a Instanced object
    By CHECCO in forum C++ Programming
    Replies: 2
    Last Post: 09-03-2002, 07:05 AM
  5. Windows events
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 12-22-2001, 01:44 PM