views:

121

answers:

2

See Also:

understanding events and event handlers in C#

As a web dev, I don't typically have to get into building my own events for objects, since most are inherent in the page. However, now I'm into a pretty complex (web) project where I think I may have to use them. I just read a chapter out of Pro VB 2008 and the .NET 3.5 Platform regarding events, delegates and lambdas (ch. 11) and while I have a better idea of what's going on, I am still having trouble wrapping my mind around when exactly to use them and how to use them. I understand their implementation, but the example in the book is a bit contrived.

I was hoping someone with a bit more understanding on the subject of events could provide me with a really solid example.

For my real-world application, I'm trying to use an event that would let me know when an item (some class, named OrderItem) has been updated and perform actions based on whether or not it's been updated. I considered using a flag/boolean property, but I know this doesn't smell right, and it's high-time I learn about how to use events correctly.

Thanks a lot!

EDIT Ok, so I guess to clarify a bit, what is the point of calling an event when all it is doing is calling a method? Why not simply call the method? This isn't a duplicate either, as I'm talking conceptually and she wants to know about handlers specifically. Also, I want to know what the difference would be between using an event or a "flag" property. And what do people mean by "subscribe" to an event?

+4  A: 

Lets say you had an elevator system, and the part that moves up and down is called the ElevatorCar. When a person pushes a button to go to the 5th floor, it would make sense for the ElevatorController to call the ElevatorCar.RequestToFloor(5) method. Now when the car actually arrives at the 5th floor, it makes sense for it to raise an event like ArrivedAtFloor, passing 5 as the argument. The ElevatorController class would subscribe to the ArrivedAtFloor event of the ElevatorCar class.

This is all becauase the ElevatorCar class doesn't "know" anything about the ElevatorController class, but the ElevatorController class does know about the ElevatorCar class. Therefore the ElevatorCar class is given instructions by means of Methods, and it notifies the outside world of stuff that happens by means of events.

Does that make any sense?

EDIT:

Ok, first, go read the excellent answer listed in the See Also link (assume you've done that). An event is basically an object saying "if you want me to call you whenever X happens, then register your method here". Registering an event handler with the event is "subscribing". This allows encapsulation. So you can write, for instance, your ElevatorCar class once, and use it many times by many other classes.

As for the difference between using an event, or just calling a method, you have to ask yourself, should the ElevatorCar know about the ElevatorController class. If ElevatorController calls ElevatorCar.GoToFloor(x), how could the ElevatorCar class "call back" to ElevatorController without storing a reference to ElevatorController? That means the call has to become ElevatorCar.GoToFloor(int floor, ElevatorController ctrlr). Then the ElevatorCar class eventually has to call ctrlr.ArrivedAtFloor(floor). But you get into a lot of complexities... what if there's more than one car? You probably have to call ctrlr.ArrivedAtFloor(floor, this). Passing around references to yourself isn't optimal.

As for just setting a property, how does the other class know to come and read the property? The only way is to poll the property over and over to check for it to change. You can solve this, of course, by implementing INotifyPropertyChanged, but then again you're back to events!

Scott Whitlock
i updated my question
Jason
how is this any different than just having a class call a method instead of going through the event wireup?
Jason
+2  A: 

Events are a specific case of the Inversion of Control (IoC) pattern. The traditional control flow, the caller invokes a method on the callee (like you are suggesting). With IoC (and thus events), we change around the application control and instead of tell the callee what to do, we tell the callee to notify us when something we are interested in happens.

Consider the case of a Timer. I want to be notified every 5 seconds (say). I don't want to constantly poll the timer ("is it time yet? is it time yet? is it time yet?"). Instead, I want to invert the flow control of my application. I want to tell the timer: "When it's time, please tell me by calling this method." That way, control is "inverted", the callee is invoking a method on the caller!

Consider the following code ...

using System;
using System.Timers;

namespace TestTimer
{
    class Program
    {
        static void Main(string[] args)
        {
            // create my timer.
            var t = new System.Timers.Timer(1000);

            // register for notification
            // tell the timer, "when it's time, call TimerGoesOff method"
            t.Elapsed += new ElapsedEventHandler( TimerGoesOff );

            // start the timer
            t.Start();

            // wait
            Console.ReadLine();
        }

        // this gets called when the timer goes off
        public static void TimerGoesOff(object source, ElapsedEventArgs e)
        {
            Console.WriteLine("The Time is " + e.SignalTime);
        }
    }
}

Rather than call a method on the Timer to ask when it will go off again (as you suggest), I tell it, "when you go off, call the TimerGoesOff" method. Now, instead of just waiting for the Timer to go off, I could do some work. Consider this code ...

using System;
using System.Threading;
using System.Timers;

namespace TestTimer
{
    class Program
    {
        static void Main(string[] args)
        {
            // create my timer.
            var t = new System.Timers.Timer(1000);

            // register for notification
            t.Elapsed += new ElapsedEventHandler( TimerGoesOff );

            // start the timer
            t.Start();

            // do some work while timer is going
            new Thread(() => DoWork()).Start();

            Console.ReadLine();
        }

        // this gets called when the timer goes off
        public static void TimerGoesOff(object source, ElapsedEventArgs e)
        {
            Console.WriteLine("The Time is " + e.SignalTime);
        }

        public static void DoWork()
        {
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine( "Working ..." );
                Thread.Sleep( 1000 );
            }
        }
    }
}

Now, I get output something like ...

Working ...
The Time is 8/25/2009 1:05:59 PM
Working ...
Working ...
The Time is 8/25/2009 1:06:00 PM
Working ...
The Time is 8/25/2009 1:06:01 PM
Working ...
The Time is 8/25/2009 1:06:02 PM
The Time is 8/25/2009 1:06:03 PM

I have used the Timer.Elapsed Event to change the control flow of my application. I can go off and do work while "waiting" for the timer event to pulse. This is made possible by IoC and Events.

This is particularly visible (hehe) in User Interfaces. I don't want to keep asking "has the user done anything yet? has the user done anything yet?" (that's the way it used to be done way back when). Instead, I tell Controls for example, "when the user clicks you, let me know". That way, I can go off and do other great stuff and still be responsive to the user.

JP Alioto
this is an interesting and useful answer... although in VB lambdas must return values :\
Jason