Passing an array of any data type as an argument to a procedure

Cosmos75

Registered User.
Local time
, 19:39
Joined
Apr 22, 2002
Messages
1,281
I have the following code.
Code:
[B]Sub [COLOR="Blue"]CreateArray[/COLOR]()[/B]
Dim lngArray(1 To 10) As Long
Dim i As Long
For i = LBound(lngArray) To UBound(lngArray)
    lngArray(i) = i
Next i
DebugPrintArray lngArray()
[B]End Sub[/B]

[B]Sub [COLOR="blue"]DebugPrintArray[/COLOR](AnArray() As Long)[/B]
Dim i As Long
For i = LBound(AnArray) To UBound(AnArray)
    Debug.Print "AnArray (" & i & ") = " & AnArray(i)
Next i
[B]End Sub[/B]
But I would like to be able to have a DebugPrintArray() function that can handle an array of any data type. I've looked around and I think that you can only pass arrays of the exact same data type.

Code:
[COLOR="Green"]'This will not work.[/COLOR]
[B]Sub [COLOR="blue"]DebugPrintArray[/COLOR](AnArray() As [COLOR="Red"]Variant[/COLOR])[/B]
....
Does anyone know of a way around this?
:confused:

Related Links
ACC2000: How to Pass an Array as an Argument to a Procedure
Type Mismatch: Array or User Defined Type Expected
 
Last edited:
You could try a class module.

The array would have class scope, as opposed to the life of the original routine.

It worked in my test.

-Mike
 
Found the answer. I have to declare the array argument as a VARIANT variable, not as a VARIANT array.
Code:
Sub DebugPrintArray(AnArray As [B][COLOR="Blue"]Variant[/COLOR][/B])
Dim i As Long
For i = LBound(AnArray) To UBound(AnArray)
    Debug.Print "AnArray (" & i & ") = " & AnArray(i)
Next i
End Sub
 
Last edited:

Users who are viewing this thread

Back
Top Bottom