I am having trouble with this code and realizing that my flags are not work.
I am referring to a list box with US states inside.
See my code below and tell me what I am doing wrong. I can not output the state with first two letters.
A simple concept but I can not solve.
VB2010

Public Class frmStates

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        
        
        Dim letter As String = mtbFirstTwo.Text.ToUpper
        Dim Flag As Boolean = True
        Dim state As String = ""

        Dim first, mid, last As Integer
        first = 0
        last = CInt(lstStates.Items.Count) - 1

        Do While (Not Flag) And (first <= last)
            mid = CInt((first + last)) / 2
            Select Case CStr(lstStates.Items(mid)).Substring(0, 2).ToUpper
                Case letter
                    state = CStr(lstStates.Items(mid))
                    Flag = True
                Case Is > letter
                    last = mid - 1

                Case Is < letter
                    first = mid + 1

            End Select
        Loop
        If Flag Then
            txtOutput.Text = state & " begins with " & mtbFirstTwo.Text & "." 'can not get output
        Else

            txtOutput.Text = "No State begins with " & mtbFirstTwo.Text & "."
        End If
    
    End Sub

You need to initialize the variable "Flag" to False.

If you look at how a Do...While works, it is looking for the entire "While" condition to evaluate to "True". Since Flag = True, then (Not Flag) = False. And we know that your initial values for variable "first" is less than variable "last", so that evaluates to True. So from the truth tables, False AND True = False, therefore your entire condition statement evaluates to False, and the Do loop never executes.

Hope this helps!

You need to initialize the variable "Flag" to False.

If you look at how a Do...While works, it is looking for the entire "While" condition to evaluate to "True". Since Flag = True, then (Not Flag) = False. And we know that your initial values for variable "first" is less than variable "last", so that evaluates to True. So from the truth tables, False AND True = False, therefore your entire condition statement evaluates to False, and the Do loop never executes.

Hope this helps!

Thank you for this feedback. However, I changed my flag to false to execute the loop. However my variable 'states' does not seem to show as an output. Any ideas? The error shows that the value is null and been used before. Maybe it is my version of the program.

I don't see a variable named "states". Sounds like a different problem. Post your code and the error.

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Dim letter As String = txtFirstTwoLetters.Text.ToUpper
        Dim foundflag As Boolean = False
        Dim usstates As String
        Dim first, middle, last As Integer
        first = 0
        last = lstStates.Items.Count - 1
        Do While Not foundflag And (first <= last)
            middle = CInt((first + last) / 2)
            Select Case CStr(lstStates.Items(middle)).Substring(0, 2).ToUpper
                Case letter
                    usstates = CStr(lstStates.Items(middle))
                    foundflag = True
                Case Is > letter
                    last = middle - 1
                Case Is < letter
                    first = middle + 1

            End Select
        Loop
        If foundflag Then
            txtOutput.Text = usstates & " begins with " & txtFirstTwoLetters.Text & "."

        Else
            txtOutput.Text = "No state begins with " & txtFirstTwoLetters.Text & "."
        End If
    End Sub
End Class

I don't see a variable named "states". Sounds like a different problem. Post your code and the error.

I Have changed the variable to usstates, am I missing a list box setting.

Put your cursor on the Do While statement.
Press F9
Run the program.
When it stops, look a the values of the variables. Are they correct?
If not, check to code to see why not, and hit STOP
If so, < press F8, check variables, repeat > until you find the error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.