locked
value type detect RRS feed

  • Question

  • Is there any code in (visual basic) that detect what type of value(integer, string, double) entered in textbox?

    I made a calculator I want to show the user a message box when he enter a string instead of Double

    • Moved by litdev Tuesday, February 5, 2013 5:27 PM From Small Basic
    Tuesday, February 5, 2013 4:54 PM

Answers

  • Handle the TextBox Validating event and use Double.TryParse() to attempt convert the text into a double.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by AmeenSA Tuesday, February 5, 2013 6:14 PM
    Tuesday, February 5, 2013 5:52 PM

All replies

  • Handle the TextBox Validating event and use Double.TryParse() to attempt convert the text into a double.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by AmeenSA Tuesday, February 5, 2013 6:14 PM
    Tuesday, February 5, 2013 5:52 PM
  • The user can only enter a String in a TextBox.  Some strings can represent numbers, but in many cases they can represent more than one type of number. For example "12" could represent a Byte, Short, Integer or Long (or possible a Decimal, Single or Double), "12.5" could represent a Decimal,  Single or Double). If you want to pick one numeric type for a given string, you will need to come up with rules to decide which to pick when the answer could be ambiguous.

    On the other hand, if you just want to know if a given String could represent a particular type of number, you can use the TryParse method of the numeric data types. For example to see if the contents of TextBox1 can be converted to a Double:

    Dim num As Double
    If TryParse(TextBox1.Text, num) Then
        MessageBox.Show(TextBox1.Text & " can be converted to a Double with the value " & num.ToString)
    End If

    Tuesday, February 5, 2013 5:53 PM
  • Yes there is, it is called IsNumeric

    If an IsNumeric is a Double, a Decimal or a Integer you cannot see.

    Also be aware that every number which can be seen as a number (so also an exponent) is numeric.

    http://msdn.microsoft.com/en-us/library/6cd3f6w1(v=VS.90).aspx

     For Framework 4.5 it is placed in the information namespace

    http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.information.isnumeric.aspx


    Success
    Cor


    Tuesday, February 5, 2013 5:56 PM
  • you forget "Double"

    if double.tryparse()

    Tuesday, February 5, 2013 6:14 PM