Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > BOOK: Beginning Visual C#
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 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 June 19th, 2005, 03:56 AM
Registered User
Join Date: Jun 2005
Location: Shipston-on-stour, Warwickshire, United Kingdom.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default chapter 7, exercise 4, what is suppose to happen


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.

Reply With Quote
  #2 (permalink)  
Old June 20th, 2005, 02:36 PM
Authorized User
Join Date: Jul 2004
Location: Saint Peters, Missouri, USA.
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts

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.
Reply With Quote
  #3 (permalink)  
Old June 20th, 2005, 02:48 PM
Registered User
Join Date: Jun 2005
Location: Shipston-on-stour, Warwickshire, United Kingdom.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts


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.


Reply With Quote

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
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

All times are GMT -4. The time now is 08:24 AM.

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