Puede utilizar la pila de clases en System.Collections, como se puede usar cola y otros. Simplemente busque vb.net stack para documentación. No he probado todos los métodos (por ejemplo, Getenumerator: no sé cómo usar un iterador, si es posible en VBA). El uso de una pila o una cola le brinda algunos beneficios agradables, normalmente no tan fáciles en VBA. Puede usar
anArray = myStack.ToArray
EVEN si la pila está vacía (Devuelve una matriz de tamaño 0 a -1).
Al usar un objeto de colecciones personalizado, funciona muy rápido debido a su simplicidad y se puede volver a escribir fácilmente (por ejemplo, para manejar solo varibles fuertemente tipadas). Es posible que desee verificar la pila vacía. Si intenta utilizar Pop en una pila vacía, VBA no lo manejará correctamente, como todos los objetos nulos.Me pareció más razonable utilizar:
If myStack.Count > 0 Then
de la función utilizando la pila, en lugar de hornearlo en clsStack.Pop. Si lo preparas en la clase, una llamada a Pop puede devolver un valor del tipo elegido; por supuesto, puedes usarlo para manejar valores vacíos, pero obtienes mucho más dolor de esa manera.
Un ejemplo de uso:
Private Sub TestStack()
Dim i as long
Dim myStack as clsStack
Set myStack = New clsStack
For i = 1 to 2
myStack.Push i
Next
For i = 1 to 3
If myStack.Count > 0 Then
Debug.Print myStack.Pop
Else
Debug.Print "Stack is empty"
End If
Next
Set myStack = Nothing
End Sub
Usando una pila LIFO-puede ser extremadamente útil!
Clase clsStack
Dim pStack as Object
Private Sub Class_Initialize()
set pStack = CreateObject("System.Collections.Stack")
End Sub
Public Function Push(Value as Variant)
pStack.Push Value
End Function
Public Function Pop() As Variant
Pop = pStack.Pop
End Function
Public Function Count() as long
Count = pstack.Count
End Function
Public Function ToArray() As Variant()
ToArray = pStack.ToArray()
End Function
Public Function GetHashCode() As Integer
GetHashCode = pStack.GetHashCode
End Function
Public Function Clear()
pStack.Clear
End Function
Private Sub Class_terminate()
If (Not pStack Is Nothing) Then
pStack.Clear
End If
Set pStack = Nothing
End Sub
¡Bienvenido a stackoverflow! +1 para proporcionar algún código y mostrar lo simple que puede ser. Algunas advertencias: 1) El uso de '=' para devolver un valor en sus rutinas 'Push' y 'Pop' fallará cuando los objetos estén involucrados debido a la molesta sintaxis 'Set' de VBA. Consulte el final de esta respuesta: http://stackoverflow.com/questions/4716382/excel-select-case/4719706#4719706 2) Tenga en cuenta que la indexación en una Colección es O (n) por tiempo. Ver http://stackoverflow.com/questions/4827963/what-is-the-difference-between-the-time-complexity-of-these-two-ways-of-using-loo/4830157#4830157 – jtolle