
March 30th, 2012, 11:58 AM
|
 |
Type Cast Exception
|
|
Join Date: Apr 2004
Location: OAKLAND CA | Adam's Point (Fairyland)
|
|
It's hard to help you debug code we can't see, or even speculate on a randomization method which is not even described.
Hypothetical way to do this with just a standard array (adapt to image as appropriate)
Code:
Sub RandomArray()
' assumes arrays start at 0 (option base 0)
' create three undimensioned arrays
Dim SourceList() As String ' the original list
Dim RandomList() As String ' the randomized list
Dim SelectedList() As Long ' to keep track of random selections
Dim NumberSelected As Long ' keep track of the number of picks
Dim NumToSelect As Long ' how many we are picking (ubound + 1)
Dim ThisSelection As Long ' for the random pick
' populate the source list
SourceList = Split("ANT,BAT,CAT,DOG,ELEPHANT,FROG,GERBIL,HAMSTER,IGUANA", ",")
' redimension our other two
NumToSelect = UBound(SourceList) + 1 ' the number of elements in our source array
ReDim RandomList(0 To UBound(SourceList))
ReDim SelectedList(0 To UBound(SourceList))
Randomize ' if you don't do this your random sequence will be the same every time
Do
Do
ThisSelection = Int(Rnd * NumToSelect) ' select a random number in the range
Loop Until SelectedList(ThisSelection) = 0 ' loop until we find a number we haven't picked yet
SelectedList(ThisSelection) = 1 ' mark this as picked now
RandomList(NumberSelected) = SourceList(ThisSelection) ' copy the data from the random pick to output list
NumberSelected = NumberSelected + 1 ' increment the pick counter
Loop Until NumberSelected = NumToSelect ' loop until all have been picked
' Just output the results to immediate pane/debug window
For ThisSelection = 0 To UBound(SelectedList)
Debug.Print RandomList(ThisSelection) & " ";
Next
Debug.Print
End Sub
That's a VB6/VBA you can run in Excel, Access, etc. Should be about the same for .Net
The output will be like
Code:
CAT GERBIL DOG ELEPHANT HAMSTER IGUANA BAT FROG ANT
FROG ANT HAMSTER IGUANA CAT GERBIL DOG BAT ELEPHANT
ELEPHANT HAMSTER IGUANA FROG ANT GERBIL DOG BAT CAT
__________________
medialint.com
“Today you are You, that is truer than true. There is no one alive who is Youer than You.” - Dr. Seuss
|