Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pop/push to array 1

Status
Not open for further replies.

EfratS

MIS
Aug 4, 2004
15
CA
Is there a convenient way to push/pop element to/from an array in vba?

Thanks!
 
If my answer is too simple for you then please rephrase your question because it seems to ask how to use an array.

If you have an array defined as such:
Dim aValues(10 to 20) as Integer

And you wanted to put something in to Array position 11,

aValues(11)=1

And to retrieve:
Debug.print aValues(11)

You can also define and populate an array like this:
Dim aValues as Variant
aValues=Array(1,2,3,4,10,11,12)

Now you can access the array aValues(0) to aValues(6) assuming the default Option Base is in effect.
 
ok, let me rephrase my question:

The size of my array is dynamic - it'll be changed during the running time:
If certain condition is true - I want to add element to my array. If another condition is true - I want to remove the elements from the array in FIFO order until it'll be empty.

The way to implement such thing in Perl, for example, is to use the "push" and "pop" functions of array.
In VBA, as I read so far, I saw only the "Redim" feature, that can change the size of the array.
Is it the only way to implement the described scenario in VBA?

Thanks!
 
Push and Pop normally implies a stack, LIFO, as opposed to a queue, FIFO.

Implementing LIFO is quite straightforward and can easily be implemented with ReDim Preserve.

Probably the easiest way to implement FIFO would be to use a combination of ReDim Preserve when adding to the array, and to use a variable to act as head of queue pointer, and comparing it to UBound(array) to determine when the queue is empty. You could create a Sub Push to add an item to the queue and a Function Pop to remove an item.

Hope this helps.
 
For FIFO, I would consider using a Collection Object.

For example:
Code:
Sub Demo()
Dim oMyCollection As New Collection

  oMyCollection.Add "Apples"
  oMyCollection.Add "Bananas"
  MsgBox oMyCollection(1)
  oMyCollection.Remove (1)
  oMyCollection.Add "Cantaloupes"
  MsgBox oMyCollection(1)
  oMyCollection.Remove (1)
  MsgBox oMyCollection(1)
  oMyCollection.Remove (1)
  
End Sub
Note that Apples and Bananas are put in the collection, Apples are processed and removed, Cantaloupes are added to the collection while Bananas are still there also. Then Bananas are processed and removed, followed finally by the last item added, Canataloupes.

I belive that is exactly what you described.

You should be able to develop the routine you need using those fundamental operations.

 
I think that if you had asked the original question with the depth of your second post you'd have been better off :) Usually you won't suffer any problems giving "Too Much Information" when seeking technical help.

Cheers
 
As hinted by earthandfire, one way of simulating pushing items onto the array 'stack' and poping them off would be to do something like:

Sub test()
'Define counters
Dim i As Integer
Dim j As Integer
'Define the array
Dim MyArray() As Integer
'Define the output
Dim Result As String
Result = ""
'Populate the array
For i = 1 To 10
ReDim Preserve MyArray(i)
MyArray(i) = i
Next
'Show what we've got
For j = 1 To UBound(MyArray)
Result = Result & vbCrLf & MyArray(j)
Next
MsgBox Result
'Clear the result
Result = ""
'Make space for a new item
ReDim Preserve MyArray(i)
'Add the new item
MyArray(i) = i
'Show what we've got
For j = 1 To UBound(MyArray)
Result = Result & vbCrLf & MyArray(j)
Next
MsgBox Result
'Clear the result
Result = ""
'Delete the new item
i = i - 1
ReDim Preserve MyArray(i)
'Show what we've got
For j = 1 To UBound(MyArray)
Result = Result & vbCrLf & MyArray(j)
Next
MsgBox Result
End Sub

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top