Home > Software engineering >  Recognize two or more digits
Recognize two or more digits

Time:08-14

How to make Visual Basic .NET recognize two or more digits instead of one? When I execute it, it only validates correctly in case both numbers are one digit.

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click

    Dim menorOigual As Boolean

    menorOigual = numero1.Text <= numero2.Text

    MsgBox(menorOigual)

End Sub

CodePudding user response:

By comparing the value of the Text properties, you're comparing strings, which is why it only works with single digits (because as a string "10" comes before "3", because each character is compared in turn). You need to convert the strings to a numeric type like integer, e.g.

Convert.ToInt32(numero1.Text)

For production code, you'll probably also need to validate the conversion, so see also:

Integer.TryParse
  • Related