novice question

G

gordon

Hi,

i am learning c# at home in my spare time (what a life), and i am working
through some examples. I run the following code and i get the following
error when the if condition is not met.
An unhandled exception of type System.FormatException' occurred in
mscorlib.dll

string charval="Hello.";

string pharVal="How are you?";

string input;

try

{

Console.WriteLine("Hey,,,,, {0} {1}",pharVal,charval);

input=Console.ReadLine();

if (input=="1")

Console.WriteLine("Hello {0}", input);

else

Console.WriteLine("Back again? {0)", input);

}

finally

{

}

Sorry if this is a bit too basic - but i just can't work out why the value
of 2 is not acceptable.
 
G

Guest

Gordon,

Don't worry about posting basic questions, there is always someone who will
be happy to help. This is an easy one to miss - in the WriteLine after the
else, the format specifier should be {0}, not {0) !
 
G

Guest

it seems you have an incorrect bracket in your second statement

Console.WriteLine("Back again? {0)", input);
should be
Console.WriteLine("Back again? {0}", input);

hence the change of ')' to '}'
 
J

Jon Skeet [C# MVP]

gordon said:
i am learning c# at home in my spare time (what a life), and i am working
through some examples. I run the following code and i get the following
error when the if condition is not met.
An unhandled exception of type System.FormatException' occurred in
mscorlib.dll

The problem is in this line:
Console.WriteLine("Back again? {0)", input);

You meant {0}, not {0)

Jon
 
G

gordon

Yes of course Jon - thanks - i didnt pick that up.

I wish the debugger was more descriptive.

Doug
 
B

Bill Butler

gordon said:
Yes of course Jon - thanks - i didnt pick that up.

I wish the debugger was more descriptive.

For future reference.
Any time You get a System.FormatException start looking at your format string.
Unfortunately, it is not validated at compile time.
I realize that the format string may not be known 'til run time, but in general, the string is a
string literal and COULD easily be validated.

Oh well
Bill
 

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