• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Linear Search Help

mjs34

Junior Member
Hey everyone! I am new to using Visual Basic, and i need help!! Im tryin to create a 2-d linear search array, where if i type in a 2 letter code, it would come back with a name linked to that code!!

for example, if i typed in "ca" into textbox1, it would return with "california" in textbox2, and so on!!

so far my search looks like this

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim areacode As String(,) = {{"CA","NY"}, {"California","New York"}}
Dim Index As Integer = 0
Dim code As String

code = TextBox1.Text

Do Until areacode(index) = code_
Or_
code(index) = "end"
index = index + 1

Loop
If areacode(index) = code Then
TextBox2.Text = areacode(index)
Else
TextBox2.Text = "Error Not Found"
End IF
End Sub


If anyone could help me with this it would be highly appreciated!!!!




 
You've listed your homework, but you need to list what part of it you're having a problem with.

What isn't working, what have you tried doing so far to fix it?
 
I don't know VB syntax at all but it looks like you have some array referencing problems

areacode is an array of arrays so you'll need something like: areacode[index1][index2]
If you want to access anything. It might be easier to break it into two arrays like thus:

Dim areacode As String() ={{"CA","NY"}];
Dim state As String() = [{"California","New York"}]

Then when you find your index in the loop:
TextBox2.Text = state(index);

Or else, do it like you're currently doing it but you'll need to do references like this to get the correct array (does VB array index start at 0?).

Do Until areacode(0,index) = code_

and

If areacode(0,index) = code Then
TextBox2.Text = areacode(1,index)
 
the problem that i am having with this is that the loop does not work, and will not show in textbox2! Here is my updated code:

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim areacode(,) As String = {{"CA", "LA", "NY"}}
Dim area () As String = {"California" "Los Angeles" "New York"}
Dim index As Integer
Dim code As String

code = TextBox1.Text
index = 0
TextBox2.Text = area(0, index)

Do Until areacode(0, index) = code
If areacode(0, index) = code Then
TextBox2.Text = areacode(1, index)
Else
TextBox2.Text = "Error"
End If
Loop
End Sub
End Class

can anyone point out where im going wrong, and what it is im doing wrong!!
all help so far has been appreciated, im just struggling with the getting it to work!!
 
Here's one bug:

Dim area () As String = {"California" "Los Angeles" "New York"}
...
TextBox2.Text = areacode(1, index)

You changed one part of your design (though I don't see why) without changing the other.

Some other bugs are bad changes to the loop:
1. it will run forever for codes other than "CA" (no lndex increment)
2. when you fix (1.) it will crash for unknown codes since you took out the guard in the loop that keeps it from running past the end of the array.
3. The new loop tests for equal and exits before it gets to the code in the loop that sets the textbox. The old design had the code to set the textbox outside the loop, that will work.

I'd suggest learning some basic debugging: how to set a breakpoint, and how to single-step through the code to see what is happening. If you did that the loop problems should be easy to see.
 
i realli am sorri for all this people but this is all going straigh over my head!!! im just stuck on the do until section and loop!! ive reverted back to my 'old design' for the loop, but am still struggling to see what im doing wrong!!! im brand new to this so please bare with me

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim area(,) As String = {{"California, "Los Angeles", "New York"}}
Dim index As Integer
Dim areacode(,) As String = {{"CA", "LA", "NY"}}

TextBox2.Text = areacode(1, index)


Do Until areacode(index) = area
Or _()
areacode(index) = "end"
index = index + 1
Loop
If areacode(index) = areacode Then
TextBox2.Text = areacode(index)
Else
TextBox2.Text = "Error Not Found"
End If
End Sub
End Class

im just confussed as to wat im missing out in the 'do until' part, and the loop!!! i feel it is to do with my index's but can't see what!!
 
ive solved it now it, however just need to be pointed out where im going wrong with the loop!! i at least now have text to appear in the textbox, but it shows an error sign all the time rather than the area name!! here is my latest code:

index = 0
index1 = 1
Code = TextBox1.Text
Code = Code.ToUpper
found = False

Do Until areacode(0, index + 1) = areacode(0, index + 1) Or found
index = area(index1 + 1)
Loop


If areacode(0, index) = results Then
TextBox2.Text = area(index1)
Else
TextBox2.Text = "Invalid Area"
End If

where am i going wrong for it to constantly say "Invalid Area"??? if this could be pointed out to me it wud be appreciated, and then i will stop hasserling u all!!
 
I think you really need to take a step back and learn some basics. What you are trying to do is not very difficult. You might want to learn things in two different chunks, start with arrays... just write a simple program, create an array and test outputing different items. Don't do it programmatically at first, just hard code it and see if you get what you expected.

The second thing to do is experiment with loops separate. Make a loop that will output numbers from 1 to 10. Then modify the loop to see if you can get it to stop at a number you want, say 6.

Once you understand these two elements separately you should be able to combine them effectively.

If you're asking someone here to write your code for you, you might get lucky but chances are you'll just get ignored.
 
Back
Top