Hi everyone. I have a different type of question. Is there anyway to inherit eventargs into an interface.
thanks,
-zd
Printable View
Hi everyone. I have a different type of question. Is there anyway to inherit eventargs into an interface.
thanks,
-zd
I'm afraid that that doesn't really make any sense. EventArgs is a class. Interfaces can't inherit anything but other interfaces, and nothing can inherit an interface but another interface. Classes implement interfaces. What's more, there are certain places where you need an EventArgs object, so you must either use an instance of that class or a class that inherits it. Any other type will not be of any use.
Would you care to explain why this would be desirable.
I'm sorry for the poor explanation, I was in quite of a hurry to get home and was typing poorly. Let me try to explain a little more. I realize that interfaces cannot inherit classes. I also realize that Eventargs is a class. Let me try some code out.
I have created my eventhandler generic delegate passing my Employee Object as Eventargs
My employee object inherits from the EventArgs classCode:public static event EventHandler<Employee> myEvent
I then wire up my form to the eventCode:public class Employee : EventArgs
{
private string _myPerson;
public string myPerson
{
get { return _myPerson; }
set { _myPerson = value; }
}
}
I raise the eventCode:myEvent += new EventHandler<Employee>(Form1_myEvent);
Finally in my event we get this.Code:Employee ev = new Employee();
ev.myPerson = "test";
myEvent.Invoke(this, ev);
Everything works great doing that. However I would like to make my generic delegate look something like this.Code:void Form1_myEvent(object sender, Employee e)
{
string myString = e.myPerson;
}
Which if I am not mistaken would require my IEmployee interface to implement something form EventArgs. However, I don't think there is an IEventargs.Code:public static event EventHandler<IEmployee> myEvent;
I hope that helps.
Thanks JL for helping me clarify. I hope this helps. Thanks everyone for all their help.
Thanks in advance,
-zd
This is from the MSDN documentation for the EventHandler<TEventArgs>:What you're asking for is simply not possible because the generic type of the delegate MUST be derived form EventArgs.Quote:
The standard signature of an event handler delegate defines a method that does not return a value, whose first parameter is of type Object and refers to the instance that raises the event, and whose second parameter is derived from type EventArgs and holds the event data. If the event does not generate event data, the second parameter is simply an instance of EventArgs. Otherwise, the second parameter is a custom type derived from EventArgs and supplies any fields or properties needed to hold the event data.
Here's the answer to your problem: declare an abstract class that inherits EventArgs and specify that as the generic type of your delegate. That will satisfy the requirement that the type inherit EventArgs but it also satisfies the requirement that the type have members that must be implemented by types of the actual objects used, just like an interface.
Awesome jmcilhinney. i really appreciate your help. I have been wondering about this all weekend. I don't really need to use it. I was simply playing around and wondered if it was possible.
Thanks again jmcilhinney..
-zd