New coder question: problems with parsing string to int

  • Thread starter Thread starter CSharp-Jay
  • Start date Start date
C

CSharp-Jay

I am pretty new to csharp myself, but have been trying to do a few
things that I am running into problems with. I know there is a way
around it, but I just dont know what it is. I am hoping somebody here
can help me.

Here is a code example:

string sanswer = "";
int answer;

// have user choose an option
Console.WriteLine("\nPlease choose an option:");
Console.WriteLine("1. Yes");
Console.WriteLine("2. No");
sanswer = Console.ReadLine();

// sanity check sanswer and convert to int;

try
{
answer = int.Parse(sanswer);
}
catch
{
Console.WriteLine("There was an error! Please try again.");
Console.ReadKey();
Console.Clear();
Menu();
}

// this generates a compiler error when you use it
Console.WriteLine("You have chosen {0}", answer);

With this code, the compiler will return the error that I am
attempting to use an unassigned local variable 'answer'. But answer
NEEDS to be checked, else the user can type b as their choice to the
yes/no question. So, how do I get around this problem? - Jay
 
use;

int answer = 0;

If you look at the flow of code there is no guarantee that answer will have
a value set to it. If the .parse fails for example, "answer" is left
unassigned. By assigning it a value when you declare it you know it will
always be assigned something.

Thank you so much, that will work. A simple solution, but I finally
see it now.
 
Sounds like issues I was having with DateTime. Looks like you would want
Convert.ToInt() instead of Int.Parse(). I have the code below:

Event.Date="11/22/2008";

Then I have to put it inside a date field of some kind:
DateTime Date = Convert.ToDateTime(Event.Date);

HTH
 
Andy B said:
Sounds like issues I was having with DateTime. Looks like you would want
Convert.ToInt() instead of Int.Parse(). I have the code below:

Event.Date="11/22/2008";

Then I have to put it inside a date field of some kind:
DateTime Date = Convert.ToDateTime(Event.Date);

HTH

You might want to add a try-catch block around that. Change your regional
settings (e.g. to UK or Swedish) to see why...

/claes
 
Claes said:
You might want to add a try-catch block around that. Change your regional
settings (e.g. to UK or Swedish) to see why...

Heh - localization is such fun.

DateTime.ToString() can really bite you in the arse too; if you're using
something like DateTime.ToString("dd-MMM-yyyy") to push in to an SQL
query it all works wondefuly, right up until you run it on a machine
where the locale is set to somewhere that doesn't use European style
numbers.
 
if you're using something like DateTime.ToString("dd-MMM-yyyy")
to push in to an SQL query

Which is one of the many reasons why you shouldn't be using
concatenation to build SQL - parameters are safer, both from an i18n
angle, and from injection ("Bobby Tables", anyone?).

i.e. "... WHERE date = @date", and add a parameter with Name =
"@date", DbType = DbType.DateTime, Value = (whatever)

Marc
 
You meanhttp://xkcd.org/327/ ?

Indeed. Actually, I'm of the opinion that this link should be
"required reading" before we let anybody code against a database ;-p

Marc
 
Indeed. Actually, I'm of the opinion that this link should be
"required reading" before we let anybody code against a database ;-p

Yup. I had it pinned to my wall at my previous job, and will probably
do so again when I'm back in my permanent office.

There are few comics which capture an issue so perfectly (and
humorously).

Jon
 
Marc said:
Which is one of the many reasons why you shouldn't be using
concatenation to build SQL - parameters are safer, both from an i18n
angle, and from injection ("Bobby Tables", anyone?).

Alas, I know this. Unfortunately this is all inherited code.
Suggestions of re-writing and doing it properly are normally met with
responses indicating that it will take too much time. Where "too much
time" appears to be synonym for "any amount of time greater than 0".
 
Jon said:
Yup. I had it pinned to my wall at my previous job, and will probably
do so again when I'm back in my permanent office.

I think I might have to join you and put it up here.
 
Back
Top