Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax to send two strings in eventArgs

In the following code I need to know the syntax of passing two strings when the event is raised.

 [PublishEvent("Click")]
 public event EventHandler<EventArgs<string>> MyEvent;

Thanks, Saxon.

like image 926
user961440 Avatar asked Sep 19 '12 16:09

user961440


2 Answers

The cleanest way is to create your own class that derives from EventArgs:

    public class MyEventArgs : EventArgs
    {
        private readonly string _myFirstString;
        private readonly string _mySecondString;

        public MyEventArgs(string myFirstString, string mySecondString)
        {
            _myFirstString = myFirstString;
            _mySecondString = mySecondString;
        }

        public string MyFirstString
        {
            get { return _myFirstString; }
        }

        public string MySecondString
        {
            get { return _mySecondString; }
        }
    }

And use it like this:

public event EventHandler<MyEventArgs> MyEvent;

To raise the event, you can do something like this:

    protected virtual void OnMyEvent(string myFirstString, string mySecondString)
    {
        EventHandler<MyEventArgs> handler = MyEvent;
        if (handler != null)
            handler(this, new MyEventArgs(myFirstString, mySecondString));
    }
like image 178
Thomas Levesque Avatar answered Oct 24 '22 03:10

Thomas Levesque


Make your class and extend for EventArgs, and pass it

public class YourCustomeEvent : EventArgs
{
   public string yourVariable {get; }
}

Now you have to provide your custom class like this

 public event EventHandler<YourCustomeEvent> MyEvent;
like image 28
Adil Avatar answered Oct 24 '22 02:10

Adil