-
Oct 26th, 2002, 02:15 PM
#1
Thread Starter
Lively Member
rnd string from array, no repeats
i have an array of strings. i want to keep track of which ones i've used so i don't use them again. so right now i have it so that it adds the string to a listbox. when it chooses a word, it checks to see if it's already in the listbox. if it's already there, it'll keep choosing another string from the array until it finds one that hasn't been used yet. the problem with this method is that sometimes it'll stall for a long time because vb keeps choosing the same numbers over and over. does anyone know of a better way?
-
Oct 26th, 2002, 02:31 PM
#2
Frenzied Member
Why dont you have 2 listboxes....one with the ones to choose and ones with the ones that have been chosen.
When you use a string, move it from one listbox to the other, that way you will only select a new string from ones that havent been chosen. If you dont want 2 listboxes, use 2 collections
There are 3 types of people in this world.........those that can count, and those that can't.
Blobby
-
Oct 26th, 2002, 03:15 PM
#3
Here is an other option. You could load the contents of the array into a collection and as the item is used remove it from the collection.
VB Code:
Option Explicit
Dim strState(9) As String
Dim colState As Collection
Private Sub Command1_Click()
Dim i As Integer
If colState.Count = 0 Then Exit Sub
Randomize
i = (Int(Rnd() * colState.Count)) + 1
Me.Print colState(i)
colState.Remove i
End Sub
Private Sub Form_Load()
Dim i As Integer
Me.AutoRedraw = True
strState(0) = "Alabama"
strState(1) = "Alaska"
strState(2) = "Arizona"
strState(3) = "Arkansas"
strState(4) = "California"
strState(5) = "Colorado"
strState(6) = "Connecticut"
strState(7) = "Delaware"
strState(8) = "Florida"
strState(9) = "Georgia"
Set colState = New Collection
For i = 0 To 9
colState.Add Item:=strState(i)
Next i
End Sub
Last edited by MarkT; Oct 26th, 2002 at 03:41 PM.
-
Oct 26th, 2002, 04:51 PM
#4
Thread Starter
Lively Member
oh yeah...i was doing it backwards. hm.. thanks.
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
|