Geeks With Blogs
Saqib Ullah BootStrapper Know How
According to C# specification interface contain a signature of events but a complete and brief implementation of events through interface is not available on the net.
 
First you identify the interface
 
    public interface IControl
    {
        // Raise this event.
        event EventHandler OnCommand;
    }
 
According to my need I create a class that inherit from System.EventArgs
 
    public class CommandArgs : EventArgs
    {
        public string CommandName;
    }
 
My OutCommand class is a implementation of IControl interface that contain an OnCommand event.
 
public class OutCommnad : IControl
    {
        public OutCommnad()
        {}
 
        // Create an event from interface event
        event EventHandler CommandEvent;
        event EventHandler IControl.OnCommand
        {
            add
            {
                if (CommandEvent != null)
                {
                    lock (CommandEvent)
                    {
                        CommandEvent += value;
                    }
                }
                else
                {
                    CommandEvent = new EventHandler(value);
                }
            }
            remove
            {
                if (CommandEvent != null)
                {
                    lock (CommandEvent)
                    {
                        CommandEvent -= value;
                    }
                }
            }
        }
 
        public void Command()
        {
            EventHandler handler = CommandEvent;
            if (handler != null)
            {
                //My custom class that passed as arguments.
                CommandArgs cmd = new CommandArgs();
                cmd.CommandName = "Test Application event.";
                handler(this, cmd);
            }
        }
    }
 
Calling of IControl.OnCommand event.
 
    OutCommnad outCommand = new OutCommnad();
    IControl con = (IControl)outCommand;
    con.OnCommand +=new EventHandler(con_OnCommand);
    outCommand.Command();

IControl.OnCommand event function.
 
        void con_OnCommand(object sender, CommandArgs e)
        {
            string str = e.CommandName;
            Console.WriteLine(str);
        }
 
Posted on Monday, April 7, 2008 5:49 PM OOPs , .Net | Back to top


Comments on this post: Implement events through interface in C#

# re: Implement events through interface in C#
Requesting Gravatar...
The event handler function con_OnCommand(), generates error when CommandArgs param in signature...instead it requires EventArgs param...why ?
Left by oliver on May 21, 2008 1:10 AM

# re: Implement events through interface in C#
Requesting Gravatar...
Can apply delegate with event instead of EventArgs ?
Left by Yen on Feb 15, 2009 3:30 PM

# re: Implement events through interface in C#
Requesting Gravatar...
This is a smart blog. I mean it. You have so much knowledge about this issue, and so much passion. You also know how to make people rally behind it, obviously from the responses. Youve got a design here thats not too flashy, but makes a statement as big as what youre saying. Great job, indeed.
Left by Chinese New Year 2012 on Jul 26, 2011 9:10 AM

# re: Implement events through interface in C#
Requesting Gravatar...
Hrmm that was weird, my comment got eaten. Anyway I wanted to say that it's nice to know that someone else also mentioned this as I had trouble finding the same info elsewhere. This was the first place that told me the answer. Thanks.
Left by Thailand Travel on Jul 26, 2011 9:14 AM

# re: Implement events through interface in C#
Requesting Gravatar...
that is really weird, I agree! what's in it?
Left by Forex Basics on Dec 05, 2011 1:57 AM

# re: Implement events through interface in C#
Requesting Gravatar...
Factoring around the bridesmaid wedding dresses can conserve enormous fees. The very best dresses want not usually be essentially the most high-priced dresses. It could be a practical selection to buy a low-cost dress since it is typical expertise that these dresses are in no way worn for any second time.
Left by knee length bridesmaid dresses on Dec 18, 2011 6:26 PM

# re: Implement events through interface in C#
Requesting Gravatar...
I dont think you absolutely need to use EventHandler delegate. It is also possible to create your own public delegate using whatever parameter list you want and then use that delegate in the interface.

For example:
public delegate void ReceiveDelegate(byte[] message);

Within the interface, you would just have:

event ReceiveDelegate onMessageReceive;


In fact doing that eliminates the need to implement your own version of EventArgs which reduces the amount of code needing written if you are only passing simple data types.

It will also help to ensure that the objects types that are being passed through the delegate are strong types and thus eliminating the need for type casting (boxing) of data types.
Left by JustSomeone on Feb 02, 2012 9:36 AM

# re: Implement events through interface in C#
Requesting Gravatar...
Great article, matev3
Left by I think we can work on Jun 01, 2012 12:32 AM

Your comment:
 (will show your gravatar)


Copyright © Saqib Ullah | Powered by: GeeksWithBlogs.net