-
Apr 15th, 2012, 08:49 AM
#1
Thread Starter
New Member
question about loops and arrays
im just learning loops and arrays, and i have a couple of questions.
i have a array set up, and i need the array to be global, but i can only get vb to accept it in a button, if anyone can help out thier. in addition, after the loop is completed, i am trying to pull a value from the array and display it but am getting an error.
the code is as follows:
Code:
Public Class Form1
Friend staten As String
Friend statea As String
Dim statec(1, 54)
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
statename.Clear()
stateabbr.Clear()
End Sub
Public Sub findabbr_Click(sender As System.Object, e As System.EventArgs) Handles findabbr.Click
staten = statename.Text
Dim i As Integer
Dim statec(,) As String = {{"alabama", "al"}, {"alaska", "ak"}, {"american samoa", "as"},
{"arizona", "az"}, {"arkansas", "ar"}, {"california", "ca"},
{"colorado", "co"}, {"connecticutt", "ct"}, {"delaware", "de"},
{"washington d.c.", "dc"}, {"florida", "fl"}, {"georgia", "ga"},
{"guam", "gu"}, {"hawaii", "hi"}, {"idaho", "id"}, {"illinois", "il"},
{"indiana", "in"}, {"iowa", "ia"}, {"kansas", "ks"}, {"kentucky", "ky"},
{"louisana", "la"}, {"maine", "me"}, {"maryland", "md"}, {"massachussets", "ma"},
{"michigan", "mi"}, {"minnesota", "mn"}, {"missisippi", "ms"}, {"missouri", "mo"},
{"montana", "mt"}, {"nebraska", "ne"}, {"nevada", "nv"}, {"new hampshire", "nh"},
{"new jersey", "nj"}, {"new mexico", "nm"}, {"new york", "ny"}, {"north carolina", "nc"},
{"north dakota", "nd"}, {"ohio", "oh"}, {"oklahoma", "ok"}, {"oregon", "or"}, {"pennsylvania", "pa"},
{"puerto rico", "pr"}, {"rhode island", "ri"}, {"south carolina", "sc"}, {"south dakota", "sd"},
{"tennesee", "tn"}, {"texas", "tx"}, {"trust territories", "tt"}, {"utah", "ut"}, {"vermont", "vt"},
{"virginia", "va"}, {"virgin islands", "vi"}, {"washington", "wa"}, {"west virginia", "wv"},
{"wisconsin", "wi"}, {"wyoming", "wy"}}
i = 0
Do Until i = 50
Loop
If staten = statec(0, i) Then
statea = statec(1, i)
Else
i = i + 1
If i > 49 Then
MessageBox.Show("input error. please recheck")
End If
End If
stateabbr = statec
End Sub
End Class
the error code is
Code:
Error 1 Value of type '2-dimensional array of String' cannot be converted to 'System.Windows.Forms.TextBox'. C:\Users\chris\Documents\Visual Studio 2010\Projects\state 8.4\state 8.4\Form1.vb 50 21 state 8.4
-
Apr 15th, 2012, 10:16 AM
#2
Re: question about loops and arrays
I suspect this is the problem:
stateabbr = statec
if StateAbbr is a combobox you should rewrite it like StateAbbr.datasource= statec
why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
for every question you ask provide an answer on another thread.
-
Apr 15th, 2012, 12:05 PM
#3
Re: question about loops and arrays
Just in case it hasn't been solved, I've asked the moderators to move this thread to the appropriate subforum.
-
Apr 16th, 2012, 06:50 AM
#4
Re: question about loops and arrays
Thread moved from the 'CodeBank VB.Net' forum (which is for you to post working code examples, not questions) to the 'VB.Net' forum
thanks for letting us know formlesstree4
-
Apr 16th, 2012, 07:04 AM
#5
Re: question about loops and arrays
According to your error message I suspect that stateabbr is a textbox and you're trying to assign a two dimensional array to it:I guess what you wanted to assign to it was the statea string, but a string can't be converted to a textbox either so you must use the Text property
Code:
stateabbr.Text = statea
-
Apr 16th, 2012, 11:12 AM
#6
Re: question about loops and arrays
And if that doesn't solve it, tell us which line is showing the error. After all, both Dnereb and JA took reasonable guesses at which line was causing the problem, and if those guesses were wrong, we really need to know which line is causing the problem.
As for the initial question, to make the array global, you can declare it as Public in a module, rather than Dim as a local variable in a method.
My usual boring signature: Nothing
-
Apr 16th, 2012, 03:38 PM
#7
Thread Starter
New Member
Re: question about loops and arrays
ok that problem is fixed, thank you, but as a question, how can i take the array value assignment from the one button and make it global?
-
Apr 16th, 2012, 04:47 PM
#8
Re: question about loops and arrays
You'll have to explain what you are trying to do a bit more for me to understand it.
My usual boring signature: Nothing
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
|