Results 1 to 13 of 13

Thread: [RESOLVED] If statement not working

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2010
    Posts
    230

    Resolved [RESOLVED] If statement not working

    I have form1 and form2. On form 2 i have 2 combo boxes where the user is asked to make selections. once back on form1 i try to use those selections as part of an if statement but although i should be triggring them it does not. I cannot figure this out. I have set up test variables and msgboxes along the way to test this.

    The code excecutes. I have double checked the variables on form 2. They just do not trigger a response that will enter the if statements even though it looks like they should

    I have double checked that test1 and test 2 are grabbing the variables from form 2

    test1 is "Male"
    test2 is "10 - 19"

    When the code excecutes i never get to the msgbox("Age") line of code. I have also tried taking the spaces out of the strings with no luck as well.

    Code:
            Dim test1 As String = aselect.sex
            Dim test2 As String = aselect.age
    
            If aselect.age = "2 - 9" Then
                If aselect.sex = "Male" Then
                    If q > 5 And q < 40 Then
                        patient_frm.lthor_kypt = 1
                    End If
                    If q < 5 Or q > 40 Then
                        patient_frm.lthor_kypt = 2
                    End If
                End If
                If aselect.sex = "Female" Then
                    If q > 8 And q < 36 Then
                        patient_frm.lthor_kypt = 1
                    End If
                    If q < 8 Or q > 36 Then
                        patient_frm.lthor_kypt = 2
                    End If
                End If
            End If
    
            If aselect.age = "10 - 19" Then
                MsgBox("Age")
                If aselect.sex = "Male" Then
                    If q > 8 And q < 39 Then
                        patient_frm.lthor_kypt = 1
                    End If
                    If q < 8 Or q > 39 Then
                        patient_frm.lthor_kypt = 2
                    End If
                End If
                If aselect.sex = "Female" Then
                    If q > 11 And q < 41 Then
                        patient_frm.lthor_kypt = 1
                    End If
                    If q < 11 Or q > 41 Then
                        patient_frm.lthor_kypt = 2
                    End If
                End If
    
            End If

  2. #2
    Addicted Member
    Join Date
    Apr 2011
    Posts
    223

    Re: If statement not working

    For starters, your two main IF statements aren't going to return true. If you're looking for a range of numbers, you may want to write it as "If aselect.age >= 2 and aselect.age <= 9 then". Same goes for your second IF statement. When you put those numbers in quotations, it is treated as a String. You should be comparing a number to a number.

  3. #3
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: If statement not working

    Quote Originally Posted by Deslyxia View Post
    I have double checked that test1 and test 2 are grabbing the variables from form 2

    test1 is "Male"
    test2 is "10 - 19"

    When the code excecutes i never get to the msgbox("Age") line of code. I have also tried taking the spaces out of the strings with no luck as well.

    Code:
            
    
            If aselect.age = "10 - 19" Then
                MsgBox("Age")
          ...
            End If

    while test2 is indeed "10 - 19" , your aselect.age might not be. The only reason your msgbox is not shown is that aselect.age is not equal to the "10 - 19" string literal.
    Perhaps, you should also check your typing. (You do have option strict on, don't you?). A typo might also be the case.
    If you absolutely must compare string literals, try to hardcode them only in one place - use constants:

    Code:
    Const TEENAGE As String = "10 - 19"

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2010
    Posts
    230

    Re: If statement not working

    Let me see if i can clear some things up.

    while test2 is indeed "10 - 19" , your aselect.age might not be
    Test2 is actually being set by aselect.age this was a test just to make sure it was correct. And to take it one step further i tried substituting test2 in my if statement rather than aselect.age


    For starters, your two main IF statements aren't going to return true.
    In form2 once the user selects a selection from the combo box i set a public variable as such:

    Code:
        Public sex As String
        Public age As String
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            age = cmb_age.Text
            sex = cmb_sex.Text
            Dim ex As Boolean = True
    
    
            If String.IsNullOrEmpty(cmb_age.Text) Or String.IsNullOrEmpty(cmb_sex.Text) Then
                MsgBox("Please check your Selections")
                ex = False
            End If
    
            If ex = True Then
                Me.Visible = False
            End If
    
        End Sub
    As far as mistypes I went over that as well. This is the list of selections from the actual combo box:

    2 - 9
    10 - 19
    20 - 29
    30 - 39
    40 - 49
    50 - 59
    60 - 69
    70 - 79


    Im still at a loss. It really looks like it should work.

  5. #5
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Re: If statement not working

    Try replacing the combo values with plain numbers, such as:

    2
    10
    20
    30
    40
    50
    60
    70

    Also update all your code, such as the IF conditions and any other code to use these numbers. Then check the code and see if it works.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2010
    Posts
    230

    Re: If statement not working

    Try replacing the combo values with plain numbers
    I will try doing that as soon as I get home but I have a question. If i dont have the upper limits on the range how would i keep them from overlapping? Even if i type a number into the combo box it is still treating each line as a string right?

  7. #7
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: If statement not working

    Did you trace the program using (F10) ? Just to confirm that the execution is indeed hits that IF statements. Aside from that - use string constants. Assign it both to the combobox and use the same constand in the IF statement otherwise a single whitespace in the wrong place can return false.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Sep 2010
    Posts
    230

    Re: If statement not working

    Did you trace the program using (F10) ?
    I did trace it using and what i found was that i am just not triggering them. The code directly before and directly after the if statements excecutes as expected.

    Is there a possibility that the issue is a space at the END of the string? But if thats the case when i look at the value of test2 at excecution shouldnt it be

    "10 - 19 " instead of "10 - 19" ?

  9. #9
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: If statement not working

    I don't quite understand what you mean in your last post, yes "10 - 19 " would NOT be equal to "10 - 19"
    And if you trace the code using F10 the IF statement must be highlighted at all times, even if the condition is false. Does it happen?

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: If statement not working

    The most likely problem is that there is a hidden character or whitespace. It may not be visible, as some characters really aren't visible during debug. However, that's one good reason to re-think the design. Comparing strings is a pain, as you can see, but it is also relatively slow. Since the user is selecting these strings from a combobox, how about designing it such that the SelectedIndex (which is an integer) can be used rather than the SelectedItem (which is a string)?
    My usual boring signature: Nothing

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Sep 2010
    Posts
    230

    Re: If statement not working

    I appreciate all the help. It is working now. What i wound up doing was go back and remove the dashes from everything and replace them with to. So instead of 9 - 19 it read 9 to 19 .

    The second i did that everything worked perfectly.

    Again I appreciate the help and Shaggy that is an interesting point.

  12. #12
    Addicted Member
    Join Date
    Apr 2011
    Posts
    223

    Re: [RESOLVED] If statement not working

    That makes me think that somewhere it was literally doing 9 minus 19 instead of a string "9 - 19".

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Sep 2010
    Posts
    230

    Re: [RESOLVED] If statement not working

    Yeah i thought about that as well. I was just unable to find where it was doing it. It wasnt on form 2 because the temp variable i made came back looking correctly encapsulated by quotes. And the comparison in the If statement is also correctly encapsulated.

    It seemed wierd to me as well which is how it got posted here. But it is working now so I am happy.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width