[VB] i really need help finding the problem

Discussion in 'Mixed Languages' started by stevemk14ebr, Feb 22, 2011.

  1. stevemk14ebr

    stevemk14ebr MDL Senior Member

    Jun 23, 2010
    267
    48
    10
    #1 stevemk14ebr, Feb 22, 2011
    Last edited: Feb 22, 2011
    ok so i have a program that was writen in visualbasic 2010 exspress i am having a problem that when i select the name steve chris or mike and enter the age under 18 that the correct dialogue that i programed doesnt show up and i cant find the right "if" statement so that once i enter a name and age it doesnt show all the "elses" that are untrue due to one of them being true. here it is:

    Public Class frmmain
    Dim ageover18 As Boolean
    Dim other As Boolean
    Dim steve As Boolean
    Dim mike As Boolean
    Dim chris As Boolean
    Dim becky As Boolean
    Dim brian As Boolean
    Dim ageunder18 As Boolean




    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
    If txtbox.Text > 18 Then
    ageover18 = True
    Else : ageover18 = False
    End If
    If ComboBox1.SelectedIndex = 7 Then
    other = True
    Else : other = False
    End If
    If ComboBox1.SelectedIndex = 6 Then
    brian = True
    Else : brian = False
    End If
    If ComboBox1.SelectedIndex = 5 Then
    becky = True
    Else : becky = False
    End If
    If ComboBox1.SelectedIndex = 4 Then
    chris = True
    Else : chris = False
    End If
    If ComboBox1.SelectedIndex = 3 Then
    mike = True
    Else : mike = False
    End If
    If ComboBox1.SelectedIndex = 2 Then
    steve = True
    Else : steve = False
    End If
    If txtbox.Text < 18 Then
    ageunder18 = True
    Else : ageunder18 = False
    End If
    If steve And ageunder18 = True Then
    MessageBox.Show("Hello Steve Eckels excelent job at making me")
    End If
    If mike And ageunder18 = True Then
    MessageBox.Show("Hello Michael Eckels, How are you doing in karate")
    Else : MessageBox.Show("You are not welcomed")
    End If
    If chris And ageunder18 = True Then
    MessageBox.Show("Hello Christopher Eckels, have you beat your B.T.D.4 highscore yet")
    Else : MessageBox.Show("you are not welcomed")
    End If
    If becky And ageover18 = True Then
    MessageBox.Show("Hello Becky Eckels,your son wishes to know if he can have meetloaf tonight")
    Else : MessageBox.Show("you are not welcomed")
    End If
    If brian And ageover18 = True Then
    MessageBox.Show("Hello Brian Eckels, How was work today")
    Else : MessageBox.Show("you are not welcomed")
    End If
    If other And ageunder18 = True Then
    MessageBox.Show("you are welcomed child under 18")
    Else : MessageBox.Show("hello person over 18")
    End If
    End Sub
    End Class
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    420
    198
    10
    #2 Calistoga, Feb 23, 2011
    Last edited by a moderator: Apr 20, 2017
    I think this thread should be moved to the Mixed Languages subforum.

    It's been a few years since i touched VB.NET (can't stand the syntax), but see if this sub does it. I wrote it in notepad so I might have made errors.
    Code:
    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
            Dim age As Integer
    
            ' Check whether a valid number has been typed in the textbox and store it in the variable "age".
            If Not Integer.TryParse(txtbox.Text, age) Then
    
                MessageBox.Show("Please provide your age.")
    
                Return ' Invalid age, let's abort.
    
            End If
    
            If age >= 18 Then ' Age is higher or equal to 18.
    
                If ComboBox1.SelectedIndex = 6 Then
                    MessageBox.Show("Hello Brian Eckels, How was work today")
                ElseIf ComboBox1.SelectedIndex = 5 Then
                    MessageBox.Show("Hello Becky Eckels,your son wishes to know if he can have meetloaf tonight")
                ElseIf ComboBox1.SelectedIndex = 4 Then
                    MessageBox.Show("Hello Christopher Eckels, have you beat your B.T.D.4 highscore yet")
                ElseIf ComboBox1.SelectedIndex = 3 Then
                    MessageBox.Show("Hello Michael Eckels, How are you doing in karate")
                ElseIf ComboBox1.SelectedIndex = 2 Then
                    MessageBox.Show("Hello Steve Eckels excelent job at making me")
                End If
    
            Else ' Age is under 18
    
                If ComboBox1.SelectedIndex = 7 Then
                    MessageBox.Show("You are welcomed child under 18.")
                Else
                    MessageBox.Show("You are not welcome.")
                End If
    
            End If
    
        End Sub
    With this code, you don't need these declarations anymore
    Code:
    Dim ageover18 As Boolean
    Dim other As Boolean
    Dim steve As Boolean
    Dim mike As Boolean
    Dim chris As Boolean
    Dim becky As Boolean
    Dim brian As Boolean
    Dim ageunder18 As Boolean
     
  3. Stannieman

    Stannieman MDL Guru

    Sep 4, 2009
    2,232
    1,803
    90
    I can't read your code very well on my phone, but I see you're doing things like "if betty and ageover18 = true then"
    This should be "if betty = true and ageover18 = true"
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    5,891
    20,335
    180
    #4 Alphawaves, Feb 25, 2011
    Last edited by a moderator: Apr 20, 2017
    I tested Calistoga, i changed it a little for it to work as you not tested, what i can gather is he wanted Becky and Brian age +18, other -18:
    Code:
     Dim age As Integer
    
            ' Check whether a valid number has been typed in the textbox and store it in the variable "age".
            If Not Integer.TryParse(TextBox1.Text, age) Then
                MessageBox.Show("Please provide your age.")
                Return ' Invalid age, let's abort.
            End If
    
            ' Check name and age.
            If ComboBox1.Text.ToString = "Chris" And age <= 18 Then
                MessageBox.Show("Hello Christopher Eckels, have you beat your B.T.D.4 highscore yet")
            ElseIf ComboBox1.Text.ToString = "Mike" And age <= 18 Then
                MessageBox.Show("Hello Michael Eckels, How are you doing in karate")
            ElseIf ComboBox1.Text.ToString = "Steve" And age <= 18 Then
                MessageBox.Show("Hello Steve Eckels excelent job at making me")
            ElseIf ComboBox1.Text.ToString = "Brian" And age >= 18 Then
                MessageBox.Show("Hello Brian Eckels, How was work today")
            ElseIf ComboBox1.Text.ToString = "Becky" And age >= 18 Then
                MessageBox.Show("Hello Becky Eckels,your son wishes to know if he can have meetloaf tonight")
            Else
                MessageBox.Show("You are not welcome.")
            End If
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...