Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public interface MyEvent{
    event EventHandler myNewEvent;
}


I don't know how to init this event in same class and call it in some other class. Any help pls. I like something like:

C#
public class MyClass : MonoBehaviour, MyEvent {
     public delegate void EventHandler();
     public event EventHandler myNewEvent; // But here I have error

}


How to do this?
Posted
Updated 11-Nov-14 21:38pm
v2
Comments
Sergey Alexandrovich Kryukov 11-Nov-14 17:25pm    
"Handler", not "Hendler".
—SA

This is quite possible, with implicit interface implementation. Your problem is unrelated to the peculiarities of event members in the interfaces. Your only problem is the definition of EventHandler; you can simply use available System.EventHandler.

For example:
C#
public class MonoBehaviour { /* ... */ }

public interface MyEvent{
    event System.EventHandler myNewEvent;
}

public class MyClass : MonoBehaviour, MyEvent {
    // public delegate EventHendler; // remove it, it is already defined
    public event System.EventHandler myNewEvent; // no problem here
}
If and when you need your own delegate type (unlikely; usually it's enough to only define the class derived from System.EventArgs and use the event handler type instantiated from the generic type System.EventHander<your_event_args_type_here>), make sure the definition is accessible where you use it.

—SA
 
Share this answer
 
v3
I assume that if you wish to implement a custom event based on an Interface, you'll probably want to have a custom EventArgs class for passing custom data:
C#
public interface MyEvent
{
    // EventHandler<t> available since .NET 2.0
    event EventHandler<customeventargs> myNewEvent;
}

public class CustomEventArgs : EventArgs
{
    public int Number { set; get; }
    public string Name { set; get; }

    public CustomEventArgs(int number, string name)
    {
        Number = number;
        Name = Name;
    }
}

public class ImplementsMyEventInterface : MyEvent
{
    public event EventHandler<customeventargs> myNewEvent;
    
    protected virtual void OnMyNewEvent(CustomEventArgs args)
    {
        EventHandler<customeventargs> handler = myNewEvent;
        if (handler != null) handler(this, args);
    }
}

// example of use:

private void SomeForm_Load(object sender, EventArgs e)
{
    var instanceOfMyEvent = new ImplementsMyEventInterface ();
    
    instanceOfMyEvent.myNewEvent += instanceOfMyEvent_myNewEvent;
}

// EventHandler in Form
private void instanceOfMyEvent_myNewEvent(object sender, CustomEventArgs e)
{
    Console.WriteLine("In EventHandler for implementation of 'myNewEvent in instance of the class 'usesMyEvent");
    Console.WriteLine("e.Number = {0} e.Name = {1}", e.Number, e.Name);
}

// example of raising the event:

private void SomeButton_Click(object sender, EventArgs e)
{
    instanceOfMyEvent_myNewEvent(this, new CustomEventArgs(number: 100, name: "hello"));
}</customeventargs></customeventargs></customeventargs></t>
 
Share this answer
 
Comments
Zoran Panev 12-Nov-14 6:52am    
but I like to use this event for call void functions with no arguments. Something like:
void MyFunc(){ ...... }
and
In other class to call this event like:
someObjectNameForEvent = new MyEvent();// interface obj
someObjectNameForEvent.myNewEvent += MyFunc;
BillWoodruff 12-Nov-14 11:14am    
If you are really eager to create an Event with no arguments, yes, I can show you how to do that, but ... consider ... if you are writing classes/code that will be "consumed" by other classes/applications it may be best to use .NET's 'event where you are "committing" yourself to implementing the "standard" model of C# .NET Event definition where you will pass two arguments, an Object, and an instance of some flavor of EventHandler.

Brad Abrams in "FrameWork Design Guidelines" quoted on MSDN:

"Consider using a subclass of EventArgs as the event argument, unless you are absolutely sure the event will never need to carry any data to then event handling method, in which case you can use the EventArgs type directly.

If you ship an API using EventArgs directly, you will never be able to add any data to be carried with the event without breaking compatibility. If you use a subclass, even if initially completely empty, you will be able to add properties to the subclass when needed."

http://msdn.microsoft.com/en-us/library/ms229011(v=vs.110).aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900