View Single Post
  #1 (permalink)  
Old March 2nd, 2010, 04:24 PM
Will Will is offline
Friend of Wrox
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default Chapter 8 exercise 3

Ok now I am looking at exercise three and I thought I had this one when I read it but apparently not..

I am getting stuck on outputting the values to the ListBox. I have this code in the load event:

 Random r = new Random();    //Declare and initalise the Random class.
        int i = 0;      //Counter for the for loop.
        int[] rndm = new int[100];      //Declare and initialise the array to hold the random numbers.

        for (i = 0; i < 100; i++)   //Iterate 100 times.
            rndm[i] = (int)r.Next(1, 100);     //assign the next random number to rndm array.

        foreach (int n in rndm)
            lstDisplay.Items.Add(rndm.ToString());     //Add each item to the listBox.
I have stepped through the code and I can see that by the time I reach the foreach loop the rndm array is filled with 100 random ints 0 - 99.

What then happens is the foreach loop adds them to the ListBox but when they are displayed they just display as : System.Int32[] instead of the number value.

I am assuming this is because the text property for each item in the listview
is blank?

Not really sure where I have gone wrong here, when I wrote it I was pretty confident it would work..
Follow me on twitter.

Where I work.

Connect with me on LinkedIn