-
Nov 14th, 2012, 08:22 PM
#1
Thread Starter
New Member
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
-
Nov 14th, 2012, 09:04 PM
#2
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.
-
Nov 15th, 2012, 11:28 AM
#3
Re: Quick question!
Moved to VB.NET
As always, thanks for the report.
-
Nov 15th, 2012, 11:42 AM
#4
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!
-
Nov 15th, 2012, 12:19 PM
#5
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
-
Nov 15th, 2012, 02:34 PM
#6
Thread Starter
New Member
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.
-
Nov 15th, 2012, 02:43 PM
#7
Re: Quick question!
Originally Posted by techgnome
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!
-
Nov 15th, 2012, 02:47 PM
#8
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!
-
Nov 15th, 2012, 02:53 PM
#9
Re: Quick question!
Originally Posted by dunfiddlin
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
-
Nov 15th, 2012, 03:18 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|