Question How do I ensure entered data is numerical?

Adam

New member
Joined
Apr 9, 2010
Messages
1
Location
UK
Programming Experience
1-3
Hello,

How could I ensure that the data entered in the following code is numerical?

VB.NET:
        Console.Write(CurrentPlayer & " enter x coordinate: ")
        XCoordinate = Console.ReadLine()
        Console.Write(CurrentPlayer & " enter y coordinate: ")
        YCoordinate = Console.ReadLine()
        Console.WriteLine()

Sorry if it's a bit of a noobish question, it's a rather new language to me, lol.

Many Thanks,

Adam.
 
VB.NET:
Dim number As Integer
If Integer.TryParse(xCoord, number) Then 
    'String input is a Integer and the numeric value is now in 'number' variable.
End If
 
You can use IsNumeric Function

This function return a boolean value.if the string is number then return true or else false

Put your string into ( )

For Ex:
If IsNumeric("Rad") = True Then....
 
You can use IsNumeric Function

This function return a boolean value.if the string is number then return true or else false

Put your string into ( )

For Ex:
If IsNumeric("Rad") = True Then....
Next you need to convert the String value to a numeric value... why not just use the single TryParse call that both validate and converts when possible?
 
It is obvious Adam needs to convert the string input to numeric data, so your IsNumeric suggestion falls short.
Also, Val function returns a Double, so if you need Integer you must convert it again.
 
Nope, if you use it with a String value argument you get a Double: Val Function
And as I said, if you require an Integer value you must convert the Double value to Integer value, adding yet one more operation. All primitive types has a TryParse representation, so this is win-win situation if you want to do what is asked about in this thread.
 
Back
Top