|
-
Jan 6th, 2005, 08:05 PM
#1
Thread Starter
New Member
For Each Element In Array... Next Element {RESOLVED}
I am trying to loop through an array "aryDeck" and retrieve an element from the bottom. Place that element into array "aryHand". Drop all elements in "aryDeck" down by one position erasing the first element. This performs the act of actually removing it from the array. Then Redim array "aryDeck" to one less its original value. Then Redim array "aryHand" to one plus its original value.
Code:
Dim Card As Variant
Dim i As Integer
i = UBound(aryDeck) 'To prevent access of a 53rd element
For Each Card In aryDeck
If i <> Card Then
aryDeck(Card) = aryDeck(Card + 1)
End If
Next Card
I expected "Card" to give me the element number, instead, I get the elements value. How can I get access to the elements number while using the For Each... Next method? Is there a way to access it from the element or do I need to include a counter?
In addition, the error I get is a "Subscript out of range" error on the Card variable. "aryDeck" is dimensioned as "1 to 52". So "Card" can't be retrieving the elements position in the array. The values entered into "aryDeck" where randomly chosen numbers from "1 to 52." So, it would appear that "Card" is not getting it's value from an elements contents either. This is supported by a boolean "cZero" that would be set to true if a value for "aryDeck" was ever equal '0'. Where then is "Card" getting a '0' value from?
Last edited by bcamaro85; Jan 9th, 2005 at 10:31 PM.
-
Jan 6th, 2005, 09:02 PM
#2
Re: For Each Element In Array... Next Element
You don't have a collection of object so For each ... type of loop isn't suitable in this context.
What you need is an ordinary loop similar to the following:
VB Code:
Dim i%
For i = 0 to Ubound(YourArray)
Debug.Print YourArray(i)
Next i
-
Jan 7th, 2005, 12:21 PM
#3
Thread Starter
New Member
Re: For Each Element In Array... Next Element
Got it! Thanks, but I figured a simple way of doing it.
I just draw the card from UBound(aryDeck) and Redim Preserve aryDeck(UBound(aryDeck) -1), truncating the value I just drew. Problem solved with just 2 simple lines of code.
-
Jan 7th, 2005, 12:25 PM
#4
Banned
Re: For Each Element In Array... Next Element
what if the card is in the middle of the deck you need to shift the other cards first.
its better to use dictionary (faster then collection) wich is programmed with stacks and does what you want
-
Jan 7th, 2005, 01:51 PM
#5
Thread Starter
New Member
Re: For Each Element In Array... Next Element
 Originally Posted by nareth
what if the card is in the middle of the deck you need to shift the other cards first.
its better to use dictionary (faster then collection) wich is programmed with stacks and does what you want
The Dictionary object is new to me but it looks promising. I'll check into it. Thanks
-
Jan 7th, 2005, 01:59 PM
#6
Banned
Re: For Each Element In Array... Next Element
its in the reference called Microsoft Scripting Run Time
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
|