Results 1 to 2 of 2

Thread: Arrays Displaying states/searching arrays

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2010
    Posts
    9

    Arrays Displaying states/searching arrays

    Ok so I have a project and I'm having a hard time with the arrays. Mainly how I search/remove/add items to the arrays.

    Any help would be greatly appreciated!

    "Write a program to declare an array with the statement Dim state(49) As String and maintain a list of certain states. The list of states should always be in alphabetical order and occupy consecutive elements of the array. The buttons in the program should give the user the following options:
    (a) Take the state specified by the user in a text box and insert it into its proper position in the array. If the state is already in the array, so report.
    (b) Take the state specified by the user in a text box and delete it from the array. If the state is not in the array, so report.
    (c) Display the states in the array. "

    My main question is am I going about this in the right way? Will I be able to search and remove items from the array I have built so far? I'm using a simple loop shown at the bottom to show that I can at least find items in the array, just looking for a few pointers on how to go about this more effieciently.

    This is what I have to do now I have written.

    Private Sub states_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    state(0) = "Alabama"
    state(1) = "Alaska"
    state(2) = "Arizona"
    state(3) = "Arkansas"
    state(4) = "California"
    state(5) = "Colorado"
    state(6) = "Connecticut"
    state(7) = "Delaware"
    state(8) = "Florida"
    state(9) = "Georgia"
    state(10) = "Hawaii"
    state(11) = "Idaho"
    state(12) = "Illinois"
    state(13) = "Indiana"
    state(14) = "Iowa"
    state(15) = "Kansas"
    state(16) = "Kentucky"
    state(17) = "Louisiana"
    state(18) = "Maine"
    state(19) = "Maryland"
    state(20) = "Massachusetts"
    state(21) = "Michigan"
    state(22) = "Minnesota"
    state(23) = "Mississippi"
    state(24) = "Missouri"
    state(25) = "Montana"
    state(26) = "Nebraska"
    state(27) = "Nevada"
    state(28) = "New Hampshire"
    state(29) = "New Jersey"
    state(30) = "New Mexico"
    state(31) = "New York"
    state(32) = "North Carolina"
    state(33) = "North Dakota"
    state(34) = "Ohio"
    state(35) = "Oklahoma"
    state(36) = "Oregon"
    state(37) = "Pennsylvania"
    state(38) = "Rhode Island"
    state(39) = "South Carolina"
    state(40) = "South Dakota"
    state(41) = "Tennessee"
    state(42) = "Texas"
    state(43) = "Utah"
    state(44) = "Vermont"
    state(45) = "Virginia"
    state(46) = "Washington"
    state(47) = "West Virginia"
    state(48) = "Wisconsin"
    state(49) = "Wyoming"
    End Sub

    as my array and then use this to display.

    Private Sub Display_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Display.Click
    ListBox1.Items.Clear()
    ListBox1.Items.Add(state(0))
    ListBox1.Items.Add(state(1))
    ListBox1.Items.Add(state(2))
    ListBox1.Items.Add(state(3))
    ListBox1.Items.Add(state(4))
    ListBox1.Items.Add(state(5))
    ListBox1.Items.Add(state(6))
    ListBox1.Items.Add(state(7))
    ListBox1.Items.Add(state(8))
    ListBox1.Items.Add(state(9))
    ListBox1.Items.Add(state(10))
    ListBox1.Items.Add(state(11))
    ListBox1.Items.Add(state(12))
    ListBox1.Items.Add(state(13))
    ListBox1.Items.Add(state(14))
    ListBox1.Items.Add(state(15))
    ListBox1.Items.Add(state(16))
    ListBox1.Items.Add(state(17))
    ListBox1.Items.Add(state(18))
    ListBox1.Items.Add(state(19))
    ListBox1.Items.Add(state(20))
    ListBox1.Items.Add(state(21))
    ListBox1.Items.Add(state(22))
    ListBox1.Items.Add(state(23))
    ListBox1.Items.Add(state(24))
    ListBox1.Items.Add(state(25))
    ListBox1.Items.Add(state(26))
    ListBox1.Items.Add(state(27))
    ListBox1.Items.Add(state(28))
    ListBox1.Items.Add(state(29))
    ListBox1.Items.Add(state(30))
    ListBox1.Items.Add(state(31))
    ListBox1.Items.Add(state(32))
    ListBox1.Items.Add(state(33))
    ListBox1.Items.Add(state(34))
    ListBox1.Items.Add(state(35))
    ListBox1.Items.Add(state(36))
    ListBox1.Items.Add(state(37))
    ListBox1.Items.Add(state(38))
    ListBox1.Items.Add(state(39))
    ListBox1.Items.Add(state(40))
    ListBox1.Items.Add(state(41))
    ListBox1.Items.Add(state(42))
    ListBox1.Items.Add(state(43))
    ListBox1.Items.Add(state(44))
    ListBox1.Items.Add(state(45))
    ListBox1.Items.Add(state(46))
    ListBox1.Items.Add(state(47))
    ListBox1.Items.Add(state(48))
    ListBox1.Items.Add(state(49))
    End Sub

    There has to be an easier way to display this right?

    I'm using this code to show that at least I can search the array

    Private Sub Remove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Remove.Click
    Dim Deletename As String
    Dim n As Integer
    Deletename = TextBox1.Text
    Do
    n += 1 'add 1 to n
    Loop Until (state(n) >= Deletename) Or (n = 49) 'interpret result of search
    If state(n) = Deletename Then
    textbox1.text = "found"
    Else
    textbox1.text = "not found"
    End If
    End Sub

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Arrays Displaying states/searching arrays

    to check if your array contains textbox1.text:

    vb Code:
    1. MsgBox(Array.IndexOf(state, StrConv(TextBox1.Text, VbStrConv.ProperCase)))

    if not found it'll return -1. anything else it exists


    c/

    vb Code:
    1. Private Sub Display_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Display.Click
    2.     ListBox1.Items.Clear()
    3.     ListBox1.Items.addrange(state)
    4. End Sub

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