Sunday, September 1, 2013

Creating Events in C#

In C#, creating and consuming events are easy and definitely not a rocket science. However, from time to time, I find myself keep having trouble remembering how to implement it.

So, this is more a "note-to-self" post on creating... events.


Method 1:

public class MyClass
{
    /// <summary>
    /// MyClass event for getting data progress.
    /// </summary>
    public event GetDataProgressEventHandler GetDataProgress;

    /// <summary>
    /// MyClass event handler for getting data progress.
    /// </summary>
    /// <param name="percentage">Progress percentage.</param>
    /// <param name="message">Progress message.</param>
    public delegate void GetDataProgressEventHandler(decimal percentage, string message);

    ...
    
    public void GetData()
    {
        if (GetDataProgress!= null)
            GetDataProgress(((decimal)count / (decimal)max) * 100, statusMessage);    // Trigger the get data progress event.
    }
}

Usage Example:

MyClass myClass          = new MyClass();
myClass.GetDataProgress += new MyClass.GetDataProgressEventHandler(myClass_GetDataProgress);

...

void myClass_GetDataProgress(decimal progress, string message)
{
    ...
}


Method 2 (using Event Data):

public class MyClass
{
    /// <summary>
    /// Occurs when MyClass getting data giving progress.
    /// </summary>
    public event EventHandler<GetDataProgressEventArgs> GetDataProgress;

    /// <summary>
    /// Raises the GetDataProgress event of MyClass.
    /// </summary>
    /// <param name="e">The event argument.</param>
    protected virtual void OnGetDataProgress(GetDataProgressEventArgs e)
    {
        EventHandler<GetDataProgressEventArgs> handler = GetDataProgress;

        if (handler != null)
        {
            handler(this, e);
        }
    }

    ...
    
    public void GetData()
    {
        ...

        GetDataProgressEventArgs getDataProgressEventArgs = new GetDataProgressEventArgs();
        getDataProgressEventArgs.Percentage = ((decimal)count / (decimal)max) * 100;
        getDataProgressEventArgs.Message    = statusMessage;

        OnGetDataProgress(getDataProgressEventArgs);    // Trigger the get data progress event.
    }
}

For the event data, you need to define a class for GetDataProgressEventArgs:

/// <summary>
/// A derived EventArgs class used by MyClass to pass the GetDataProgress event data.
/// </summary>
public class GetDataProgressEventArgs : EventArgs
{
    private decimal _percentage = 0;

    /// <summary>
    /// Progress percentage.
    /// </summary>
    public decimal Percentage 
    { 
        get { return _percentage;  }
        set { _percentage = value; } 
    }

    private string _message = null;

    /// <summary>
    /// Progress status message.
    /// </summary>
    public string Message
    {
        get { return _message;  }
        set { _message = value; }
    }
}

Usage Example:

MyClass myClass          = new MyClass();
myClass.GetDataProgress += new EventHandler<GetDataProgressEventArgs>(myClass_GetDataProgress);

...

void myClass_GetDataProgress(object sender, GetDataProgressEventArgs e)
{
    ...
}


Additional Resources:
Handling and Raising Events



If you find this post helpful, would you buy me a coffee?


No comments:

Post a Comment