radio button handlers

A

Alex K.

Hi all

if three radio buttons can be handled by one handler, should I still create
three new handlers? E.g. can I use this:

this.radioA.CheckedChanged += LocationCheckedChanged;
this.radioB.CheckedChanged += LocationCheckedChanged;
this.radioC.CheckedChanged += LocationCheckedChanged;
...
void LocationCheckedChanged(object sender, EventArgs e)
{
...
}

instead of this:

this.radioA.CheckedChanged += new EventHandler(LocationCheckedChanged);
this.radioB.CheckedChanged += new EventHandler(LocationCheckedChanged);
this.radioC.CheckedChanged += new EventHandler(LocationCheckedChanged);
....

What are pros and cons?

Thank you
Alex
 
J

Jeff Johnson

This syntax:
this.radioA.CheckedChanged += LocationCheckedChanged;

is simply a shortcut for this syntax:
this.radioA.CheckedChanged += new EventHandler(LocationCheckedChanged);

They do exactly the same thing. Correct me if I'm wrong, someone, but I
believe the syntax of the first example was introduced in C# 2.0.
 
J

Jeff Johnson

This syntax:
this.radioA.CheckedChanged += LocationCheckedChanged;

is simply a shortcut for this syntax:
this.radioA.CheckedChanged += new EventHandler(LocationCheckedChanged);

They do exactly the same thing. Correct me if I'm wrong, someone, but I
believe the syntax of the first example was introduced in C# 2.0.
 
I

Ignacio Machin ( .NET/ C# MVP )

Hi all

if three radio buttons can be handled by one handler, should I still create
three new handlers? E.g. can I use this:

            this.radioA.CheckedChanged += LocationCheckedChanged;
            this.radioB.CheckedChanged += LocationCheckedChanged;
            this.radioC.CheckedChanged += LocationCheckedChanged;
            ...
            void LocationCheckedChanged(object sender, EventArgs e)
            {
             ...
            }

instead of this:

this.radioA.CheckedChanged += new EventHandler(LocationCheckedChanged);
this.radioB.CheckedChanged += new EventHandler(LocationCheckedChanged);
this.radioC.CheckedChanged += new EventHandler(LocationCheckedChanged);
...

What are pros and cons?

Thank you
Alex

It's the same. You are creating 3 delegates. A different approach
would be
EventHandler eh = new EventHandler(LocationCheckedChanged);

this.radioA.CheckedChanged += eh ;
this.radioB.CheckedChanged += eh ;
this.radioC.CheckedChanged += eh ;

In this case you create only one delegate

But IMHO it's not a big difference
 
I

Ignacio Machin ( .NET/ C# MVP )

Hi all

if three radio buttons can be handled by one handler, should I still create
three new handlers? E.g. can I use this:

            this.radioA.CheckedChanged += LocationCheckedChanged;
            this.radioB.CheckedChanged += LocationCheckedChanged;
            this.radioC.CheckedChanged += LocationCheckedChanged;
            ...
            void LocationCheckedChanged(object sender, EventArgs e)
            {
             ...
            }

instead of this:

this.radioA.CheckedChanged += new EventHandler(LocationCheckedChanged);
this.radioB.CheckedChanged += new EventHandler(LocationCheckedChanged);
this.radioC.CheckedChanged += new EventHandler(LocationCheckedChanged);
...

What are pros and cons?

Thank you
Alex

It's the same. You are creating 3 delegates. A different approach
would be
EventHandler eh = new EventHandler(LocationCheckedChanged);

this.radioA.CheckedChanged += eh ;
this.radioB.CheckedChanged += eh ;
this.radioC.CheckedChanged += eh ;

In this case you create only one delegate

But IMHO it's not a big difference
 

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