View Single Post
  #3 (permalink)  
Old March 3rd, 2010, 12:14 AM
PeterPeiGuo PeterPeiGuo is offline
Friend of Wrox
 
Join Date: Dec 2008
Location: , , .
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default

Well, it's true that his code is longer than needed, but he does not need an index to access the element, as his foreach loop already gave him all the elements one at a time. All he needs to do is slightly modify one line of his code, see my comment in the code:
Code:
            Random r = new Random();     
            int i = 0;       
            int[] rndm = new int[100];       

            for (i = 0; i < 100; i++)    
            {
                rndm[i] = (int)r.Next(1, 100);     

            }

            foreach (int n in rndm)
            {
                listBox1.Items.Add(n.ToString());    //insert n, not rndm  
            }


And it is true that the code can be largely simplified:
Code:
            Random r = new Random();     
            for (int i = 0; i < 100; i++)    
            {
                listBox1.Items.Add(r.Next(1, 100)); 
            }

Last edited by PeterPeiGuo; March 3rd, 2010 at 12:42 AM..