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 January 6th, 2010, 05:05 PM
Registered User
 
Join Date: Jan 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Answer to Chapter 7, Exercise 4

Admittedly, I am a newbie programmer, although I have been dipping into code very subtely for the past ten years.
I am struggling somewhat with the provided answer (taken from the downloaded word doc on this site) to question 4 for chapter 7.
Not helped by the fact that having entered the suggested answer into my compiler, it doesn't compile, due to a big list of errors.
Oops!

This has been my first stumbling block so far. I hope I don't need to have this bit nailed, because I don't get it!

If anyone has coded a working answer to this question, I'd be more than happy to peruse it....
 
Old January 16th, 2010, 01:28 PM
Authorized User
 
Join Date: Nov 2009
Posts: 22
Thanks: 1
Thanked 3 Times in 3 Posts
Send a message via Yahoo to msherburne84
Default

Copy and past this into your program.cs file in your solution. This is how it should look. Just press F5 and it will pause for you to see the results. place a few break points in there to see how it works. On a side note it is always good to learn how to use exceptions and custom exceptions. One thing that I have learned is that for every line of code there are ten other lines of error checking

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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

        static void Main(string[] args)
        {
            
            orientation myDirection;            

            for (byte myByte = 2; myByte < 10; myByte++)
            {
                try
                {
                    myDirection = checked((orientation)myByte);
                    if ((myDirection < orientation.north) ||
                        (myDirection > orientation.west))
                    {
                        throw new ArgumentOutOfRangeException("myByte", myByte,
                           "Value must be between 1 and 4");
                    }
                }
                catch (ArgumentOutOfRangeException e)
                {
                    // If this section is reached then myByte < 1 or myByte > 4.
                    Console.WriteLine(e.Message);
                    Console.WriteLine("Assigning default value, orientation.north.");
                    myDirection = orientation.north;
                }

                Console.WriteLine("myDirection = {0}", myDirection);
               
            }
            Console.ReadKey();
        }

    }
}





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 12, Exercise 5 Answer cree8uk BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 3 June 5th, 2010 02:33 PM
Wrong answer to Exercise 4 in Chapter3 lvehe BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 3 May 23rd, 2010 01:17 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
The answer of Chapter 3,(5) wallhorse C# 2 June 30th, 2004 10:55 AM





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