Events

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;
}

}
}
 
G

Guest

You should replace:
DemoEvent(this,new DemoEventEventArgs(5));


With:
OnDemoEvent(this,new DemoEventEventArgs(5));

That way your request from MonitoringClass to raise the event first checks
to see if there are any subscribers to it, and if there are calls them. The
reason your first call is most likely failing is because nothing is currently
subscribed to DemoEvent.

Brendan
 
M

Moty Michaely

Hey Man,

You should add this line of codes:

public class Demo
{
public event DemoEventHandler DemoEvent
public void OnDemoEvent(EventArgs e)
{
if this.DemoEvent != null
this.DemoEvent(this, e)
}

public void DoSomething()
{
this.OnDemoEvent()
}
}
Then, create an instance of your class add an event handler and do
something:

Demo d = new Demo()
d.DemoEvent += new DemoEventHandler([delegate])
d.DoSomething // this will raise the event


I hasn't checked it but this should work. It's the best way of raising
events.

Take care of events in multi threaded areas.

Cheers,
- Moty -
 
A

aaj

Thanks all

I used both answers and have it working now, but I'm not exactly sure how?.

would any one be able to explain. I've posted the full code at the end along
with my comments as to how I think it works, the big question I have is the
line

// ??? Why do I need this line, I'm guessing it instatiates the event so is
has a reference to something ,
this.DemoEvent +=new DemoEventEventHandler(MonitoringClass_DemoEvent);

but why += does it read this.DemoEvent =this.DemoEvent + new
DemoEventEventHandler(MonitoringClass_DemoEvent); if so why?


thanks again

Andy


[System.ComponentModel.DefaultEvent("DemoEvent")]
public class MonitoringClass
{
// this is the code that handles the event when the class generates it
// create a new object type called ...EventHandler that holds information
about the sender plus some aguments stored in the class DemoEventEventArgs
public delegate void DemoEventEventHandler(object sender, DemoEventEventArgs
e);
// generate the public event built up from the main class and the relevant
arguments
public event DemoEventEventHandler DemoEvent;
public MonitoringClass()
{
}
public void haveago()
{
try
{
// ??? Why do I need this line, I'm guessing it instatiates the event so is
has a reference to something
this.DemoEvent +=new DemoEventEventHandler(MonitoringClass_DemoEvent);
// call the instantiated event
this.OnDemoEvent(new DemoEventEventArgs(5));
}
catch(System.Exception e)
{
MessageBox.Show(e.Message);
}
}
protected void OnDemoEvent(DemoEventEventArgs e)
{
// only if the demo event has been instantiated allow it to run
if (DemoEvent!=null) this.DemoEvent(this,e);
}
// And generate the event
private void MonitoringClass_DemoEvent(object sender, DemoEventEventArgs e)
{
MessageBox.Show("Event Generated " + e.DemoArguments.ToString() );
}
}

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;
}
}
}








Moty Michaely said:
Hey Man,

You should add this line of codes:

public class Demo
{
public event DemoEventHandler DemoEvent
public void OnDemoEvent(EventArgs e)
{
if this.DemoEvent != null
this.DemoEvent(this, e)
}

public void DoSomething()
{
this.OnDemoEvent()
}
}
Then, create an instance of your class add an event handler and do
something:

Demo d = new Demo()
d.DemoEvent += new DemoEventHandler([delegate])
d.DoSomething // this will raise the event


I hasn't checked it but this should work. It's the best way of raising
events.

Take care of events in multi threaded areas.

Cheers,
- Moty -

aaj said:
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;
}

}
}
 
M

Moty Michaely

Hey,

+ Operator:
Delegate types also provide a binary + operator, which performs delegate
concatenation.

Since any event is just a delegate that can be concatenated, you use the +
operator to concatanate delegates or - operator to subtract delegates.

Hope this helps.

- Moty -


aaj said:
Thanks all

I used both answers and have it working now, but I'm not exactly sure
how?.

would any one be able to explain. I've posted the full code at the end
along with my comments as to how I think it works, the big question I have
is the line

// ??? Why do I need this line, I'm guessing it instatiates the event so
is has a reference to something ,
this.DemoEvent +=new DemoEventEventHandler(MonitoringClass_DemoEvent);

but why += does it read this.DemoEvent =this.DemoEvent + new
DemoEventEventHandler(MonitoringClass_DemoEvent); if so why?


thanks again

Andy


[System.ComponentModel.DefaultEvent("DemoEvent")]
public class MonitoringClass
{
// this is the code that handles the event when the class generates it
// create a new object type called ...EventHandler that holds information
about the sender plus some aguments stored in the class DemoEventEventArgs
public delegate void DemoEventEventHandler(object sender,
DemoEventEventArgs e);
// generate the public event built up from the main class and the relevant
arguments
public event DemoEventEventHandler DemoEvent;
public MonitoringClass()
{
}
public void haveago()
{
try
{
// ??? Why do I need this line, I'm guessing it instatiates the event so
is has a reference to something
this.DemoEvent +=new DemoEventEventHandler(MonitoringClass_DemoEvent);
// call the instantiated event
this.OnDemoEvent(new DemoEventEventArgs(5));
}
catch(System.Exception e)
{
MessageBox.Show(e.Message);
}
}
protected void OnDemoEvent(DemoEventEventArgs e)
{
// only if the demo event has been instantiated allow it to run
if (DemoEvent!=null) this.DemoEvent(this,e);
}
// And generate the event
private void MonitoringClass_DemoEvent(object sender, DemoEventEventArgs
e)
{
MessageBox.Show("Event Generated " + e.DemoArguments.ToString() );
}
}

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;
}
}
}








Moty Michaely said:
Hey Man,

You should add this line of codes:

public class Demo
{
public event DemoEventHandler DemoEvent
public void OnDemoEvent(EventArgs e)
{
if this.DemoEvent != null
this.DemoEvent(this, e)
}

public void DoSomething()
{
this.OnDemoEvent()
}
}
Then, create an instance of your class add an event handler and do
something:

Demo d = new Demo()
d.DemoEvent += new DemoEventHandler([delegate])
d.DoSomething // this will raise the event


I hasn't checked it but this should work. It's the best way of raising
events.

Take care of events in multi threaded areas.

Cheers,
- Moty -

aaj said:
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;
}

}
}
 
A

aaj

Excellent, I think I'm getting the idea, but I'll have a bit of a read up on
Delegates


thanks for the help

Andy

Moty Michaely said:
Hey,

+ Operator:
Delegate types also provide a binary + operator, which performs delegate
concatenation.

Since any event is just a delegate that can be concatenated, you use the +
operator to concatanate delegates or - operator to subtract delegates.

Hope this helps.

- Moty -


aaj said:
Thanks all

I used both answers and have it working now, but I'm not exactly sure
how?.

would any one be able to explain. I've posted the full code at the end
along with my comments as to how I think it works, the big question I
have is the line

// ??? Why do I need this line, I'm guessing it instatiates the event so
is has a reference to something ,
this.DemoEvent +=new DemoEventEventHandler(MonitoringClass_DemoEvent);

but why += does it read this.DemoEvent =this.DemoEvent + new
DemoEventEventHandler(MonitoringClass_DemoEvent); if so why?


thanks again

Andy


[System.ComponentModel.DefaultEvent("DemoEvent")]
public class MonitoringClass
{
// this is the code that handles the event when the class generates it
// create a new object type called ...EventHandler that holds information
about the sender plus some aguments stored in the class
DemoEventEventArgs
public delegate void DemoEventEventHandler(object sender,
DemoEventEventArgs e);
// generate the public event built up from the main class and the
relevant arguments
public event DemoEventEventHandler DemoEvent;
public MonitoringClass()
{
}
public void haveago()
{
try
{
// ??? Why do I need this line, I'm guessing it instatiates the event so
is has a reference to something
this.DemoEvent +=new DemoEventEventHandler(MonitoringClass_DemoEvent);
// call the instantiated event
this.OnDemoEvent(new DemoEventEventArgs(5));
}
catch(System.Exception e)
{
MessageBox.Show(e.Message);
}
}
protected void OnDemoEvent(DemoEventEventArgs e)
{
// only if the demo event has been instantiated allow it to run
if (DemoEvent!=null) this.DemoEvent(this,e);
}
// And generate the event
private void MonitoringClass_DemoEvent(object sender, DemoEventEventArgs
e)
{
MessageBox.Show("Event Generated " + e.DemoArguments.ToString() );
}
}

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;
}
}
}








Moty Michaely said:
Hey Man,

You should add this line of codes:

public class Demo
{
public event DemoEventHandler DemoEvent
public void OnDemoEvent(EventArgs e)
{
if this.DemoEvent != null
this.DemoEvent(this, e)
}

public void DoSomething()
{
this.OnDemoEvent()
}
}

Then, create an instance of your class add an event handler and do
something:

Demo d = new Demo()
d.DemoEvent += new DemoEventHandler([delegate])
d.DoSomething // this will raise the event


I hasn't checked it but this should work. It's the best way of raising
events.

Take care of events in multi threaded areas.

Cheers,
- Moty -

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;
}

}
}
 

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

Top