Monday, January 31, 2011

[Dev, C#] Generic EventArgs

This is something I created a few years ago and it really helps me every week multiple times. It is a generic type of EventArgs. This way no EventArgs for a specific type need to be created. This saves time and makes it (for me) easier readable.

The code

    /// <summary>
/// This is the definition for a generic eventargs class with one
/// parameter.
/// </summary>
public class ParameterEventArgs<T> : EventArgs
{
#region Constructor

/// <summary>
/// Constructor...
/// </summary>
/// <param name="parameter">The parameter to pass.</param>
public ParameterEventArgs(T parameter)
{
_parameter = parameter;
}

#endregion

#region Properties

/// <summary>
/// Gets the parameter from the event arguments.
/// </summary>
public T Parameter
{
get
{
return _parameter;
}
}

#endregion

#region Fields

private T _parameter;

#endregion
}
Using it

public event EventHandler<ParameterEventArgs<int>> EventWhichPassesAnInt;
public event EventHandler<ParameterEventArgs<CustomClass>> EventWhichPassesACustomClass;

No comments:

Post a Comment