Wrox Programmer Forums
BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4
This is the forum to discuss the Wrox book Beginning Microsoft Visual C# 2008 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, Eric White; ISBN: 9780470191354
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 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
Old September 27th, 2009, 09:30 AM
Friend of Wrox
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Question Chapter 5 exercise 3

I am confused about a few things regarding exercise 3 of chapter 5.

The code from the book of application is here:

namespace ConsoleApplication1
    enum orientation : byte
        north = 1,
        east = 2,
        south = 3,
        west = 4,

    struct route
        public orientation direction;
        public double distance;

    class Program
        static void Main(string[] args)
            route myRoute;
            int myDirection = -1;
            double myDistance;
            Console.WriteLine("1) North\n2)East\n3)South\n4) West");
                Console.WriteLine("Select a direction:");
                myDirection = Convert.ToInt32(Console.ReadLine());
            while ((myDirection < 1) || (myDirection > 4));
            Console.WriteLine("Input a distance:");
            myDistance = Convert.ToDouble(Console.ReadLine());
            myRoute.direction = (orientation)myDirection;
            myRoute.distance = myDistance;
            Console.WriteLine("myRoute specifies a direction of {0} and a " +
                "distance of {1}", myRoute.direction, myRoute.distance);
However the book doesn't explain a few of the things to do with this which I do not understand.

The first part is this line:

int myDirection = -1;
declaring myDirection as an int and giving it a value of -1, however when I change this number to 0 or 7 or 3 I still get the application to function in the same way. Is this number value an x number?

There is a do while loop in there which acts as validation ensuring that myDirection can only be a value of 1-4 which is why I think that -1 is an x number but I want to check as they don't specify.

Because if it is then shouldn't you just have:

int myDirection;
as this seems to work as well?
Old September 27th, 2009, 01:51 PM
Imar's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts

Hi Will,

Take a look at this:

myDirection = Convert.ToInt32(Console.ReadLine());

myDirection is assigned whatever you enter at the console. That means its initial value is never used and as such is indeed useless. -1, 0 (using just int myDirection;) or 7425483 all would have had the same effect.

-1 is often used as a "magic number" to indicate that a value hasn't been set yet. As such, I think this is a preference instead of a necessity.


Imar Spaanjaars
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Old September 27th, 2009, 02:41 PM
Friend of Wrox
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts

Cheers Imar,

I was confused because up until this point the book has (I believe) left variables which don't get values until user input as int myDirection; which threw me a bit when I saw -1 as I could not figure out why they would do that.

As you say I can see it makes sense and comes down to personal preference like you say.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 5 Exercise 2 diango BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 4 February 1st, 2011 03:24 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 3 - Exercise 3 AndyN BOOK: Beginning Cryptography with Java 3 August 16th, 2006 03:09 PM
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 8, Exercise 4 cjo BOOK: Beginning ASP.NET 1.0 0 November 3rd, 2003 02:26 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.