tags:

views:

75

answers:

3

I have created a very simple dummy program to understand Delegates and events. In my below program I am simple calling a method.When I call a method, five methods are automatically calll with the help of Delegates and events.

Kindly take a look on my program and do let me know where I am wrong or right as I am using first time Delegates and events.

using System;

namespace ConsoleApplication1
{


public delegate void MyFirstDelegate();
class Test
{
    public event MyFirstDelegate myFirstDelegate;

    public void Call()
    {
        Console.WriteLine("Welcome in Delegate world..");
        if (myFirstDelegate != null)
        {
            myFirstDelegate();
        }
    }

}
class AttachedFunction
{
    public void firstAttachMethod()
    {
        Console.WriteLine("ONE...");
    }
    public void SecondAttachMethod()
    {
        Console.WriteLine("TWO...");
    }
    public void thirdAttachMethod()
    {
        Console.WriteLine("THREE...");
    }
    public void fourthAttachMethod()
    {
        Console.WriteLine("FOUR...");
    }
    public void fifthAttachMethod()
    {
        Console.WriteLine("FIVE...");
    }

}
class MyMain
{
    public static void Main()
    {
        Test test = new Test();
        AttachedFunction attachedFunction = new AttachedFunction();
        test.myFirstDelegate += new MyFirstDelegate(attachedFunction.firstAttachMethod);
        test.myFirstDelegate += new MyFirstDelegate(attachedFunction.SecondAttachMethod);
        test.myFirstDelegate += new MyFirstDelegate(attachedFunction.thirdAttachMethod);
        test.myFirstDelegate += new MyFirstDelegate(attachedFunction.fourthAttachMethod);
        test.myFirstDelegate += new MyFirstDelegate(attachedFunction.fifthAttachMethod);

        test.Call();
        Console.ReadLine();
    }
}
}

Thanks in advance. Mohit Kumar

A: 

Try putting the line

public delegate void MyFirstDelegate();

inside the Test class.

Also, use the Invoke function on the event instead, i.e.

myFirstDelegate.Invoke();
macfanpro
Thanks Macfanpro.
Mohit Kumar
+1  A: 

Events are implemented using Delegates. That said by convention events take the form of:

 void EventHandler(Object sender, EventArgs args);

EventHandler is actually a delegate defined in .Net. EventArgs is a class in .Net that acts as a placeholder to pass additional information. If you have additional information you would create a class that derived from EventArgs and contained properties for the additional data; therefore you would create your own delegate like so:

void MyEventHandler(Object sender, MyEventArgs args);

Microsoft has a tutorial on events here and also describes defining and raising events here

Steve Ellinger
Thanks Steve..Nice Link..:)
Mohit Kumar
+1  A: 

This is a common pattern with dealing with events:

// define the delegate
public delegate void CustomEventHandler(object sender, CustomEventArgs e);

// define the event args
public class CustomEventArgs : EventArgs
{
     public int SomeValue { get; set; }

     public CustomEventArgs( int someValue )
     {
         this.SomeValue = someValue;
     }
}

// Define the class that is raising events
public class SomeClass
{
    // define the event
    public event CustomEventHandler CustomEvent;

    // method that raises the event - derived classes can override this
    protected virtual void OnCustomEvent(CustomEventArgs e)
    {
        // do some stuff
        // ...

        // fire the event
        if( CustomEvent != null )
            CustomEvent(this, e);
    }

    public void SimulateEvent(int someValue)
    {
        // raise the event
        CustomEventArgs args = new CustomEventArgs(someValue);

        OnCustomEvent(args);
    }
}

public class Main
{
    public static void Main()
    {
        SomeClass c = new SomeClass(); 

        c.CustomEvent += SomeMethod;
        c.SimulateEvent(10); // will cause event
    }

    public static void SomeMethod(object sender, CustomEventArgs e)
    {
         Console.WriteLine(e.SomeValue);
    }
}
Dismissile
Thanks Dismissile
Mohit Kumar