Wrox:Anonymous method in event hanlder

  • Thread starter Thread starter moondaddy
  • Start date Start date
M

moondaddy

I'm reading the book Wrox-Professional C# 2005 and it's trying to
demonstrate an anonymous method in an event handler. I would not do this in
a real app, but am trying to understand how this works.

They have a windows form with a button on it. Rather than wiring up the
click event to a method, they just wire it up to some code like this:

btnOne.Click += new EventHandler(lblInfo.Text = "Show some text.";);

Doesn't look like a good idea, but never the less, I'm trying to see it
work. this line is exactly as they have in the book and it get the
following compile errors:

Error 1 ) expected
Error 2 Invalid expression term ')'

Am I missing something or is the author out of his mind?

Thanks.
 
Well, there's a spurious semicolon before the final parentheses...
Can't speak to the validity of this method otherwise, but that error
will go away if you change the ending to: text.");

Bob
 
HI,

moondaddy said:
I'm reading the book Wrox-Professional C# 2005 and it's trying to
demonstrate an anonymous method in an event handler. I would not do this
in a real app, but am trying to understand how this works.

Why not? anonymous methods are an elegant solution

Doesn't look like a good idea, but never the less,

believe me, it's a VERY good idea !
I'm trying to see it work. this line is exactly as they have in the book
and it get the following compile errors:

Error 1 ) expected
Error 2 Invalid expression term ')'

Am I missing something or is the author out of his mind?

I don't have 2.0 here with me, but I could bet that the correct code is:

btnOne.Click += EventHandler( object sender, EventArgs e){ lblInfo.Text =
"Show some text."; };



cheers,
 
Response inline

moondaddy said:
They have a windows form with a button on it. Rather than wiring up the
click event to a method, they just wire it up to some code like this:

btnOne.Click += new EventHandler(lblInfo.Text = "Show some text.";);

Actually, the syntax is totally off. If you want an anonymous delegate:
btnOne.Click += delegate { lblInfo.Text = "Show some text."; };

Alternatively, you can assign a standard method:
btnOne.Click += new EventHandler(btnOne_Click);

Or use a shortened syntax new in C# 2.0:
btnOne.Click += btnOne_Click;
Doesn't look like a good idea,
I find anonymous delegates very elegant for short blocks of event handlers
or mini functions. An hypothetical example:

string[] array = GetSomeStrings();
// count strings with more than four characters
int countLongStuff = Query.Count(array,
delegate(string s) { return s.Length > 4; });
// order strings ascending by length
IEnumerable<string> sorted = Query.OrderBy<string, int>(array,
delegate(string s) { return s.Length; });

HTH,
Mark
 
this won't compile. Out of all the responses in this thread, the only one
that compiles is:

btnOne.Click += delegate { lblInfo.Text = "Show some text.";};

Maybe the text code from the Wrox book is beta code, but the book doesn't
claim to be a beta book. Maybe they were guessing and didn't test their
code first.
 
Actually the code that works is in the next post from StealthyMark:

btnOne.Click += delegate { lblInfo.Text = "Show some text.";};
and even this nice shorthand to a method:
btnOne.Click += Button_Click;
 
Thanks Mark, these worked fine. its a wonder Wrox didnt get it.

--
(e-mail address removed)
StealthyMark said:
Response inline

moondaddy said:
They have a windows form with a button on it. Rather than wiring up the
click event to a method, they just wire it up to some code like this:

btnOne.Click += new EventHandler(lblInfo.Text = "Show some text.";);

Actually, the syntax is totally off. If you want an anonymous delegate:
btnOne.Click += delegate { lblInfo.Text = "Show some text."; };

Alternatively, you can assign a standard method:
btnOne.Click += new EventHandler(btnOne_Click);

Or use a shortened syntax new in C# 2.0:
btnOne.Click += btnOne_Click;
Doesn't look like a good idea,
I find anonymous delegates very elegant for short blocks of event handlers
or mini functions. An hypothetical example:

string[] array = GetSomeStrings();
// count strings with more than four characters
int countLongStuff = Query.Count(array,
delegate(string s) { return s.Length > 4; });
// order strings ascending by length
IEnumerable<string> sorted = Query.OrderBy<string, int>(array,
delegate(string s) { return s.Length; });

HTH,
Mark
 
Thanks Mark, these worked fine. its a wonder Wrox didnt get it.

I had the edition that preceded this book. It is full of wonderful
incongruities and suspect samples. The edition I have did not in any
way live up to what I consider Wrox's usually high standards. This is
probably a good example of why not to write a book by committee.

Ken Wilson
Seeking viable IT employment in Victoria, BC
 

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

Back
Top