Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer
This is the forum to discuss the Wrox book Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer by Rod Stephens; ISBN: 9780470596906
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 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 June 12th, 2013, 03:05 PM
Authorized User
Points: 179, Level: 3
Points: 179, Level: 3 Points: 179, Level: 3 Points: 179, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2013
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default Lesson 17 Try It

Ok, new day, new problem.

I worked through the Lesson 17 Try It, and set the Preferred Contact to 0, like this:

Code:
preferredMethodComboBox.SelectedIndex = 0;
When I ran it, I kept getting an error message that it was out of range.

I replaced the "0" with -1, 1 and 2, and kept getting the error.

Finally, I guessed it was something with the Properties of the preferredMethodComboBox. Comparing mine to the one in your book's downloadable example, I found that my combobox did not have any entries in its "Items".

I thought that was what the code was supposed to do ... initialize the combobox with those Items, and assign indexes to them.

After typing in "None, Email, Phone and Snailmail" into Edit Items for the combobox, the error messages went away and the solution worked.

So what is the purpose of this code? :

Code:
private enum ContactMethod
        {
            None = -1,
            Email = 0,
            Phone = 1,
            SnailMail = 2,
        }
I thought it was supposed to set the indexes for those selections, but None is still 0, Email is 1, Phone is 2, SnailMail is 3.

That is,

preferredMethodComboBox.SelectedIndex = 0;

brings up "None" when I thought it would be "Email"

Your sample code works the same way, 0 is None, not Email.

Last edited by John S; June 12th, 2013 at 03:59 PM..
  #2 (permalink)  
Old June 13th, 2013, 12:08 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

I don't know why it didn't work for you initially and then started working.

You're right that there's something wrong with the enum. The value -1 in a ComboBox means "no selection" so I think the idea was to have "none" not be an actual selection.

To make the values match the indexes of the choices in the ComboBox it should probably be:

Code:
        private enum ContactMethod
        {
            None = 0,
            Email = 1,
            Phone = 2,
            SnailMail = 3,
        }
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
  #3 (permalink)  
Old June 13th, 2013, 03:47 PM
Authorized User
Points: 179, Level: 3
Points: 179, Level: 3 Points: 179, Level: 3 Points: 179, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2013
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yeah that would make more sense.

I'm curious about entering any indexes at all in the enum. When I had the wrong ones in there, the project seemed to ignore them anyway.

The solution did not work originally, until I manually entered the enum Items into the Items editor of the combo box Properties.

I guess I was expecting the code for the enum to enter the values into the combo box, but it didn't work that way.
  #4 (permalink)  
Old June 14th, 2013, 08:55 AM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

The idea of the enums is to let your code refer to them by names that make sense. I could imagine that it might store values that don't match what's in the ComboBox. Internally a program treats an enum as an integer anyway and doesn't really care what it's textual value is.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
  #5 (permalink)  
Old June 14th, 2013, 03:05 PM
Authorized User
Points: 179, Level: 3
Points: 179, Level: 3 Points: 179, Level: 3 Points: 179, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2013
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm just a beginner at this, but it seems like a long way around for nothing.

Perhaps the purpose of the Try It is just to demonstrate how enums and structs work??

In the form, the user selects the preferred way to contact the person being added (None, Email, etc.) which the combobox stores as an integer.

But the code includes an enumerator that matches the integer from the combobox back to a string, so that the structure can store the string?

You not only have to type the choices into the combobox Items, but then also have to type them into the enum, and make sure to do so in the same order!

Why doesn't the structure just store the integer? The only purpose of the result generated by the combobox is to show the person's preferred contact when one uses the "Find"

Looks like a lot of work just to display a selection in a combobox
  #6 (permalink)  
Old June 14th, 2013, 06:43 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Yes and no. A lot of the Try Its are intended to give you practice with one or two specific concepts. Some also build on others so you need to learn one technique before moving on.

But even in professional programming, following certain standards makes your code much less buggy and bugs cost a huge amount to fix. So you learn certain patterns of design and development to make the code more readable and maintainable.

One of those "best practice" techniques is using enumerated types instead of "magic numbers." That makes your code more readable and easier to understand.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
  #7 (permalink)  
Old June 14th, 2013, 07:27 PM
Authorized User
Points: 179, Level: 3
Points: 179, Level: 3 Points: 179, Level: 3 Points: 179, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2013
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well that sounds like good advice.

I'm not sure, however, what you're referring to as magic numbers.

Can you give an example or two?

thanks
  #8 (permalink)  
Old June 14th, 2013, 09:27 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Like when a program lets 1 mean small, 2 mean medium, and 3 mean large. Then the code is full of 1s, 2s, and 3s so you can't keep track of what the numbers mean.

It's much better if the code uses an enum so it says "Small," "Medium," and "Large."
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
 


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
Lesson 17 alawode BOOK: Knight's Microsoft Business Intelligence 24-Hour Trainer 1 February 2nd, 2012 09:48 PM
Lesson 7 Joakim BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 1 November 30th, 2011 11:16 AM
Lesson 4 Femi Sonuga BOOK: Knight's Microsoft Business Intelligence 24-Hour Trainer 0 November 25th, 2011 10:25 AM
Lesson 9 MTMike BOOK: Knight's Microsoft Business Intelligence 24-Hour Trainer 3 November 3rd, 2011 01:51 PM
Lesson 4 - Try It brookesmith BOOK: JavaScript 24-Hour Trainer 2 September 15th, 2011 05:40 PM



All times are GMT -4. The time now is 09:35 PM.


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