Subject: Errata - Chapter 9, Page 296, C#
Posted By: RobC Post Date: 8/28/2006 10:01:12 AM
Near the bottom of the page, there's this line of code:

Label1.Text = Convert.ToInt32(TextBox1.Text) + Convert.ToInt32(TextBox.Text);

First, (TextBox.Text) should be (TextBox2.Text). Second, this line will give a compile error:
quote:

Cannot implicitly convert type 'int' to 'string'


In order for this to work, the numbers have to be converted back to a string.

Also in Chapter 9, when I press F5 or Ctrl + F5 to run the Begin\Chapter09\ web site, I get a compile error:
quote:

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.

Requested URL: /Chapter09/ForEach.aspx


There are a lot of errors in this book. It really should be proofed and the code tested more thoroughly.
Reply By: DaveSussman Reply Date: 8/30/2006 2:51:35 AM
Rob

The first is a simple typo and should be, as you say, TextBox2.Text.

The second is more interesting and shows a) a major difference between VB and C#, and b) I didn't check the code (more on that later). The line should be:

Label1.Text = (Convert.ToInt32(TextBox1.Text) + Convert.ToInt32(TextBox2.Text)).ToString();

This adds the two numbers together and then converts them back into a string to display.

The error here is completely mine and is interesting. In VB you don't need to convert the two numbers back to a string - it is implicitly done for you. When I converted this chapter from VB I completely forgot this and didn't try the code becuase it wasn't going to be part of the code that finally shipped. An oversight and I should have been more careful; I can only apologise.

Dave


Go to topic 48972

Return to index page 190
Return to index page 189
Return to index page 188
Return to index page 187
Return to index page 186
Return to index page 185
Return to index page 184
Return to index page 183
Return to index page 182
Return to index page 181