p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 (http://p2p.wrox.com/forumdisplay.php?f=409)
-   -   Chapter 8 exercise 3 (http://p2p.wrox.com/showthread.php?t=78417)

Will March 2nd, 2010 04:24 PM

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:

Code:

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..

DrPurdum March 2nd, 2010 11:33 PM

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.

PeterPeiGuo March 3rd, 2010 12:14 AM

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));
            }


Will March 3rd, 2010 05:06 AM

Quote:

lstDisplay.Items.Add(rndm[n].ToString()); //Add each item to the listBox.
I woke up this morning and before I read this realised I had done something like this, so thats good I suppose.

Quote:

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.
I find this with a lot of the code I am writing at the moment, I will look in the back of the book and there will be a much simpler way of achieving what I have done.

Like PeterPeiGuo's code which is a lot simpler.

DrPurdum March 3rd, 2010 10:27 AM

Indexing
 
PeterPeiGuo's code is correct and simplier. However, in my own defense, the purpose of the exercise was to get people to think about array indexing, Indeed, the exercise specifically asks for array indexing. That's why I framed it as a "pure" array rather than an array object, which Peter does.

Another thing to keep in mind: Microsoft provides language features in C# that are not always found in other languages (e.g., LINQ) and, if you end up programming professionally, you may find a certain language feature missing in your new development language. Java, for example, has a huge following and Microsoft has added some nifty features to C# to entice people away from Java to C#. That's one reason I tend to concentrate on the "core" language features in my books, rather than the odd features. True, I do cover most of these cool features, but I rarely use them in my consulting code. Why? Because oft times I have to port that code to another environment and language where that feature isn't available. About the only time I use an unusual language feature is when I can see that it offers a significant improvement in performance.

PeterPeiGuo March 3rd, 2010 10:49 AM

LINQ, entity framework
 
Agree with Jack, and I do recommend you to read about LINQ, and read a lot about it. Personally that's one of the best language features I have seen among all languages. If you care about database stuffs, read about entity framework.

Will March 3rd, 2010 04:39 PM

Quote:

PeterPeiGuo's code is correct and simplier. However, in my own defense, the purpose of the exercise was to get people to think about array indexing, Indeed, the exercise specifically asks for array indexing. That's why I framed it as a "pure" array rather than an array object, which Peter does.
All I meant was I often look at your solutions and they are much simpler than mine..

Mine always seem to take what I think are necessary steps but when I see a simpler version I realise how convoluted my solution was..

Quote:

Another thing to keep in mind: Microsoft provides language features in C# that are not always found in other languages (e.g., LINQ) and, if you end up programming professionally, you may find a certain language feature missing in your new development language.
Would you recommend looking at other books then and other languages as opposed to just focusing on Microsoft technologies?

Quote:

Agree with Jack, and I do recommend you to read about LINQ, and read a lot about it. Personally that's one of the best language features I have seen among all languages. If you care about database stuffs, read about entity framework.
I will check out LINQ when I have a better handle on the basics.

DrPurdum March 3rd, 2010 11:35 PM

It's always good to look at someone's else's solution to a given problem. Peter had a good solution...it's just our purposes were different. You're doing it right, however. ALWAYS try to solve an exercise on your own first, then look at the alternatives and ask why the two are different and which is better. In my mind, simpler is always better.

I do cover LINQ later in the book, as well as some other things unique to C#. I think Microsoft has great technologies and I use them whenever I can. However, I also look at other languages and continue to use them...Java would be a good second language for you if you want to do professional coding. Personally, I'm using C right now writing embedded systems code for microcontrollers...and having a blast doing it! I'm two years younger than dirt, but I still love doing something different. Always be open to new stuff...

Will March 4th, 2010 04:15 AM

Cool, thanks for your help and advice.


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

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