Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to implement (Object sender, EventArgs e) in events?

Discussion in 'Scripting' started by Lancaster13, Feb 4, 2015.

  1. Lancaster13

    Lancaster13

    Joined:
    Dec 24, 2014
    Posts:
    25
    Hello Guys,

    I'm studying events, and i read a lot of articles and tutorials about events, but i can´t understand, how can i declare and use (Object sender, EventArgs e) in my events?

    Thanks!
     
  2. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    So, have your written an event handler yet or are you asking how to do that part? Or, are you asking how to use the passed in parameters in your event handler?
     
  3. Lancaster13

    Lancaster13

    Joined:
    Dec 24, 2014
    Posts:
    25
    My event handler is my delegate method, right? How i can declare the parameters. Can you give me a example in code? I just need see, how i can declare the parameters.
     
  4. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    For future reference, please provide code snippets. It helps narrow down where you are having problems.

    An event is composed of a delegate and an event type. The delegate determines how the event is called and the parameters that are passed. The event type is what the interested parties subscribe to in order to receive the event.

    Below is a snippet from Microsoft's event example (here). The parameters are declared in the delegate as you can see in the first couple lines below. You then need to declare the event in the class that will raise it (Changed in the example). Create the method to invoke the event (OnChanged). Finally, signal the event by calling OnChanged where it's needed in the rest of the class such as in Add() in the example below.

    Code (CSharp):
    1.    // A delegate type for hooking up change notifications.
    2.    public delegate void ChangedEventHandler(object sender, EventArgs e);
    3.  
    4.    // A class that works just like ArrayList, but sends event
    5.    // notifications whenever the list changes.
    6.    public class ListWithChangedEvent: ArrayList
    7.    {
    8.       // An event that clients can use to be notified whenever the
    9.       // elements of the list change.
    10.       public event ChangedEventHandler Changed;
    11.  
    12.       // Invoke the Changed event; called whenever list changes
    13.       protected virtual void OnChanged(EventArgs e)
    14.       {
    15.          if (Changed != null)
    16.             Changed(this, e);
    17.       }
    18.  
    19.       // Override some of the methods that can change the list;
    20.       // invoke event after each
    21.       public override int Add(object value)
    22.       {
    23.          int i = base.Add(value);
    24.          OnChanged(EventArgs.Empty);
    25.          return i;
    26.       }
    27.  
    28.       public override void Clear()
    29.       {
    30.          base.Clear();
    31.          OnChanged(EventArgs.Empty);
    32.       }
    33.  
    34.       public override object this[int index]
    35.       {
    36.          set
    37.          {
    38.             base[index] = value;
    39.             OnChanged(EventArgs.Empty);
    40.          }
    41.       }
    42.    }
    43.