Delegates?

M

Martijn Mulder

There are 2 ways to hook an event to a method. For example, to hook the
MouseEnter-event, you can use

MouseEnter+=OnMouseEnter;

or

MouseEnter+=new System.EventHandler(OnMouseHandler);

Both work well, as demonstrated by the small program below, where the
MouseEnter-event is hooked the one way, the MouseLeave-event the other. What
is the preferred way to hook events to methods, and why?
//class Formclass Form:System.Windows.Forms.Form{ //data member label
System.Windows.Forms.Label label=new System.Windows.Forms.Label();
//constructor Form() { Controls.Add(label); MouseEnter+=OnMouseEnter;
MouseLeave+=new System.EventHandler(OnMouseLeave); } //OnMouseEnter void
OnMouseEnter(System.Object a,System.EventArgs b) {
label.Text="MouseEntered"; } //OnMouseLeave void OnMouseLeave(System.Object
a,System.EventArgs b) { label.Text="MouseLeft"; } //Main [System.STAThread]
static void Main() { System.Windows.Forms.Application.Run(new Form()); }}
 
M

Martijn Mulder

óeps, Linux style ;-) Here is the correct program:




//class Form
class Form:System.Windows.Forms.Form
{


//data member label
System.Windows.Forms.Label label=new System.Windows.Forms.Label();


//constructor
Form()
{
Controls.Add(label);
MouseEnter+=OnMouseEnter;
MouseLeave+=new System.EventHandler(OnMouseLeave);
}


//OnMouseEnter
public void OnMouseEnter(System.Object a,System.EventArgs b)
{
label.Text="MouseEntered";
}


//OnMouseLeave
public void OnMouseLeave(System.Object a,System.EventArgs b)
{
label.Text="MouseLeft";
}


//Main
[System.STAThread]
static void Main()
{
System.Windows.Forms.Application.Run(new Form());
}
}
 
D

Dave Sexton

Hi Martijn,

The compiler simply checks that the signature for the OnMouseEnter method in
your first example matches the signature for the System.EventHandler
delegate required by the MouseEnter event and swaps it with a call to
Delegate.Combine, just like in the second example.

Personally, I like the first syntax better (it's new to the 2.0 framework).
 
D

Dustin Campbell

Hi Martijn, I prefer the first way. The both compile to exactly the same
thang and, IMO, the conciseness does not make it less readable. In fact,
it makes it more readable because, normally, you don't care about the delegate
type that is being instantiated it. Also, because the code in the event handlers
is so simple, I might rewrite it like this:

//class Form
class Form: System.Windows.Forms.Form
{
//data member label
System.Windows.Forms.Label label =new System.Windows.Forms.Label();

//constructor
Form()
{
Controls.Add(label);
MouseEnter += delegate { label.Text = "MouseEntered"; };
MouseLeave += delegate { label.Text = "MouseLeft"; };
}

//Main
[System.STAThread]
static void Main()
{
System.Windows.Forms.Application.Run(new Form());
}
}

Best Regards,
Dustin Campbell
Developer Express Inc.
 
S

Stoitcho Goutsev \(100\)

Martijn,

You didn't say what the problem was. There shouldn't be any problem because
the code generated by the c# compiler is exactly the same in both cases. It
has been added for convinience only. The preffered way is 'whatever makes
you code more readable'.

Execuse me for saying that, but please format a little your code snippets
before posting them. It is unreadable.
 
M

Martijn Mulder

Hi Stoitcho

:) Thank you, I'll try to format a little. Also thanks for other posts that
you answered in a most helpfull way.



//class Form
class Form:System.Windows.Forms.Form
{


//data member label
System.Windows.Forms.Label label=new System.Windows.Forms.Label();


//constructor
Form()
{
Controls.Add(label);
MouseEnter+=OnMouseEnter;
MouseLeave+=new System.EventHandler(OnMouseLeave);
}


//OnMouseEnter
public void OnMouseEnter(System.Object a,System.EventArgs b)
{
label.Text="MouseEntered";
}


//OnMouseLeave
public void OnMouseLeave(System.Object a,System.EventArgs b)
{
label.Text="MouseLeft";
}


//Main
[System.STAThread]
static void Main()
{
System.Windows.Forms.Application.Run(new Form());
}
}



Martijn,

You didn't say what the problem was. There shouldn't be any problem
because the code generated by the c# compiler is exactly the same in both
cases. It has been added for convinience only. The preffered way is
'whatever makes you code more readable'.

Execuse me for saying that, but please format a little your code snippets
before posting them. It is unreadable.


--
HTH
Stoitcho Goutsev (100)

Martijn Mulder said:
There are 2 ways to hook an event to a method. For example, to hook the
MouseEnter-event, you can use

MouseEnter+=OnMouseEnter;

or

MouseEnter+=new System.EventHandler(OnMouseHandler);

Both work well, as demonstrated by the small program below, where the
MouseEnter-event is hooked the one way, the MouseLeave-event the other.
What is the preferred way to hook events to methods, and why?
//class Formclass Form:System.Windows.Forms.Form{ //data member label
System.Windows.Forms.Label label=new System.Windows.Forms.Label();
//constructor Form() { Controls.Add(label); MouseEnter+=OnMouseEnter;
MouseLeave+=new System.EventHandler(OnMouseLeave); } //OnMouseEnter void
OnMouseEnter(System.Object a,System.EventArgs b) {
label.Text="MouseEntered"; } //OnMouseLeave void
OnMouseLeave(System.Object a,System.EventArgs b) {
label.Text="MouseLeft"; } //Main [System.STAThread] static void Main()
{ System.Windows.Forms.Application.Run(new Form()); }}
 
M

Martin Z

Also, one can consider doing the GUI in VB.Net. They're similar enough
that a coder should have no problem jumping from one to the other, and
if you're writing your GUI as a separate project from your logic, then
they can be in different languages anyways. VB.Net has the "Handles"
syntactic sugar that is very very nice.

Advantages of "Handles" (1) afaik, it's tied to the reference, not the
object, so if the reference changes to point to another object, it
automatically handles removing the handler from the old object and
adding it to the new object. (2) Since declaration and handling are all
in one line (the function declaration) you can just delete the
declaration when you accidentally create new handler code after
accidentally doubleclicking on a widget, rather than having to crawl
into the autogenerated code to find that += statement.
 
D

David Boucherie & Co

Advantages of "Handles" ..SNIP..

Disadvantage of "Handles":

If you want your event handlers to activate *after* you set all your
design time values, so they don't fire while initializing, you still
have to use the syntax where you "add" the function to the event.

Not a very big problem for us C# programmers, but as I have experienced,
it tends to confuse VB programmers to no end.

The C# Forms Designer, by the way (as you probably all know), always
adds the handler to the event *after* design time property
initialization of its controls. Default behavior in that respect differs
from the VB way using "Handles". I personally definitely prefer the C# way.

David
 

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