3 Replies - 1883 Views - Last Post: 09 March 2012 - 03:33 PM Rate Topic: -----

#1 ara4638  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-March 12

Question about arrays and for next loop

Posted 09 March 2012 - 01:57 PM

Hi, I am a beginner with using visual basic. I need a little bit of help. I am making an application that allows a teacher to enter students names to be displayed in a listbox. Then they need to be randomly put into 4 groups and displayed in four labels. The students have to be distributed as evenly as possible and cannot be repeated.

So, the code that I have figured out so far is: The names can be entered in a listbox, and then I put them into an array. Then I have a piece of code that randomizes the array (I tested it to make sure it displayed a random order of names).

The problem that I am having now is figuring out how to use the for next loop to cycle through the array. For example, I don't know how this would work exactly, but I need the loop to put lstNames(0) in lblGroup1.text..lstNames(1) in lblGroup2.text...lstNames(2) in lblGroup3.text...lstNames(3) in lblGroup4.text....lstNames(4) in lblGroup1.text....and so on until the loop reaches the end of the array and all of the names are evenly distributed in one of the four groups and displayed in their respective groups.

I've tried different for/next and for/each loops. Do I create an array for each of the four groups and then display the array in the label? If so, how would I do that?

I have been trying to figure this out all day, there has to be something that I am just totally missing/thinking its harder than it is...so sorry if this question is dumb! Any information would be greatly appreciated.

Here is the code that I have so far:


 Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

        'Create array that holds all student's names..goes through listbox and adds them to array
        Dim strNameArray() As String
        ReDim strNameArray(lstNames.Items.Count - 1)
        lstNames.Items.CopyTo(strNameArray, 0)


        'Randomizes the array 
        Dim random As New Random()
        Dim max_index As Integer = random.Next(0, lstNames.Items.Count() - 1)
        Dim rnd As New Random
        For i As Integer = 0 To max_index - 1
            ' Pick an item for position i.
            Dim j As Integer = rnd.Next(i, max_index + 1)
            ' Swap them.
            Dim temp As String = strNameArray(i)
            strNameArray(i) = strNameArray(j)
            strNameArray(j) = temp
        Next i

        '>>>>>>>>>>>>>>>>>This is where I am stuck<<<<<<<<<<<<<<<<<<<<<<<<<

        'loop through array and put each name in a group

        'Dim intMAX_SUB As Integer = lstNames.Items.Count() - 1
        'Dim intCount As Integer

        'For intCount = 0 To intMAX_SUB Step 1
        '    lblGroup1.Text = strNameArray(intCount)
        'Next

        'Display the group results in the respective label 
    End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Question about arrays and for next loop

#2 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 323
  • Joined: 12-January 11

Re: Question about arrays and for next loop

Posted 09 March 2012 - 03:06 PM

There are several ways to go about what you are trying to do.

For example you could do this:
Dim lblArray As Label() = {lbl1, lbl2, lbl3, lbl4} 'edited, forgot one of the labels, but you get the point.

For i As Integer = 0 to lblNames.Count-1
    lblArray(i).Text = strNamesArray(i)
Next



You could also use a Select statement from within the For loop. Its really up to you how you want to do it.

This post has been edited by Psyguy: 09 March 2012 - 03:12 PM

Was This Post Helpful? 1
  • +
  • -

#3 ara4638  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-March 12

Re: Question about arrays and for next loop

Posted 09 March 2012 - 03:25 PM

I added that in and modified it to have the correct names and stuff(I think) so it looks like this:

        'Total # students on list
        Dim intNumberofStudents = lstNames.Items.Count() - 1


        'loop through array and put each name in a group

        Dim lblArray As Label() = {lblGroup1, lblGroup2, lblGroup3, lblGroup4}

        For i As Integer = 0 To intNumberofStudents

            lblArray(i).Text = strNameArray(i)

        Next



But, I am getting unhandled exception error:

System.IndexOutOfRangeException was unhandled
Message=Index was outside the bounds of the array.
Was This Post Helpful? 0
  • +
  • -

#4 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 323
  • Joined: 12-January 11

Re: Question about arrays and for next loop

Posted 09 March 2012 - 03:33 PM

If I understood you correctly, you have an array with four strings (one for each group). We added an array with four labels with which we we will assign the text property the value from our string array. Is this correct?

EDIT:
It appears to be a slight miscommunication, lets try again. I reread your post a few times, now I see what you are trying to do, although, I'm not sure it is working as you intend it to. Here is what I have come up with using your code (I rearranged some stuff):
Private Sub btnSortAndDisplay_Click( sender As System.Object,  e As System.EventArgs) Handles btnSortAndDisplay.Click
        Dim strNameArray(lbNames.Items.Count-1) As String
        Dim lblArray As Label() = {Label1, Label2, Label3, Label4}
        Dim r As New Random()
        Dim rand As New Random
        Dim max As Integer

        lbNames.Items.CopyTo(strNameArray, 0)
        max = r.Next(0, lbNames.Items.Count - 1)
        'clear the labels
        For i As Integer = 0 to lblArray.Count-1
            lblArray(i).Text = String.Empty
        Next
        'sort the array of names into a random order (shuffle the names)
        For i As Integer = 0 to max-1
            Dim j As Integer = rand.Next(i, max +1)
            Dim temp As String = strNameArray(j)
            strNameArray(i) = strNameArray(j)
            strNameArray(j) = temp
        Next
        'add the names to each of the four labels
        Dim lblPos As Integer = 0
        For i As Integer = 0 to strNameArray.Count-1
            'if i/4 has no remainder, do this...it allows the lblPos to reset
            If i mod 4 = 0 then
                lblPos = 0
            End If
            'add the name to the text group with a space between names
            lblArray(lblPos).Text &= strNameArray(i) & " "
            'increment the lblPos
            lblPos+=1
        Next
End Sub



You will find that this will display the names, but you will get repeats and some that dont show up at all, presumably due to the shuffling process. You may want to have a look at that.

This post has been edited by Psyguy: 09 March 2012 - 04:13 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1