validating datatype (newbie question)

J

Jack Fu

I want to make sure the user enters a number (rather than

a letter, for example). This is the way I am doing it. There

must be a better, more efficient way. Can someone please

tell me the better way? Thanks in advance!



Jack





Console.Write("\nEnter the ID number : ");

tempInput = Console.ReadLine();

try

{

checked

{

valueID = int.Parse(tempInput);

}

}

catch (Exception e)

// I don't know why this word (catch) gets squiggly line.

// It compiles and runs ok.

{

Console.WriteLine(" You must enter a number for the ID.");

goto WRITE_MENU;

}

Inventory[recordCount].ID = valueID;
 
C

Craig Wagner

[Reply posted to group and cc: to author as a courtesy (if no 'spam guard')]
Please do not reply directly to the e-mail

You could use Double.TryParse() as an alternative, but if it returns true then
you have to do a Parse operation to turn it into a number.

If you put your pointer over the squiggly lines it will tell you why they are
there. In this case it's a warning because the variable e is declared but not
used.
I want to make sure the user enters a number (rather than

a letter, for example). This is the way I am doing it. There

must be a better, more efficient way. Can someone please

tell me the better way? Thanks in advance!



Jack





Console.Write("\nEnter the ID number : ");

tempInput = Console.ReadLine();

try

{

checked

{

valueID = int.Parse(tempInput);

}

}

catch (Exception e)

// I don't know why this word (catch) gets squiggly line.

// It compiles and runs ok.

{

Console.WriteLine(" You must enter a number for the ID.");

goto WRITE_MENU;

}

Inventory[recordCount].ID = valueID;
 
J

Jack Fu

Craig: Thanks!

Craig Wagner said:
[Reply posted to group and cc: to author as a courtesy (if no 'spam guard')]
Please do not reply directly to the e-mail

You could use Double.TryParse() as an alternative, but if it returns true then
you have to do a Parse operation to turn it into a number.

If you put your pointer over the squiggly lines it will tell you why they are
there. In this case it's a warning because the variable e is declared but not
used.

I want to make sure the user enters a number (rather than

a letter, for example). This is the way I am doing it. There

must be a better, more efficient way. Can someone please

tell me the better way? Thanks in advance!



Jack





Console.Write("\nEnter the ID number : ");

tempInput = Console.ReadLine();

try

{

checked

{

valueID = int.Parse(tempInput);

}

}

catch (Exception e)

// I don't know why this word (catch) gets squiggly line.

// It compiles and runs ok.

{

Console.WriteLine(" You must enter a number for the ID.");

goto WRITE_MENU;

}

Inventory[recordCount].ID = valueID;

---
Craig Wagner, craig.wagner(at)attbi.com
Portland, OR

"Don't ban high-performance vehicles, ban low-performance drivers!"
 

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