Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > BOOK: Beginning Visual C#
|
BOOK: Beginning Visual C#
This is the forum to discuss the Wrox book Beginning Visual C#, Revised Edition of Beginning C# for .NET v1.0 by Karli Watson, David Espinosa, Zach Greenvoss, Jacob Hammer Pedersen, Christian Nagel, Jon D. Reid, Matthew Reynolds, Morgan Skinner, Eric White; ISBN: 9780764543821
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 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 June 19th, 2005, 03:56 AM
Registered User
 
Join Date: Jun 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default chapter 7, exercise 4, what is suppose to happen

Hi,

I have done exercise 4 in chapter 7 (virtually identical to the post by seblake in this forum).

However when the line:
myDirection = checked((orientation)myByte)
... is hit it WORKS!

I thought that the whole point of using "checked" like this (as the book itself tells you to do in the exercise text) that an OverflowException would occur if you entered a value that was not in the range 1 to 4 (in other words north = 1 etc...) of the orientation enum.

I have to admit I am using .NET v1.1 and this is a v1.0 book, but I can't believe this behaviour has changed.

Can anybody confirm that they thought the same would happen or am I missing something?

As far as I can see the line of code:
    myDirection = checked((orientation)myByte)
..is completely pointless since by the time we get to this line we know that we have a valid byte from the Console.ReadLine(), so doing the checked conversion does absolutely nothing for us since it allows values outside the orientation enum range.

Bye,
Peter
 
Old June 20th, 2005, 02:36 PM
Authorized User
 
Join Date: Jul 2004
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

First, if you have a byte type defined for myByte, there is no type conversion for checked() to test because the enum underlying type is also byte. In the example that you reference, I used a string data type value which had to be converted to byte.

Second, the enumeration class has no method for range analysis -- your code has to provide for any testing and error handling. Simply casting a byte value that was not included within the enumeration does not raise an error on its own.
 
Old June 20th, 2005, 02:48 PM
Registered User
 
Join Date: Jun 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

Thanks for responding; however I don't see how your code of:

orientation myDirection;
Console.WriteLine("Enter a whole number to consider:");
myDirection = checked((orientation)Convert.ToByte(Console.ReadLi ne()));

is operating any differently to my code of:

byte myByte;
orientation myDirection
Console.WriteLine("Enter a small number:");
myByte = Convert.ToByte(Console.ReadLine());
myDirection = checked((orientation)myByte);

The only difference as far as I can see is that your doing the read, convert to byte, checked convert to orientation in one line of code whereas I am doing it in 2 lines. In fact I commented out the myByte stuff and replaced the checked conversion with your line of code and it still does not cause an OverflowException.

Therefore my original question stands, what was the authors point of telling use to use such syntax since it does absolutley nothing for us.

Regards,
Peter






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 3 exercise 4 walkamongus BOOK: Beginning JavaScript 3rd Ed. ISBN: 978-0-470-05151-1 0 September 10th, 2007 09:34 PM
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 3 Exercise 3 Matt WAXON BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 3 July 4th, 2005 02:19 AM





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