A
aaj
Hi
I'm an OOP newbie and working my way through some of the concepts of .NET
and c#.
I'm looking at messaging between objects using events, but having a few
problems generating them. This is the code I'm trying to get working but
with no luck.
To prove it works, I'm trying to force an event from elsewhere by calling
haveago().
The DemoEventEventArgs instatiates ok, but when I run the 'this' part of
DemoEvent(this,new DemoEventEventArgs(5));
it fails on Null value. I am assuming its the 'this' part causing the
problem so I added the OnDemoEvent method but with no luck.
can anyone help me with these new wonderfull concepts, that I'm begining to
enjoy but still struggling with
thanks
Andy
public MonitoringClass()
{
}
public void haveago()
{
try
{
DemoEvent(this,new DemoEventEventArgs(5));
}
catch(System.Exception e)
{
MessageBox.Show(e.Message);
}
}
protected void OnDemoEvent(object sender, DemoEventEventArgs e)
{
if (DemoEvent!=null) DemoEvent(sender,e);
}
public delegate void DemoEventEventHandler(object sender, DemoEventEventArgs
e);
public static event DemoEventEventHandler DemoEvent;
}
//////////////////////////////////////////
public class DemoEventEventArgs : System.EventArgs
{
// this is a class that holds all the data we need to pass during an event
message
private int m_DemoArguments = 0;
public int DemoArguments
{
get
{
return m_DemoArguments;
}
}
public DemoEventEventArgs(int demoarguments)
{
m_DemoArguments = demoarguments;
}
}
}
I'm an OOP newbie and working my way through some of the concepts of .NET
and c#.
I'm looking at messaging between objects using events, but having a few
problems generating them. This is the code I'm trying to get working but
with no luck.
To prove it works, I'm trying to force an event from elsewhere by calling
haveago().
The DemoEventEventArgs instatiates ok, but when I run the 'this' part of
DemoEvent(this,new DemoEventEventArgs(5));
it fails on Null value. I am assuming its the 'this' part causing the
problem so I added the OnDemoEvent method but with no luck.
can anyone help me with these new wonderfull concepts, that I'm begining to
enjoy but still struggling with
thanks
Andy
public MonitoringClass()
{
}
public void haveago()
{
try
{
DemoEvent(this,new DemoEventEventArgs(5));
}
catch(System.Exception e)
{
MessageBox.Show(e.Message);
}
}
protected void OnDemoEvent(object sender, DemoEventEventArgs e)
{
if (DemoEvent!=null) DemoEvent(sender,e);
}
public delegate void DemoEventEventHandler(object sender, DemoEventEventArgs
e);
public static event DemoEventEventHandler DemoEvent;
}
//////////////////////////////////////////
public class DemoEventEventArgs : System.EventArgs
{
// this is a class that holds all the data we need to pass during an event
message
private int m_DemoArguments = 0;
public int DemoArguments
{
get
{
return m_DemoArguments;
}
}
public DemoEventEventArgs(int demoarguments)
{
m_DemoArguments = demoarguments;
}
}
}