Check if type is numeric or not?

I have a question. In trying to do an if statement on a value thats suppose to be numeric, how do I test for that condition

Like in  a string test:

If mystring <> "" then


I want to accomplsih the same with an integer or double to see if it has a number assigned to it or not.


Thanks,


Tom




Answer this question

Check if type is numeric or not?

  • Lester Leonard Go

    Integers and Doubles always have a value - the default is 0.

    If you have a string and want to check if it represents a number, use Integer.TryParse or Double.TryParse. If the string is a number, that value will be assigned to the out parameter.

  • Stephen Weatherford MS

    Well I am basically just assinging an integer to a column index in a grid, so even the default 0 is available. I was just thinking there must be a way to check if my integer got assigned yet. But knowing it should be greater than 0(Col index) then I should be able to just place a check to see if myinteger > 0 That should work I think.


    Thanks.


  • Julie Brazier

    In .NET 2.0, you can use Nullable(Of Int). If you don't assign a value to that, if will return null (Nothing in VB).

    Otherwise, you could override the default value by declaring your variable as Dim x As Integer = -1

  • rfolk73

    Are you asking how to check if a number has anything but zero in it

    As numbers (integers, doubles, floats, etc) are value types, they always have a value assigned to them.

    For example, the number variable below has the default value of 0 and hence the first message box will be displayed.


    Dim number as Integer;

    If number = 0 Then
       MessageBox.Show("number is zero!.")
    Else     
       MessageBox.Show("number is not zero!.")
    End if

     








  • lauch

    Vikram,

    Good point, just fixed it. That will teach me for typing code without testing it.

  • coozdrm

    Hi,

    You seem to have mixed up C# and VB in the code snippet!
    It should be as follows in VB:


    Dim number As Integer
    If (number = 0) Then
       MessageBox.Show("number is zero!.")
    Else
       MessageBox.Show("number is not zero!.")
    End If

     


    Regards,
    Vikram



  • Check if type is numeric or not?