Results 1 to 3 of 3

Thread: Arg, arrays are so confusing! (Visual Basic 2008)

  1. #1
    GO! use leech seed! qwertysaur's Avatar
    Join Date
    Dec 2006
    Location
    Kanto
    Posts
    11,627
    Contributions
    • Former Site Staff

    Default Arg, arrays are so confusing! (Visual Basic 2008)

    Basically, my problem is that I have to code a program where you input up to 15 items into a list and the program sorts the list for you. I got the checking for a good number input already, it's the adding to the list that gives me trouble. I know there is a logic error somewhere, I just can't find it. Here is my code.
    Code:
    Private Sub btninput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btninput.Click
            'variables
            Dim intnumberofentries As Integer = 1
            Dim stritemtoenter As String = ""
            Dim strinputboxheading As String = "Enter the name of item # "
            Dim striteminlist As String = stra
            'conversion
            intnumberofitems = Convert.ToInt32(Me.txtnumberofentries.Text)
    
            stritemtoenter = InputBox(strinputboxheading & intnumberofentries, _
                                    "Add an item", "")
            'loop begins here
            Do Until intnumberofentries > intnumberofitems
    
                Me.lstinput.Items.Add(stritemtoenter)
                intnumberofentries += 1
                Select Case intnumberofentries
                    Case 1
                        stritemtoenter = stra
                    Case 2
                        stritemtoenter = strb
                    Case 3
                        stritemtoenter = strc
                    Case 4
                        stritemtoenter = strd
                    Case 5
                        stritemtoenter = stre
                    Case 6
                        stritemtoenter = strf
                    Case 7
                        stritemtoenter = strg
                    Case 8
                        stritemtoenter = strh
                    Case 9
                        stritemtoenter = stri
                    Case 10
                        stritemtoenter = strj
                    Case 11
                        stritemtoenter = strk
                    Case 12
                        stritemtoenter = strl
                    Case 13
                        stritemtoenter = strm
                    Case 14
                        stritemtoenter = strn
                    Case 15
                        stritemtoenter = stro
                End Select
            Loop
            Select Case intnumberofitems
                Case 1
                    ReDim Preserve strlistofitems(0)
                Case 2
                    ReDim Preserve strlistofitems(1)
                Case 3
                    ReDim Preserve strlistofitems(2)
                Case 4
                    ReDim Preserve strlistofitems(3)
                Case 5
                    ReDim Preserve strlistofitems(4)
                Case 6
                    ReDim Preserve strlistofitems(5)
                Case 7
                    ReDim Preserve strlistofitems(6)
                Case 8
                    ReDim Preserve strlistofitems(7)
                Case 9
                    ReDim Preserve strlistofitems(8)
                Case 10
                    ReDim Preserve strlistofitems(9)
                Case 11
                    ReDim Preserve strlistofitems(10)
                Case 12
                    ReDim Preserve strlistofitems(11)
                Case 13
                    ReDim Preserve strlistofitems(12)
                Case 14
                    ReDim Preserve strlistofitems(13)
                Case 15
                    ReDim Preserve strlistofitems(14)
            End Select
    
            Array.Sort(strlistofitems)
    
            For Each srtiteminlist In strlistofitems
                Me.lstoutput.Items.Add(striteminlist)
            Next
        End Sub

  2. #2
    Old school, like an old fool. Flying Mullet's Avatar
    Join Date
    Apr 2003
    Location
    Napping in a peach tree.
    Posts
    19,185
    Articles
    6
    Blog Entries
    7
    Contributions
    • Former Administrator
    • Former Cid's Knight
    • Former Senior Site Staff

    Default

    I don't understand exactly what's happening that you don't want, but I do know that you should use your looping variables whenever possible rather than use large case statements. i.e.
    Code:
    ReDim Preserve strlistofitems(intnumberofentries-1)
    Figaro Castle

  3. #3
    GO! use leech seed! qwertysaur's Avatar
    Join Date
    Dec 2006
    Location
    Kanto
    Posts
    11,627
    Contributions
    • Former Site Staff

    Default

    Woah, that nightmare is why I shouldn't play mafia and code at the same time

    After taking out both of those huge case statements, as well as rewriting the array similar to how Mullet showed, I've managed to fix the program.

    my error was that I managed to somehow make the output list blank, even though there was data in the input for the array.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •