-1

I have this code on VB. I get no syntax error but when running and testing the numbers i get "number is not valid" or a wrong answer. Would you please help me what is wrong with this code? Thank you.

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    TextBox2.Text = ""
    Dim Num As Integer
    Dim str As String
    str = TextBox1.Text
    Num = Integer.Parse(str)


    If Num >= 100 And Num <= 199 Then

        TextBox2.Text = "Basement"

    ElseIf Num >= 200 And Num <= 500 And Num >= 900 Then

        TextBox2.Text = "Main Floor"

    ElseIf Num <= 700 And Num >= 501 And Num >= 750 And Num <= 900 Then

        TextBox2.Text = "Upper Floor"

    ElseIf Num <= 750 And Num >= 700 Then

        TextBox2.Text = "Archives"
    Else
        TextBox2.Text = "Number is not valid"
    End If

End Sub
5
  • 7
    VS comes with a debugger which works great to solve this sort of problem. You should also take the tour Mar 20, 2017 at 15:22
  • 4
    Don't assume you have a valid number in your text box. Use Integer.TryParse instead.
    – LarsTech
    Mar 20, 2017 at 15:23
  • 3
    Rubber duck debugging Mar 20, 2017 at 15:31
  • And Num <= 500 And Num >= 900 how can this ever be the case?
    – djv
    Mar 20, 2017 at 16:08
  • Thank you guys for your comments, I really did not check the math part, when I read your comments I got what I have done. Because this is what the professor asked us to do. This is for a small library that shows where are the books when you enter the books' number. So he just confused us with this. I think I have to put only one condition for each if so it works. Thanks a lot.
    – yas
    Mar 21, 2017 at 21:31

2 Answers 2

6

This is impossible

ElseIf Num >= 200 And Num <= 500 And Num >= 900 Then

Here you are asking that a number be greater-equal than 200 (ok), lower-equal than 500 (ok) AND greater-equal than 900 (not ok since you already asked for a number lower-equal than 500). A number can't both be lower than 500 and greater than 900.

Without having the requirements, it's hard to know what your if statement should look like. I would suggest you run through your code manually "on paper" or start with one if statement at a time instead of writing all of them.

1
  • 5
    also ElseIf Num <= 700 And Num >= 501 And Num >= 750 And Num <= 900 Then Mar 20, 2017 at 15:29
2

I would not mix to many checks in one line as it can get confusing.

Do somthing like this:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    TextBox2.Text = ""
    Dim Num As Integer
    Dim str As String
    str = TextBox1.Text
    Num = Integer.Parse(str)

    If Num >= 100 And Num <= 199 Then
        TextBox2.Text = "Basement"
    ElseIf Num >= 200 And Num <= 500 Then
        TextBox2.Text = "Main Floor"
    ElseIf Num >= 501 And Num <= 700 then
        TextBox2.Text = "Upper Floor"
    ElseIf Num ..... and so on
    Else
        TextBox2.Text = "Number is not valid"
    End If

End Sub

Also it's good practice to name your TextBox controls and any other controls:

  • textbox1 could be something like txtfloornumber
  • textbox2 could be something like txtfloorname

This makes coming back to your code later in life easier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.