View Single Post
  #2 (permalink)  
Old March 2nd, 2010, 11:33 PM
DrPurdum DrPurdum is offline
Friend of Wrox
Points: 1,166, Level: 13
Points: 1,166, Level: 13 Points: 1,166, Level: 13 Points: 1,166, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2008
Location: Indianapolis, IN, USA.
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default

Take a look at your code in the first for loop:

rndm[i] = (int)r.Next(1, 100); //assign the next random number to rndm array.


You're stuffing the array correctly. Now look at your code in the foreach loop:

lstDisplay.Items.Add(rndm.ToString()); //Add each item to the listBox.


Where's the array index value?? The code you wrote is simply telling you the type of the rndm array. To index it, you'd need:

lstDisplay.Items.Add(rndm[n].ToString()); //Add each item to the listBox.


Indeed, there is really no reason to use a collection to do this. Simply use a regular for loop and index it directly. I think you'd also find this easier to read and understand six months from now, and that's always a good idea. Simple is best.
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)