Results 1 to 10 of 10

Thread: Quick question!

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2012
    Posts
    4

    Quick question!

    This is a program where you can enter 2 first letters of a state in the masked textbox, click the search button, and see the full name of the state displayed inside a regular textbox.
    I am having a little problem at the end getting txtOutput.Text to produce the name of the state selected. Here is what i have:
    Code:
    Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click
            Dim first, last, middle As Integer
            Dim flag As Boolean = False
            first = 0
            last = lstStates.Items.Count - 1
            Dim letters As String = mtbFirstTwoLetters.Text.ToUpper
            While first <= last
                middle = CInt((first + last) / 2)
                Dim state As String =
                    CStr(lstStates.Items.Item(middle)).ToUpper
                If state.StartsWith(letters) Then
                    flag = True
                    Exit While
                Else
                    Dim st As String = state.Substring(0, 2)
                    If letters > st Then
                        first = middle + 1
                    Else
                        last = middle - 1
                    End If
                End If
            End While
            If flag Then
                txtOutput.Text = 
            Else
                txtOutput.Text = "The State Is Not Found"
            End If
        End Sub
    End Class
    Last edited by Hack; Nov 15th, 2012 at 11:26 AM. Reason: Added Code Tags

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Quick question!

    Firstly, this question belongs in the VB.NET forum because it is not a general question. I have asked the mods to move it.

    Secondly, please use formatting tags when posting code snippets. Indented code is significantly easier to read, as you can see by comparing your post to the formatted version of the code below:

    Code:
        Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click
            Dim first, last, middle As Integer
            Dim flag As Boolean = False
            first = 0
            last = lstStates.Items.Count - 1
            Dim letters As String = mtbFirstTwoLetters.Text.ToUpper
            While first <= last
                middle = CInt((first + last) / 2)
                Dim state As String =
                    CStr(lstStates.Items.Item(middle)).ToUpper
                If state.StartsWith(letters) Then
                    flag = True
                    Exit While
                Else
                    Dim st As String = state.Substring(0, 2)
                    If letters > st Then
                        first = middle + 1
                    Else
                        last = middle - 1
                    End If
                End If
            End While
            If flag Then
                txtOutput.Text = 
            Else
                txtOutput.Text = "The State Is Not Found"
            End If
        End Sub
    As for the issue, have you actually debugged the code? Just reading it is often not enough to be able to spot an issue, especially with code that loops and branches a lot. You need to actually watch it in action.

    To do that, place a breakpoint (F9) at the top of the code and then execution will break at that point. You can then step through the code line by line (F10) and examine the state of the application at each step. Look at the current state first, determine what you think the state should be after the step and then step. Examine the state again and, if it doesn't match your expectation, you've found an issue. There are various tools available to examine the state, the most obvious being the Autos, Locals and Watch windows.

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Quick question!

    Moved to VB.NET

    As always, thanks for the report.

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Quick question!

    Er ... I think I'd work on the premise of the application before I worried about output. There's a reason that Zip codes aren't just the first two letters of the state. Several in fact, starting with ALabama and ALaska, getting really tricky with Maine, Maryland and Massachusetts, and ending with Tennessee and Texas!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Quick question!

    I don't know of ANY US Zip code that has letters period... now, if you grep that statement, replacing "Zip codes" with "State abbreviations" then it would be right.
    AL - Alabama
    AK - Alaska
    ME - Maine
    MD - Maryland
    MA - Massachusetts
    TN - Tennessee
    TX - Texas



    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2012
    Posts
    4

    Re: Quick question!

    techgnome,
    Say i changed the code to "state abbreviation" what i am wondering is when you enter TN for example for Tennessee what would i put in the txtOutput.Text to make it read Tennessee? Or if AL was entered it should say Alaska.

  7. #7
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Quick question!

    Quote Originally Posted by techgnome View Post
    I don't know of ANY US Zip code that has letters period... now, if you grep that statement, replacing "Zip codes" with "State abbreviations" then it would be right.
    AL - Alabama
    AK - Alaska
    ME - Maine
    MD - Maryland
    MA - Massachusetts
    TN - Tennessee
    TX - Texas



    -tg
    Well if we're going to be pedantic about it, I never actually suggested that there were. I merely stated that there was a reason that it wasn't the first two letters of the state. I made no reference to what they chose instead!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  8. #8
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Quick question!

    Your list of abbreviations needs a corresponding list of states. Instead of merely confirming that the abbreviation exists in the list you need to find its index. You then use the corresponding item in the full name list as your output.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  9. #9
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Quick question!

    Quote Originally Posted by dunfiddlin View Post
    Well if we're going to be pedantic about it, I never actually suggested that there were. I merely stated that there was a reason that it wasn't the first two letters of the state. I made no reference to what they chose instead!
    I was refeering to this comment: "There's a reason that Zip codes aren't just the first two letters of the state." ... that is all... the abbreviations was simple me showing off.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Quick question!

    Sorry, I omitted the emotion communicator (or smiley as it's better known!)! No offence taken or intended!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

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