PDA

Click to See Complete Forum and Search --> : [RESOLVED] Interface Inheritance


zdavis
Aug 31st, 2007, 04:40 PM
Hi everyone. I have a different type of question. Is there anyway to inherit eventargs into an interface.

thanks,

-zd

jmcilhinney
Aug 31st, 2007, 08:42 PM
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.

zdavis
Sep 1st, 2007, 11:37 PM
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

public static event EventHandler<Employee> myEvent

My employee object inherits from the EventArgs class

public class Employee : EventArgs
{
private string _myPerson;
public string myPerson
{
get { return _myPerson; }
set { _myPerson = value; }
}
}


I then wire up my form to the event

myEvent += new EventHandler<Employee>(Form1_myEvent);


I raise the event

Employee ev = new Employee();
ev.myPerson = "test";
myEvent.Invoke(this, ev);


Finally in my event we get this.

void Form1_myEvent(object sender, Employee e)
{
string myString = e.myPerson;
}


Everything works great doing that. However I would like to make my generic delegate look something like this.


public static event EventHandler<IEmployee> myEvent;


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.

I hope that helps.

Thanks JL for helping me clarify. I hope this helps. Thanks everyone for all their help.

Thanks in advance,

-zd

jmcilhinney
Sep 2nd, 2007, 02:19 AM
This is from the MSDN documentation for the EventHandler<TEventArgs>: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.What you're asking for is simply not possible because the generic type of the delegate MUST be derived form EventArgs.

jmcilhinney
Sep 2nd, 2007, 02:31 AM
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.

zdavis
Sep 2nd, 2007, 09:43 AM
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