Raising event

S

SimonZ

Hi,

can someone explain me this piece of code:

public class AlarmEventArgs : EventArgs
{
private readonly bool snoozePressed ;
private readonly int nrings;

//Constructor.
//
public AlarmEventArgs(bool snoozePressed, int nrings)
{
this.snoozePressed = snoozePressed;
this.nrings = nrings;
}
......
....

Why means constructor? Why method AlarmEventArgs has the same name as class?
To be called every time when class AlarmEventArgs is instantiated?

Why set nrings to nrings? Just to be available outside of method
AlarmEventArgs?

The complete code is on:
http://msdn.microsoft.com/library/d...en-us/cpguide/html/cpconeventsmini-sample.asp

Thanks,S
 
P

Peter Rilling

The common way that people define events and delegates is simple a
convention, there is really no reason that things have to be named the way
they are.

1) Since AlarmEventArgs is only a class, it has a constructor. This forces
the consumer to pass in properties that are expected for proper behavior.
2) Constructors always have the same name as the class. That is how the
compiler knows it is a constructor.
3) The constructor simply initializes the internal variables that will be
used later.

Generally speaking (and this is not a rule but rather a convention), event
arguments are derived from EventArgs.
 
R

Rob Windsor [MVP]

The constructor is the method that is called when an instance of a class is
created. For example:

AlarmEventArgs args = new AlarmEventArgs(false, 10);

In this case, when the object args is created the constructor will execute
and it will populate snoozePressed with false and nrings with 10.

The name of the constructor is the same as the class, this is how it is
identified as a constructor.

The code "this.nrings = nrings" sets the value of the private variable
(field) to the value passed in as a parameter to the constructor. Now any
code inside the AlarmEventArgs class can access the value of that field.

I suggest that you buy a book or find some online resources that cover the
basics of object oriented programming (OOP). These will be very helpful in
answering questions similar to these.
 
S

SimonZ

Thank you for your answers.

That was all I wanted to know.

regards,
S

Rob Windsor said:
The constructor is the method that is called when an instance of a class
is created. For example:

AlarmEventArgs args = new AlarmEventArgs(false, 10);

In this case, when the object args is created the constructor will execute
and it will populate snoozePressed with false and nrings with 10.

The name of the constructor is the same as the class, this is how it is
identified as a constructor.

The code "this.nrings = nrings" sets the value of the private variable
(field) to the value passed in as a parameter to the constructor. Now any
code inside the AlarmEventArgs class can access the value of that field.

I suggest that you buy a book or find some online resources that cover the
basics of object oriented programming (OOP). These will be very helpful in
answering questions similar to these.

--
Rob Windsor [MVP-VB]
G6 Consulting
Toronto, Canada
http://msmvps.com/windsor/


SimonZ said:
Hi,

can someone explain me this piece of code:

public class AlarmEventArgs : EventArgs
{
private readonly bool snoozePressed ;
private readonly int nrings;

//Constructor.
//
public AlarmEventArgs(bool snoozePressed, int nrings)
{
this.snoozePressed = snoozePressed;
this.nrings = nrings;
}
.....
...

Why means constructor? Why method AlarmEventArgs has the same name as
class?
To be called every time when class AlarmEventArgs is instantiated?

Why set nrings to nrings? Just to be available outside of method
AlarmEventArgs?

The complete code is on:
http://msdn.microsoft.com/library/d...en-us/cpguide/html/cpconeventsmini-sample.asp

Thanks,S
 

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