J
Joe Cool
OK, based on my research and personal experiance, here is how I code
events in C#.NET.
First, let's say that the event I need to code will return a custome
EventArgs object, so I define that class as:
public class MyCustomEventArgs : EventArgs
{
public MyCustomEventArgs(string myCustomEventProperty)
{
this.myCustomEventProperty = myCustomEventProperty;
}
public string myCustomEventProperty;
}
Next, in the class that needs to expose this property, I first declare
a delegate for the event:
public delegate void MyEventHandler(object sender, MyCustomEventArgs
ce);
Next, I define the public event:
public event MyEventHandler MyEvent;
Next I declare a special method that any other method or event handler
in the class can use to raise the event:
protected virtual void OnMyEvent(string myCustomEventProperty)
{
MyCustomEventArgs ce = new MyCustomEventArgs(myCustomEventProperty);
if (this.MyEvent != null)
{
this.MyEvent(this, ce);
}
}
Now to raise the event I can:
this.OnMyEvent("a string value");
Now, my question is this. The OnMyEvent method is not realy required
to raise this event. I could just as easily have:
MyCustomEventArgs ce = new MyCustomEvent();
ce.myCustromEventProperty = "a string value";
if (this.MyEvent != null)
{
this.MyEvent(this, ce);
}
From what I can tell, the OnMyEvent method is only useful if the event
needs to ber raised in more than one place in the class.
Is this a correct assumption? Or is there a reason to use the
OnMyEvent method even if the event will only be raised once in the
class?
events in C#.NET.
First, let's say that the event I need to code will return a custome
EventArgs object, so I define that class as:
public class MyCustomEventArgs : EventArgs
{
public MyCustomEventArgs(string myCustomEventProperty)
{
this.myCustomEventProperty = myCustomEventProperty;
}
public string myCustomEventProperty;
}
Next, in the class that needs to expose this property, I first declare
a delegate for the event:
public delegate void MyEventHandler(object sender, MyCustomEventArgs
ce);
Next, I define the public event:
public event MyEventHandler MyEvent;
Next I declare a special method that any other method or event handler
in the class can use to raise the event:
protected virtual void OnMyEvent(string myCustomEventProperty)
{
MyCustomEventArgs ce = new MyCustomEventArgs(myCustomEventProperty);
if (this.MyEvent != null)
{
this.MyEvent(this, ce);
}
}
Now to raise the event I can:
this.OnMyEvent("a string value");
Now, my question is this. The OnMyEvent method is not realy required
to raise this event. I could just as easily have:
MyCustomEventArgs ce = new MyCustomEvent();
ce.myCustromEventProperty = "a string value";
if (this.MyEvent != null)
{
this.MyEvent(this, ce);
}
From what I can tell, the OnMyEvent method is only useful if the event
needs to ber raised in more than one place in the class.
Is this a correct assumption? Or is there a reason to use the
OnMyEvent method even if the event will only be raised once in the
class?