C# Event Raising Problem

T

Tina

I am trying to raise a custom event in a user control passing custom data
arguments. I have done this many times in VB but this is my first time in
C#.

The code below passes the proper arguments in the
this.OnRaiseCFEvent(myArgs); code but in the OnRaiseCFEvent method
RaiseCFEVent is always null so this.RaiseCFEvent(this,e); never executes.

Can anyone tell my why RaiseCFEvent is always null?
thanks,
T




public partial class EditGrid : System.Web.UI.UserControl
{
public delegate void ChangedFieldsEventHandler(object sender,
ChangedFieldsEventArgs e);
public event ChangedFieldsEventHandler RaiseCFEvent;
protected virtual void OnRaiseCFEvent(ChangedFieldsEventArgs e)
{
if (this.RaiseCFEvent != null)
{
this.RaiseCFEvent(this,e);
}
}
..
..
..
ChangedFieldsEventArgs myArgs = new
ChangedFieldsEventArgs(gvRow.RowIndex, i, string1, string2);
this.OnRaiseCFEvent(myArgs);

..
..
..



}

public class ChangedFieldsEventArgs : EventArgs
{
public readonly int row, col;
public readonly string newVal, oldVal;
public ChangedFieldsEventArgs(int row, int col, string newVal, string
oldVal)
{
this.row = row;
this.col = col;
this.newVal = newVal;
this.oldVal = oldVal;
}
}
 
T

Tim_Mac

hi Tina
at a guess, your code goes something like this:

if(RaiseCFEVent != null)
this.RaiseCFEvent(args);

and you are wondering why the RaiseCFEVent delegate is null? if this is
null, it means nobody is listening for the event.
if you want some code to listen to the event, use code like this:

MyObject.RaiseCFEvent += new EventHandler(this.RaiseCFEvent_Handler);

where MyObject is the object that contains the event delegate. it may help
to open up the debugger in VS and examine the RaiseCFEvent delegate before
and after you add your event handlers, you will be able to see if any
handlers are wired up to it.

hope this helps.
tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top