Why won't this work?

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

Ignacio Machin \( .NET/ C# MVP \)

Hi,

What it does?

I created a new console project, paste your code and does correctly display
the string, the only thing I changed was that I change the signature of
Main:
[STAThread]
static void Main(string[] args)

and added a Console.ReadLine() after the printout

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



using System;

namespace ConsoleApplication6
{
class Class1
{
enum Color
{
red,
white,
blue
}
[STAThread]
static void Main(string[] args)
{
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);
System.Console.ReadLine();
}

}
}
 
T

Tom Porterfield

Kim said:
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.

What does it do when you execute the program? I copied and pasted your
code directly into a console application. When running the behavior is
as follows:

- I am prompted with the message:
"Enter a value for a color: 0 = Red, 1 = White, 2 = Blue):"
- I enter 1
- The following text is output on the console:
Switched to White...

Color is white (1)
- The program ends

Based on the code that appears to be the correct behavior. What are you
expecting it to do?
 
P

Pratush Bajpai

Try running it with a ctrl-f5,
Then it'll pause at the console after execution.
 
K

Kim

Hi Tom,
thank you for replying.
When I run it - I get no response.
I am running it from the dos prompt
 
K

Kim

Hi,
I commented out all of your changes and it works.
question is - what is wrong with the one i posted originally.
it makes no sense.

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

What it does?

I created a new console project, paste your code and does correctly display
the string, the only thing I changed was that I change the signature of
Main:
[STAThread]
static void Main(string[] args)

and added a Console.ReadLine() after the printout

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



using System;

namespace ConsoleApplication6
{
class Class1
{
enum Color
{
red,
white,
blue
}
[STAThread]
static void Main(string[] args)
{
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);
System.Console.ReadLine();
}

}
}


Kim said:
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);
}
}
 
B

Bill Butler

Kim said:
Hi Tom,
thank you for replying.
When I run it - I get no response.
I am running it from the dos prompt

I copied your code int test.cs
compiled it from a DOS window (csc *.cs)
and ran it.

Here is the output when run 4 times
-----------------------------------------------------
C:\Code\dotnet\test>test
Enter a value for a color: 0 = Red, 1 = White, 2 = Blue):0

Switched to Red...

Color is red (0)

C:\Code\dotnet\test>test
Enter a value for a color: 0 = Red, 1 = White, 2 = Blue):1

Switched to White...

Color is white (1)

C:\Code\dotnet\test>test
Enter a value for a color: 0 = Red, 1 = White, 2 = Blue):2

Switched to Blue...

Color is blue (2)

C:\Code\dotnet\test>test
Enter a value for a color: 0 = Red, 1 = White, 2 = Blue):3

Switched to Default...

Color is 3 (3)

C:\Code\dotnet\test>

----------------------------------------------------------
Silly question:
Are you pressing ENTER after selecting a value?

Assuming that you are:
Try putting some Console.WriteLine statements in there to see if you can spot what is happening.


It should work

Bill
 
I

Ignacio Machin \( .NET/ C# MVP \)

hi

The only thing I changed was the signature of the Main method:
[STAThread]
static void Main(string[] args)

and included a ReadLine at the end.


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Kim said:
Hi,
I commented out all of your changes and it works.
question is - what is wrong with the one i posted originally.
it makes no sense.

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote
in message news:#[email protected]...
Hi,

What it does?

I created a new console project, paste your code and does correctly display
the string, the only thing I changed was that I change the signature of
Main:
[STAThread]
static void Main(string[] args)

and added a Console.ReadLine() after the printout

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



using System;

namespace ConsoleApplication6
{
class Class1
{
enum Color
{
red,
white,
blue
}
[STAThread]
static void Main(string[] args)
{
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);
System.Console.ReadLine();
}

}
}


Kim said:
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);
}
}
 
K

Kim

It is the strangest thing.
Yes I am hitting the enter key.
The sample off the CD works as it should.

I added Console.WriteLine("I am here");
as the first line of the program
after the Main() and still I get no response.

bizarro!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top