Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.
'start of class Private stack() As String Private size_ As Integer Private count_ As Integer Public Sub Initialize(size As Integer) size_ = size ReDim stack(size_ - 1) End Sub Public Function Pop(push As String) As String If count_ < size_ Then stack(count_) = push count_ = count_ + 1 Pop = "" Else Dim popResult As String popResult = stack(0) Dim tempStack() As String ReDim tempStack(size_ - 1) 'copy to temp array but leave out the oldest item (source is stack(1)) CopyMemory VarPtr(tempStack(0)), VarPtr(stack(1)), Len(popResult) * (size_ - 1) 'put new item at the end of stack tempStack(size_ - 1) = push 'copy temp array back to stack CopyMemory VarPtr(stack(0)), VarPtr(tempStack(0)), Len(popResult) * size_ Pop = popResult End If End Function Public Function ToArray() As String() ToArray = stack() End Function 'end of class of class ' I have declared CopyMemory API function in module as this: Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _ (ByVal pDestination As Long, ByVal pSource As Long, ByVal ByteLen As Long) 'Finally, here is some test code, paste it into a form: Private Sub testStack_Click() Dim stack As New ConstSizeStringStack stack.Initialize (5) Dim i As Integer For i = 0 To 8 Dim itm As String * 10 itm = "STR" & CStr(i) 'prints nothing after returning STR0 and the content of internal 'stack array in debbuger shows error Debug.Print stack.Pop(itm) Next i Dim strs() As String strs = stack.ToArray Dim j As Integer For j = LBound(strs) To UBound(strs) Debug.Print strs(j) Next j End Sub
Add your voice to the tech community where 5M+ people just like you are talking about what matters.
Join the community of 500,000 technology professionals and ask your questions.