user2666002
user2666002

Reputation: 11

If/else statement VB.Net

If txtNum1.Text <= 0 Or txtNum2.Text <= 0 Then
    lblResult.Text = "Result Error: Enter in a number graeter than zero"
End If

I am new to programming. I am trying to create an if/else statement so that if the number in either text box is less than or equal to 0 it will display an error message and not crash.

Upvotes: 1

Views: 4718

Answers (4)

Steve
Steve

Reputation: 216363

Use proper conversion from string to numbers

Dim res1 As Integer
Dim res2 as Integer

if Not Int.TryParse(txtNum1.Text, res1) then
   lblResult.Text = "Enter a valid first number "
   return
End If
if Not Int.TryParse(txtNum2.Text, res2) then
   lblResult.Text = "Enter a valid second number "
   return
End If

If res1 <= 0 OrElse res2 <= 0 Then
  lblResult.Text = "Result Error: Enter numbers greater than zero"
End If

You need to convert the user input to a numeric value. The Text property of a textbox is a string not a number. And if you want to convert it you should be prepared to receive bad inputs (like a non numeric value).

The best approach is to use Int.TryParse that try to convert the value typed by your user in a number and if it fails returns false. If successful the converted number will be found in the second argument.

Notice also that you should use OrElse instead of Or because the former use short-circuit evaluation

I wish to warn you about another pitfall that seems to be evident from the error message. The VB compiler tried to help you converting the two strings in numbers. This is very bad from my point of view. You should take the responsability to handle this kind of conversions disabling the automatic conversion of the compiler. Go to the properties of your project, page Compile and set the Option Strict to ON. In this way the compiler would stop this automatic conversion and signal as error the textBox1.Text <= 0

Upvotes: 2

Pacane
Pacane

Reputation: 21521

You have to Parse the number in the .Text property as an integer.

so your If statement would be something like

If Int32.Parse(txtNum1.Text) <= 0 ....

if you plan on reusing that value multiple times in your code, you can extract it in a variable.

Also, as pointed out in the comments, you should check for invalid numbers, you can do so, with Int32.TryParse(value, number). So then, if the TryParse(..) method returns false, you can handle the case.

To know exactly how this method works, you can read this

But to make it quick value is the string you want to parse and number is the integer value that is parsed out of the string. The method itself returns a boolean (true if it was successfully parse, and false otherwise)

Upvotes: 2

Gar
Gar

Reputation: 862

your comparison won't work normally, you are not using the same types (string vs integer) i'd rather use integer.tryParse

so the code becomes something like :

dim n1 as integer
dim n2 as integer
if integer.tryparse(txtNum1.Text,n1) and integer.tryparse(txtnum2.text,n2) then
    If n1 <= 0 Or n2 <= 0 Then
        lblResult.Text = "Result Error: Enter in a number graeter than zero"
    End If
else
    lblResult.Text = "please input numbers"
end if

Upvotes: 0

Fredou
Fredou

Reputation: 20140

something like this would be better,

you check if it is a int then check if it is zero or under

    Dim value1, value2 As Integer

    If not Integer.TryParse(txtNum1.text, value1) orelse value1 <= 0 OrElse not Integer.TryParse(txtNum2.text, value2) orelse value2 <= 0 Then
        lblResult.Text = "Result Error: Enter in a number graeter than zero"
    End If

Upvotes: 0

Related Questions