Return to Snippet

Revision: 10069
at January 20, 2010 13:06 by pckujawa


Updated Code
using System;

namespace testConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Test myTest = new Test();
            // Add event handler to the event
            myTest.TestEvent += new EventHandler(myTest_TestEvent);
            myTest.fireEvent();
            Console.ReadLine(); // Keep console window open
        }

        static void myTest_TestEvent(object sender, EventArgs e)
        {
            if (e is TestEventArgs)
            {
                TestEventArgs data = e as TestEventArgs;
                Console.WriteLine(data.Message);
            }
        }
    }

    class Test
    {
        public event EventHandler TestEvent;
        public Test()
        {
        }
        public void fireEvent()
        {
            // Always check if the EventHandler is null (no subscribers)
            var handler = TestEvent;
            if (handler != null)
            {
                handler(this, new TestEventArgs("Event Fired"));
            }
        }
    }

    class TestEventArgs : EventArgs
    {
        public string Message { get; private set; }
        public TestEventArgs(string message)
        {
            this.Message = message;
        }
    }
}

Revision: 10068
at December 5, 2008 14:12 by pckujawa


Initial Code
using System;

namespace testConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Test myTest = new Test();
            // Add event handler to the event
            myTest.TestEvent += new EventHandler(myTest_TestEvent);
            myTest.fireEvent();
            Console.ReadLine(); // Keep console window open
        }

        static void myTest_TestEvent(object sender, EventArgs e)
        {
            if (e is TestEventArgs)
            {
                TestEventArgs data = e as TestEventArgs;
                Console.WriteLine(data.Message);
            }
        }
    }

    class Test
    {
        public event EventHandler TestEvent;
        public Test()
        {
        }
        public void fireEvent()
        {
            // Always check if the EventHandler is null (no subscribers)
            if (TestEvent != null)
            {
                TestEvent(this, new TestEventArgs("Event Fired"));
            }
        }
    }

    class TestEventArgs : EventArgs
    {
        public string Message { get; private set; }
        public TestEventArgs(string message)
        {
            this.Message = message;
        }
    }
}

Initial URL

                                

Initial Description
See [Raise and Handle Events Generically](http://snipplr.com/view/15033/raise-and-handle-events-generically/) for a better way to use events (generically) and links to best practices in .NET events.

This example should be used as a Console program and will simply write a line of text to the console. It shows how to create an event handler in a class, have that event handler fire an event with custom information, and how to subscribe to that event and use its data. I hope it is straightforward enough.

Initial Title
Example of subscribing to and firing events

Initial Tags
event

Initial Language
C#