EventArgs Example

In one my past posts, I talked about that CryptoCollaboration using the EventArgs base class because it keeps things nice and generalized, allowing for expanasion for the handling of future event integration. A person emailed me and asked for a practical example of when would use such a thing in a SharePoint WebPart. Eh, ok.

I am going to use an example of a progress class, that, well, as you can guess will display the progress of some sort of execution that happens with a SharePoint WebPart.

The first class is the Process Arguments class (ProcessArgs), that inherits from the EventArgs base class.

[csharp]

public class ProgressArgs : EventArgs
{
private int m_nPercentageComplete = 0;
private DataRow m_oDataRow;

public ProgressArgs(DataRow dataRow, int percentageComplete)
{
this.m_oDataRow = dataRow;
this.m_nPercentageComplete = percentageComplete;
}

public DataRow CurrentDataRow
{
get
{
return this.m_oDataRow;
}
}

public int PercentageComplete
{
get
{
return this.m_nPercentageComplete;
}
}
}

[/csharp]

In the next class, there will be the establishment of the event handler that will be called from a sister class by use of a delegate.

[csharp]

public delegate void ProgressEventHandler(object sender, ProgressArgs e);

[/csharp]

Now that the delegate is established, you can call it in some general class file.

[csharp]

public event ProgressEventHandler ProgressEvent;

protected virtual void OnProgress(ProgressArgs e)
{
if (this.ProgressEvent != null)
{
this.ProgressEvent(this, e);
}
}

[/csharp]

Then calculate out the progress.

[csharp]

ProgressArgs e = new ProgressArgs(row, (int) ((((double) num) / ((double) this.DBDataTable.Rows.Count)) * 100));
this.OnProgress(e);

[/csharp]

I am not saying that this is the only way to do it, but with applications that you plan on extending later it is the route that I have choosen to go in the past. I may at some point explore the deficiencies in a such an approach, however at this current time I just want to finish the damn thing.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *