Saqib Ullah

BootStrapper Know How

  Home  |   Contact  |   Syndication    |   Login
  109 Posts | 1 Stories | 819 Comments | 15 Trackbacks

News



Article Categories

Archives

Post Categories

Blogging websites

Favourite Blogs

Private Links

Sites

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);
        }
 
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted on Monday, April 07, 2008 5:49 PM

Feedback

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

# re: Implement events through interface in C# 2/15/2009 3:30 PM Yen
Can apply delegate with event instead of EventArgs ?

# re: Implement events through interface in C# 7/26/2011 9:10 AM Chinese New Year 2012
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.

# re: Implement events through interface in C# 7/26/2011 9:14 AM Thailand Travel
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.

# re: Implement events through interface in C# 12/5/2011 1:57 AM Forex Basics
that is really weird, I agree! what's in it?

# re: Implement events through interface in C# 12/18/2011 6:26 PM knee length bridesmaid dresses
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.

# re: Implement events through interface in C# 2/2/2012 9:36 AM JustSomeone
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.


Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: