5

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.

John Woo
  • 258,903
  • 69
  • 498
  • 492
user961440
  • 51
  • 1
  • 1
  • 8

2 Answers2

19

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));
    }
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
2

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;
Adil
  • 146,340
  • 25
  • 209
  • 204