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.
-- mo*******@nospam.nospam 7 1410
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" <mo*******@nospam.nospam> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl... 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,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Response inline
"moondaddy" wrote: 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.
-- mo*******@nospam.nospam
"RvGrah" <rv****************@sbcglobal.net> wrote in message
news:11*********************@v46g2000cwv.googlegro ups.com... 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
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;
-- mo*******@nospam.nospam
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:e7*************@TK2MSFTNGP10.phx.gbl... HI,
"moondaddy" <mo*******@nospam.nospam> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl... 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,
-- Ignacio Machin, ignacio.machin AT dot.state.fl.us Florida Department Of Transportation
Thanks Mark, these worked fine. its a wonder Wrox didnt get it.
-- mo*******@nospam.nospam
"StealthyMark" <St**********@discussions.microsoft.com> wrote in message
news:82**********************************@microsof t.com... Response inline
"moondaddy" wrote:
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
On Fri, 3 Mar 2006 15:46:18 -0600, "moondaddy"
<mo*******@nospam.nospam> wrote: 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Thomas Scheiderich |
last post by:
I am trying to understand Session variables and ran into a question on
how they work with data that is passed.
I have an HTM file that calls an ASP file and sends the name either by
GET or POST....
|
by: raj |
last post by:
Hi,
I am facing a problem of automatic session timeout problem and
automatic session_end event fired.
Case1: As I have analyzed I get to know that the default session
timeout is 20 in...
|
by: hazz |
last post by:
I don't get it. I have a VS2005 solution with a web service project and a
windows project. The web service when tested on its own works fine. But when
I add it as a web reference and refer to it my...
|
by: HK |
last post by:
My website emails me when it raises an exception. I'm getting about 10
emails per day that look similar to this, but in each, the IP address and
port, and the email-looking stuff, are different. ...
|
by: David Thielen |
last post by:
You would think this list exists but I can’t find it anywhere. Is this the
correct order for these events to be called?
Global.asax Application_Start (only once)
Global.asax Application_Init...
|
by: Elmo Watson |
last post by:
I previously had a project working, in which the Gridview was populated by a
DataSet - then, with a DropDownlist in one of the columns, using the
OnrowDataBound event, to populate the DDL with the...
|
by: Daz |
last post by:
Hello everyone. I am sure the answer to my question is simple, but I
can't seem to dynamically add an onClick event to my script.
I have a table which is generated dynamically, I am just...
|
by: John Kotuby |
last post by:
Hi guys,
I am converting a rather complicated database driven Web application from
classic ASP to ASP.NET 2.0 using VB 2005 as the programming language. The
original ASP application works quite...
|
by: Tanzen |
last post by:
I'm working in visual studio 2005 trying to learn visual basic. Having
come from an VB for Access background, I'm finding it a big learning
curve. I have been working through several e-books which...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |