K
Kim
Below is a program sample from the book "Teach Yourself the C# Language in
21 Days."
I've compiled the program and receieved no errors - but it does not do what
is expected.
I've compared it to the smae sample on the CD - and it appears to be exactly
the same - yet it does not do what it should.
When executed it should ask you to select a color - once you do the
appropriate message will display on the console.
Any help is appreciated.
Thank You in Advance.
Kim
//color.cs - using enumeration
// Note: entering a non-numeric number when running this program will cause
an exception to be thrown.
//.
//..........................................................................
.....................
using System;
class Colors
{
enum Color
{
red,
white,
blue
}
public static void Main()
{
string buffer;
Color myColor;
Console.Write("Enter a value for a color: 0 = Red, 1 = White, 2 = Blue):
");
buffer = Console.ReadLine();
myColor = (Color) Convert.ToInt32(buffer);
switch( myColor )
{
case Color.red:
System.Console.WriteLine("\nSwitched to Red...");
break;
case Color.white:
System.Console.WriteLine("\nSwitched to White...");
break;
case Color.blue:
System.Console.WriteLine("\nSwitched to Blue...");
break;
default:
System.Console.WriteLine("\nSwitched to Default...");
break;
}
System.Console.WriteLine("\nColor is {0} ({1})", myColor, (int) myColor);
}
}
21 Days."
I've compiled the program and receieved no errors - but it does not do what
is expected.
I've compared it to the smae sample on the CD - and it appears to be exactly
the same - yet it does not do what it should.
When executed it should ask you to select a color - once you do the
appropriate message will display on the console.
Any help is appreciated.
Thank You in Advance.
Kim
//color.cs - using enumeration
// Note: entering a non-numeric number when running this program will cause
an exception to be thrown.
//.
//..........................................................................
.....................
using System;
class Colors
{
enum Color
{
red,
white,
blue
}
public static void Main()
{
string buffer;
Color myColor;
Console.Write("Enter a value for a color: 0 = Red, 1 = White, 2 = Blue):
");
buffer = Console.ReadLine();
myColor = (Color) Convert.ToInt32(buffer);
switch( myColor )
{
case Color.red:
System.Console.WriteLine("\nSwitched to Red...");
break;
case Color.white:
System.Console.WriteLine("\nSwitched to White...");
break;
case Color.blue:
System.Console.WriteLine("\nSwitched to Blue...");
break;
default:
System.Console.WriteLine("\nSwitched to Default...");
break;
}
System.Console.WriteLine("\nColor is {0} ({1})", myColor, (int) myColor);
}
}