Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3
This is the forum to discuss the Wrox book Beginning C# 3.0 : An Introduction to Object Oriented Programming by Jack Purdum; ISBN: 9780470261293
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 2nd, 2010, 04:24 PM
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:

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..
__________________
Follow me on twitter.

Where I work.

Connect with me on LinkedIn

Blog
  #2 (permalink)  
Old March 2nd, 2010, 11:33 PM
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)
  #3 (permalink)  
Old March 3rd, 2010, 12:14 AM
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..
  #4 (permalink)  
Old March 3rd, 2010, 05:06 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default

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.
__________________
Follow me on twitter.

Where I work.

Connect with me on LinkedIn

Blog
  #5 (permalink)  
Old March 3rd, 2010, 10:27 AM
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 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.
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
  #6 (permalink)  
Old March 3rd, 2010, 10:49 AM
Friend of Wrox
 
Join Date: Dec 2008
Location: , , .
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default 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.
  #7 (permalink)  
Old March 3rd, 2010, 04:39 PM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default

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.
__________________
Follow me on twitter.

Where I work.

Connect with me on LinkedIn

Blog
  #8 (permalink)  
Old March 3rd, 2010, 11:35 PM
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

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...
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
  #9 (permalink)  
Old March 4th, 2010, 04:15 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default

Cool, thanks for your help and advice.
__________________
Follow me on twitter.

Where I work.

Connect with me on LinkedIn

Blog
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 8 exercise 1 Will BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 2 March 2nd, 2010 03:26 PM
Chapter 5 exercise 3 Will BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 2 September 27th, 2009 02:41 PM
Chapter 4 Exercise 4 Will BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 2 September 18th, 2009 11:08 AM
Chapter 5 - Exercise 1 scgtman BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 3 May 16th, 2006 08:10 PM
Chapter 4, Exercise 3 DRAYKKO BOOK: Beginning Java 2 3 July 9th, 2004 02:34 PM



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


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