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();
}
}
}