 |
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

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

December 17th, 2010, 11:02 AM
|
 |
Wrox Author
|
|
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
|
|
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.
|
|

December 17th, 2010, 01:34 PM
|
|
Registered User
|
|
Join Date: Dec 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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);
}
}
}
-------------------------------------------------------------------------------
|
|

December 17th, 2010, 03:29 PM
|
 |
Wrox Author
|
|
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
|
|
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
|
|

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