|
-
Aug 31st, 2007, 04:40 PM
#1
Thread Starter
Lively Member
[RESOLVED] Interface Inheritance
Hi everyone. I have a different type of question. Is there anyway to inherit eventargs into an interface.
thanks,
-zd
-
Aug 31st, 2007, 08:42 PM
#2
Re: Interface Inheritance
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.
-
Sep 1st, 2007, 11:37 PM
#3
Thread Starter
Lively Member
Re: Interface Inheritance
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
Code:
public static event EventHandler<Employee> myEvent
My employee object inherits from the EventArgs class
Code:
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
Code:
myEvent += new EventHandler<Employee>(Form1_myEvent);
I raise the event
Code:
Employee ev = new Employee();
ev.myPerson = "test";
myEvent.Invoke(this, ev);
Finally in my event we get this.
Code:
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.
Code:
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
-
Sep 2nd, 2007, 02:19 AM
#4
Re: Interface Inheritance
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.
Last edited by jmcilhinney; Sep 2nd, 2007 at 02:24 AM.
-
Sep 2nd, 2007, 02:31 AM
#5
Re: Interface Inheritance
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.
-
Sep 2nd, 2007, 09:43 AM
#6
Thread Starter
Lively Member
Re: Interface Inheritance
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|