Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 16th, 2013, 10:19 PM
Authorized User
 
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;

}
 
Old June 17th, 2013, 12:08 AM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
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!)
 
Old June 17th, 2013, 12:33 PM
Authorized User
 
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.
 
Old June 17th, 2013, 12:36 PM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
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!)
 
Old June 17th, 2013, 02:09 PM
Authorized User
 
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.





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.