vb.net - How do I ensure entered data is numerical?

Discussion in 'Programming' started by Anhalo, Apr 9, 2010.

  1. #1
    Hello,

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

    
            Console.Write(CurrentPlayer & " enter x coordinate: ")
            XCoordinate = Console.ReadLine()
            Console.Write(CurrentPlayer & " enter y coordinate: ")
            YCoordinate = Console.ReadLine()
            Console.WriteLine()
    
    Code (markup):
    Many Thanks,

    Anhalo.
     
    Anhalo, Apr 9, 2010 IP
  2. farooqaaa

    farooqaaa Well-Known Member

    Messages:
    2,330
    Likes Received:
    149
    Best Answers:
    0
    Trophy Points:
    180
    #2
    if IsNumeric(something) then
    // do something
    End If
    Code (markup):
    I don't have VB.NET installed at the moment. But that'll work.
     
    farooqaaa, Apr 9, 2010 IP
  3. Anhalo

    Anhalo Active Member

    Messages:
    454
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Thanks, I'll have to try that :)
     
    Anhalo, Apr 9, 2010 IP
  4. NeoCambell

    NeoCambell Peon

    Messages:
    456
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #4
    NeoCambell, Apr 14, 2010 IP
  5. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #5
    Generally you will want to use the TryParse method - so if its an interger, you would do:

    Dim testStr as string = "34"
    Dim val as Integer

    if (Integer.TryParse(testStr, val)) then
    ' we have a valid val casted as an int ready to go
    end if
     
    ccoonen, Apr 15, 2010 IP