|
-
Mar 4th, 2007, 11:44 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] [2005] Generating a list (again)
http://www.vbforums.com/showthread.php?t=455508
I posted the above and thought it was working but turned out it wasn't. I've since somehow managed to completely mess it up. Can anyone look at this and suggest a reason? I've added break points but can't figure out where the problem actually exists. The originalList definitely contains 120 items, 20 each of the letters A,B,C, X, Y, Z
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Declared outside of subroutine:
' Private originalList As New List(Of String)
' Private masterList As New List(Of String)
Dim myRandom As New Random
Dim RandomIndex As Integer = 0
Dim tempSelected As String = Nothing 'The item selected to be evaluated:
Dim same2Letters, same4Category As Boolean
Dim GroupA_count, GroupB_count As Integer
Dim previousGroup As String = Nothing
Do Until masterList.Count = 120
Do
'Step 1: Select Random Letter from Original List
'From 0 to 119: .Count = 120
RandomIndex = myRandom.Next(0, originalList.Count)
tempSelected = originalList(RandomIndex) 'Current letter selected
same2Letters = True 'Reset/set loop conditions
same4Category = True
GroupA_count = 0 'Reset category counters
GroupB_count = 0
Select Case masterList.Count
Case Is <= 1 'Only 0 or 1 item exists: item can be automatically added
' masterList.Add(tempSelected) see below
same2Letters = False
same4Category = False
Exit Select
Case Else
'Check previous 2 items in the MasterList: if not the same then:
If masterList(masterList.Count - 1) <> _
masterList(masterList.Count - 2) Then 'check passed:
same2Letters = False '2 previous items aint the same: good
End If
'Now check for Category membership:
'A,B,C = GroupA; X,Y,Z = GroupB
For i As Integer = masterList.Count - 1 To masterList.Count - 4 Step -1 'Now check category info
'Find out which Category Group the current item in masterList belongs to
previousGroup = ClassifyItem_ToGroup(masterList(i))
Select Case previousGroup 'Count how many items are in a
Case "GroupA" 'specific group
GroupA_count += 1
Case "GroupB"
GroupB_count += 1
Case Else
MessageBox.Show("Problem classifying")
End Select
If i = 0 Then Exit For 'Got to beginning of array so exit
Next
If GroupA_count <= 3 And GroupB_count <= 3 Then 'If 3 or under then:
same4Category = False 'A good thing
End If
End Select
Loop Until same4Category = False And same2Letters = False
masterList.Add(tempSelected) 'If passed then add to masterlist
originalList.RemoveAt(RandomIndex) 'Remove the item from Original List
Loop
Me.ListBox1.Items.AddRange(masterList.ToArray) 'ADD items to Listbox
End Sub
Private Function ClassifyItem_ToGroup(ByVal currentItem As String) As String
Select Case currentItem
Case "A", "B", "C"
Return "GroupA"
Case "X", "Y", "Z"
Return "GroupB"
Case Else
Return "NONE"
End Select
End Function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|