PDA

View Full Version : Solved: ReDim (Preserve) within procedure argument passed ByRef



kunguito
05-12-2008, 12:17 AM
Is it possible to ReDim an array within a procedure passing it by reference? Obviously the array was declared outside the procedure.

Thanks!

Oorang
05-12-2008, 12:14 PM
In short... Yes that will work fine:) Here is an example to play with.

Option Explicit

Public Sub Example()
Dim strLocalArray() As String
ReDim strLocalArray(2) As String
strLocalArray(0) = "Foo"
strLocalArray(1) = "Bar"
strLocalArray(2) = "Baz"
DisplayArray strLocalArray, "This is the array before we pass it:"
AddElement strLocalArray
DisplayArray strLocalArray, "This is the redimmed array while in original scope:"
End Sub

Private Sub AddElement(ByRef myArray() As String)
ReDim Preserve myArray(3)
myArray(3) = "Pie"
DisplayArray myArray, "This is the redimmed array while in new scope:"
End Sub

Private Sub DisplayArray(display() As String, title As String)
Dim strMsg As String
Dim lngIndx As Long
strMsg = title
For lngIndx = LBound(display) To UBound(display)
strMsg = strMsg & (vbNewLine & Format$(lngIndx, "(0)=") & display(lngIndx))
Next
MsgBox strMsg, vbInformation, title
End Sub

kunguito
05-13-2008, 02:04 AM
Thanks Oorang!