Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer
This is the forum to discuss the Wrox book Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer by Rod Stephens; ISBN: 9780470596906
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 16th, 2013, 10:19 PM
Authorized User
Points: 179, Level: 3
Points: 179, Level: 3 Points: 179, Level: 3 Points: 179, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2013
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default Lesson 19, FOREACH loops

This didn't work. I typed a bunch of numbers into the Items collection of the listbox, but they are seen as strings, right?

Do I need to convert each string to an integer somehow?

Or is the code supposed to cast the strings into integers?

I get an error "Specified cast is not valid. When casting from a number, the number must be less than infinity".

All of the items in my listbox are numbers less than infinity.

Code:
int total = 0;

foreach (int value in valuesListBox.Items)

{
total += value;

}
  #2 (permalink)  
Old June 17th, 2013, 12:08 AM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Quote:
This didn't work. I typed a bunch of numbers into the Items collection of the listbox, but they are seen as strings, right?

Do I need to convert each string to an integer somehow?
Yes. As it says in the middle of page 231, the values in the ListBox must be numbers. But as you note, values that you add to the ListBox through the IDE are strings. You have two choices.

1) You can write code to place numbers in the ListBox as in:

Code:
myListBox.Items.Add(1337);
The ListBox will display the value and make it look like text "1337" but it is actually a number.

2) Or you can make the code parse the values in the ListBox.

Quote:
Or is the code supposed to cast the strings into integers?
You need to parse the values like this:

Code:
int total = 0;
foreach (string value in valuesListBox.Items)
{
    total += int.Parse(value);
}
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
  #3 (permalink)  
Old June 17th, 2013, 12:33 PM
Authorized User
Points: 179, Level: 3
Points: 179, Level: 3 Points: 179, Level: 3 Points: 179, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2013
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, Parse is what I was trying to remember.

Wish the code in the book had been correct in the first place.
  #4 (permalink)  
Old June 17th, 2013, 12:36 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

The code in the book is correct. The text even says the values in the ListBox must be numbers not text.

The difference is that you wanted to load the values at design time and not have the program fill them in. That's a reasonable thing to do and might have been a good example but that's not what the example in the text does.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
  #5 (permalink)  
Old June 17th, 2013, 02:09 PM
Authorized User
Points: 179, Level: 3
Points: 179, Level: 3 Points: 179, Level: 3 Points: 179, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2013
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, if the code is correct, why did it not work?

Yes, I read the text about the "Listbox must contain integers ... ".

Your earlier post said that one way to load numbers is to use :

Code:
myListBox.Items.Add(1337);
which, in this case, would be :

Code:
valuesListBox.Items.Add(1337);
but where does it say that in the example in the book?

If the book had included that line, then the code would have worked.

But where would one put that line??

As the code includes a check to make sure the listbox is not empty, one could not include that line before the check, or that would negate the check.

If one adds it after the check, then the code stops at the messagebox.

That is why I assumed the numbers must be entered by the user, not by the code.

But how does the user enter numbers into a ListBox? Would not a textbox be required?

So I entered numbers(strings) via the listbox Properties, but couldn't remember how to convert them to integers.

Parse was the solution.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lesson 19, FOR loops John S BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 3 June 17th, 2013 12:41 PM
Lesson 19 Exercise 4 mpthemaster BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 3 June 11th, 2011 08:54 PM
break for-each loops, or limit amount of loops warhero XSLT 2 July 4th, 2007 02:18 AM
While loops and For loops in XSLT spencer.clark XSLT 1 August 5th, 2005 09:50 AM
for loops from within foreach richard.york Pro PHP 5 October 2nd, 2003 01:54 AM



All times are GMT -4. The time now is 02:07 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.