PDA

View Full Version : Arg, arrays are so confusing! (Visual Basic 2008)



qwertysaur
12-16-2008, 04:00 PM
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.

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

Flying Mullet
12-16-2008, 04:42 PM
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.

ReDim Preserve strlistofitems(intnumberofentries-1)

qwertysaur
12-16-2008, 07:05 PM
Woah, that nightmare is why I shouldn't play mafia and code at the same time :p

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.