Results 1 to 8 of 8

Thread: question about loops and arrays

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    13

    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

  2. #2
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    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.

  3. #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.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    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

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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:
    Code:
    stateabbr = statec
    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

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    13

    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?

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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
  •  



Click Here to Expand Forum to Full Width