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 December 16th, 2010, 09:08 PM
Registered User
 
Join Date: Dec 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Code Question

Greetings,

I'm currently learning C# and chose this book to be my first exposure to learning it. I'm currently at the end of Lesson 4 and have run into what I believe to be code misprints or errors. Since I'm just starting out with this, I wanted to run it by Mr. Stephens (or anyone else) to verify since I did not see this in the Errata section of the book's Wrox web page.

In Lesson 4, page 53, Step-by-Step section, #1: There are two "1." and I'm referring to the second "1." at the very bottom of the page. The two lines of example code with "private void hbarRed_Scroll..." and "lblRed.Text=hscrRed.Value.ToString();" that continues on page 54 are my concern.

It think these pieces of code are an error since VS2010 flags errors with it. When I compare the book's code to the downloadable code, it's completely different and there's no trace of the code I'm flagging.

I've noticed these types of issues in previous chapters, as well, and it's been costing me hours of time trying to figure things out, since I'm new to C#.

Am I correct about this problem?

Thank you.
  #2 (permalink)  
Old December 17th, 2010, 11:02 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

Sorry about that. In response to technical editor comments, the names of the variables got changed. Unfortunately it looks like we didn't find all of them in the book. For example, here lblRed got renamed to redLabel so it appears as lblRed in the book but redLabel in the code.

You can fix the problem by using whichever names you give the controls when you build the form. For example, if you named the label to display the red values redLabel, use that in the code not lblRed.

Sorry this happened. I'll post a note on the book's web page.

If you have other problems, please let me know so you don't have to waste too much of your time on issues like this.
__________________
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 December 17th, 2010, 01:34 PM
Registered User
 
Join Date: Dec 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you, Mr. Stephens.

As I mentioned, I'm completely new to C# (and programming) and this is my first book. I realized I could change the names of controls, but since I'm so new to this, I wanted to stick to what you did, so I wouldn't go too far into the bushes. Fortunately, the downloadable code has been working in all examples.

If I run into other errors, I'll post them for you to verify. I'd also appreciate if you give the book another look to find errors and post on the site, so that book owners are aware before pounding our heads into the desk.

While I was working on the ColorSample in Lesson 4 yesterday, I tried something that I'd like your comments on to see if I'm thinking along the right lines.

After I comprehended your code in the ColorSample, I realized the code could be done differently and it also worked. It's not as compact and I don't know if this would be considered done correctly since I'm too new to programming.

ColorSamples - Form1.cs code segment I re-jiggered -------------------------------------------

namespace ColorSamples
{
publicpartialclassForm1 : Form
{
public Form1()
{
InitializeComponent();
}
privatevoid redHScrollBar_Scroll(object sender, ScrollEventArgs e)
{
redLabel.Text = redHScrollBar.Value.ToString();
sampleLabel.BackColor =
Color.FromArgb(
255,
redHScrollBar.Value,
greenHScrollBar.Value,
blueHScrollBar.Value);

}
privatevoid greenHScrollBar_Scroll(object sender, ScrollEventArgs e)
{
greenLabel.Text = greenHScrollBar.Value.ToString();
sampleLabel.BackColor =
Color.FromArgb(
255,
redHScrollBar.Value,
greenHScrollBar.Value,
blueHScrollBar.Value);
}
privatevoid blueHScrollBar_Scroll(object sender, ScrollEventArgs e)
{
blueLabel.Text = blueHScrollBar.Value.ToString();
sampleLabel.BackColor =
Color.FromArgb(
255,
redHScrollBar.Value,
greenHScrollBar.Value,
blueHScrollBar.Value);
}

}
 
}
 
-------------------------------------------------------------------------------
  #4 (permalink)  
Old December 17th, 2010, 03:29 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

There are two good points here. First, in any programming problem there are practically an infinite number of possible solutions that will all work. Your solution is just fine and looks like a good solution. It's what you get if you double-click on the scroll bars separately.

The second point is one of minimizing code and reusing code. The solution I used made all three scroll bars refer to the same code. Later if there's a bug or I want to make an enhancement, I only need to change the code in one place. That means, for example, I can't make the change for the red and green scroll bars and forget to make the same change to the blue scroll bar.

However, my solution requires the program to set all three labels any time you change any of the scroll bars while yours only sets the label for the color that changed. In that respect, your solution is faster because the computer performs a tiny bit less work. (We're talking about tiny amounts of time, though, for this simple program.)

So the final answer of which is better is largely a matter of style. I prefer the shared code approach but I would certainly give your version full marks!

Rod
__________________
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 December 17th, 2010, 04:15 PM
Registered User
 
Join Date: Dec 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for your response.

That's how I also interpreted the differences between the two ways. Initially, I was confused by why all the event handlers were under
privatevoid redHScrollBar_Scroll(object sender, ScrollEventArgs e)

I could understand why it was that way, but I couldn't understand how it worked that way. After running it in my head for awhile, I finally understood the how and saw what you were doing. That's when I thought about separating it all out to see if it would work that way too and it did.

Onward to Lesson 5 and beyond today!
 


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
Question on hastable code petfrog BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 2 February 22nd, 2008 05:15 PM
Question on reusing code billb VBScript 0 August 18th, 2006 10:39 AM
Answer this code question jaimini VB.NET 2002/2003 Basics 3 November 27th, 2004 07:10 AM



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


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