John McGraw
Board Regular
- Joined
- Feb 25, 2002
- Messages
- 76
I am trying to write a routine that will allow me to manipulate an array as a "Stack". I want to have a fixed size array that "pushes" all the elements to the left when a new element is added to the "top of the stack". So if I have an array with 8 elements, and I add a new element to the top of the "stack", all the 8 elements will move left one, and the 1st element will "pop off" as it will no longer be needed.
I wrote a function, but it does not work, because I do not seem to be able to pass a declared array to a sub. Here is the code:<pre/>
Sub AddToStack(ItemToAdd As Variant, ParamArray StackArray() As Variant)
Dim i As Long, Top As Long, Bottom As Long
Bottom = LBound(StackArray())
Top = UBound(StackArray())
For i = Bottom To (Top - 1)
StackArray(i) = StackArray(i + 1) 'move each item 1 position to the left
Next i
StackArray(Top) = ItemToAdd ' add the "item to add" to the last position in the array
End Sub</pre>
The problem is that the Ubound and Lbound functions return 0, even though the array is obviously larger.
I think I am completely misunderstanding how to pass arrays to a procedure. VBA help is not helping either. So if anyone can help me out, I'd really appreciate it.
Here is the "test" sub that I use to test out the "addtostack" sub.<pre/>
Sub test()
Dim tester(8) As Integer
Dim i As Integer
For i = 1 To 8
tester(i) = i
Next i
AddToStack 9, tester()
End Sub</pre>
I get no errors, but it just treats the array that I pass as if it has no elements.
Thanks for any help!!!! This is driving me nuts.
Also, if there is any built-in functionality in VBA to manipulate an array as a stack, please let me know. I have a feeling I may be re-inventing the wheel on this one.
This message was edited by John McGraw on 2002-05-21 00:46
I wrote a function, but it does not work, because I do not seem to be able to pass a declared array to a sub. Here is the code:<pre/>
Sub AddToStack(ItemToAdd As Variant, ParamArray StackArray() As Variant)
Dim i As Long, Top As Long, Bottom As Long
Bottom = LBound(StackArray())
Top = UBound(StackArray())
For i = Bottom To (Top - 1)
StackArray(i) = StackArray(i + 1) 'move each item 1 position to the left
Next i
StackArray(Top) = ItemToAdd ' add the "item to add" to the last position in the array
End Sub</pre>
The problem is that the Ubound and Lbound functions return 0, even though the array is obviously larger.
I think I am completely misunderstanding how to pass arrays to a procedure. VBA help is not helping either. So if anyone can help me out, I'd really appreciate it.
Here is the "test" sub that I use to test out the "addtostack" sub.<pre/>
Sub test()
Dim tester(8) As Integer
Dim i As Integer
For i = 1 To 8
tester(i) = i
Next i
AddToStack 9, tester()
End Sub</pre>
I get no errors, but it just treats the array that I pass as if it has no elements.
Thanks for any help!!!! This is driving me nuts.
Also, if there is any built-in functionality in VBA to manipulate an array as a stack, please let me know. I have a feeling I may be re-inventing the wheel on this one.
This message was edited by John McGraw on 2002-05-21 00:46