Why whenever I want to ReDim Preserve an array it says the array is already dimensioned. Does this mean I cant redim?
Thanks
Printable View
Why whenever I want to ReDim Preserve an array it says the array is already dimensioned. Does this mean I cant redim?
Thanks
It sounds like you are trying to Redimension an array that was not declared dynamically, for example
You can't redimension an array declared like this...
This is the proper way to declare an array dynamically (if you want to change it's dimensions later using the Redim statment)....Code:Dim sArray(1 to 10)
If you still can't get it to work, post the code.Code:Dim sArray()
Well if I make it dynamic it always goes subscript out of range for this:
For i = 0 To UBound(strPuzzle)
Next i
Thanks
Try this...
Code:
For i = LBound(strPuzzle) to Ubound(strPuzzle)
' your code
next i
It STILL gives me subscript out of range, can anyone tell me why?
Thanks
Post the code where you are receiving the error.
Well its much longer than this but Ill give you the part that I need
and in General I declareCode:Private Sub cmdNew_Click()
Dim intRepeat As Integer
Dim i As Integer
For i = LBound(strPuzzle) To UBound(strPuzzle)
intRandom1 = Int((10 - 1 + 1) * Rnd + 1)
If strPuzzle(i) <> strWord Then
Exit For
End If
Next i
If intRandom1 = 1 Then
lbl1.Caption = "h"
lbl2.Caption = "a"
lbl3.Caption = "p"
lbl4.Caption = "p"
lbl5.Caption = "y"
lblSubject.Caption = "Emotion"
strWord = "happy"
ElseIf intRandom1 = 2 Then
lbl1.Caption = "t"
lbl2.Caption = "e"
lbl3.Caption = "d"
lbl4.Caption = "d"
lbl5.Caption = "y"
lblSubject.Caption = "Thing"
strWord = "teddy"
ElseIf intRandom1 = 3 Then
lbl1.Caption = "b"
lbl2.Caption = "l"
lbl3.Caption = "i"
lbl4.Caption = "m"
lbl5.Caption = "p"
lblSubject.Caption = "Thing"
strWord = "blimp"
ElseIf intRandom1 = 4 Then
lbl1.Caption = "l"
lbl2.Caption = "e"
lbl3.Caption = "a"
lbl4.Caption = "f"
lbl5.Caption = "s"
lblSubject.Caption = "Sports"
strWord = "leafs"
ElseIf intRandom1 = 5 Then
lbl1.Caption = "s"
lbl2.Caption = "m"
lbl3.Caption = "a"
lbl4.Caption = "r"
lbl5.Caption = "t"
lblSubject.Caption = "Adjective"
strWord = "smart"
ElseIf intRandom1 = 6 Then
lbl1.Caption = "s"
lbl2.Caption = "t"
lbl3.Caption = "a"
lbl4.Caption = "m"
lbl5.Caption = "p"
lblSubject.Caption = "Thing"
strWord = "stamp"
ElseIf intRandom1 = 7 Then
lbl1.Caption = "h"
lbl2.Caption = "o"
lbl3.Caption = "o"
lbl4.Caption = "p"
lbl5.Caption = "s"
lblSubject.Caption = "Sports"
strWord = "hoops"
ElseIf intRandom1 = 8 Then
lbl1.Caption = "c"
lbl2.Caption = "h"
lbl3.Caption = "a"
lbl4.Caption = "i"
lbl5.Caption = "r"
lblSubject.Caption = "Thing"
strWord = "chair"
ElseIf intRandom1 = 9 Then
lbl1.Caption = "p"
lbl2.Caption = "a"
lbl3.Caption = "r"
lbl4.Caption = "i"
lbl5.Caption = "s"
lblSubject.Caption = "Place"
strWord = "paris"
ElseIf intRandom1 = 10 Then
lbl1.Caption = "p"
lbl2.Caption = "i"
lbl3.Caption = "l"
lbl4.Caption = "o"
lbl5.Caption = "t"
lblSubject.Caption = "Job"
strWord = "pilot"
End If
intRepeat = UBound(strPuzzle) + 1
ReDim Preserve strPuzzle(intRepeat)
strPuzzle(intRepeat) = strWord
End Sub
please help,Code:Dim strPuzzle() as string
Thanks,
Now that you have my code, can you tell me how I wouldnt have two puzzles come up twice, thanks, this is really what ive been striving at.
thanks
Sorry I'm not quite sure what you are trying to accomplish with your code, could you give a description and what do you mean by not having the puzzle come up twice.
For i = LBound(strPuzzle) To UBound(strPuzzle)
you are having a problem reading which is probably a result of how you are loading the array.
What code are you using to load the array?
Well Im lodaing the array by typing dim strPuzzle() in the general procedures. And what I mean by not having a puzzle come twice, is that each intRandom1 number is a different puzzle.For example: intRandom1 = 1 is happy and
intRandom1 = 5 is smart. I would like it so that each puzzle only comes up once. So I think I have to keep it looping the randomizer until it reaches a number it hasnt done before. Or something like that.
Thanks a bunch,
Here's a thought, why not everytime a puzzle is choosen, dump it into an invisible list box, and the next time another puzzle is choosen, compare it to the listbox, if it exists in the list, grab the next puzzle. Just a thought
Would you or someone be able to show me the code of how I would compare it to the listbox to see if its there?I think I will put this into a new thread
Thanks
[Edited by cramtheman on 12-31-2000 at 04:18 PM]
I didn't want to completely rewrite the code for you so I know this is not complete but it should give you an idea of how to use the puzzles without repeating any of them.
It shouldn't be that hard to apply this to your code, if you need anymore help don't hesitate to ask.Code:
Private strPuzzle(1 To 10)
Private Sub cmdNew_Click()
Static iCurrent As Integer
Dim sStr As String
If Not iCurrent = 10 Then
iCurrent = iCurrent + 1
sStr = strPuzzle(iCurrent) ' get current element in array
lbl1.Caption = Mid(sStr, 1, 1)
lbl2.Caption = Mid(sStr, 2, 1)
lbl3.Caption = Mid(sStr, 3, 1)
lbl4.Caption = Mid(sStr, 4, 1)
lbl5.Caption = Mid(sStr, 5, 1)
Else
MsgBox "ALL PUZZLES HAVE BEEN DISPLAYED"
End If
End Sub
Private Sub Form_Load()
Dim iCt As Integer
'assign strings to the array
strPuzzle(1) = "happy"
strPuzzle(2) = "teddy"
strPuzzle(3) = "blimp"
strPuzzle(4) = "leafs"
strPuzzle(5) = "smart"
strPuzzle(6) = "stamp"
strPuzzle(7) = "hoops"
strPuzzle(8) = "chair"
strPuzzle(9) = "paris"
strPuzzle(10) = "pilot"
'shuffle the elements in the array around
ShuffleArray strPuzzle, 100
'display the results
For iCt = 1 To 10
Debug.Print iCt & " " & strPuzzle(iCt)
Next iCt
End Sub
Private Sub ShuffleArray(ByRef arr(), ByVal iShuffleTimes As Integer)
Dim iCt As Integer
Dim iRandElem As Integer
Dim vTemp
'assure we don't get the same set of random numbers
Randomize
For iCt = 1 To iShuffleTimes
iRandElem = (Rnd * (UBound(arr) - LBound(arr))) + LBound(arr) ' get an element within the bounds of the array randomly
vTemp = arr(LBound(arr)) ' assign the value of the first element in the array to the temporary variable
arr(LBound(arr)) = arr(iRandElem) ' shuffle the values
arr(iRandElem) = vTemp
Next iCt
End Sub
[Edited by YoungBuck on 01-04-2001 at 12:11 PM]