passing an array to a procedure

The following produces an error msg of ...array or user-defined type expected. Ive tried sending various ways but all produce error msgs. Can you please help.
Thanks,
Mark


Private Sub form_load()

Call ProcedureName("a", "b", "c", "d")

End Sub


Public Sub ProcedureName(x() As String)

MsgBox x(0)
MsgBox x(1)
MsgBox x(2)
MsgBox x(3)

End Sub

Comments

  • : The following produces an error msg of ...array or user-defined type expected. Ive tried sending various ways but all produce error msgs. Can you please help.
    : Thanks,
    : Mark
    :
    :
    : Private Sub form_load()
    :
    : Call ProcedureName("a", "b", "c", "d")
    :
    : End Sub
    :
    :
    : Public Sub ProcedureName(x() As String)
    :
    : MsgBox x(0)
    : MsgBox x(1)
    : MsgBox x(2)
    : MsgBox x(3)
    :
    : End Sub

    Two options depending on whether you want to pass an array or you want to pass an undetermined number of parameters:
    [code]
    Dim Arr() As String
    ReDim Arr(0 To 3)
    Arr(0) = "a"
    Arr(1) = "b"
    Arr(2) = "c"
    Arr(3) = "d"
    ProcName Arr

    Public Sub ProcName(tArr() As String)

    MsgBox tArr(0)
    MsgBox tArr(1)
    MsgBox tArr(2)
    MsgBox tArr(3)

    End Sub
    [/code]
    [code]
    ProcName "a", "b", "c", "d"

    Public Sub ProcName(ParamArray tArr() As Variant)

    MsgBox tArr(0)
    MsgBox tArr(1)
    MsgBox tArr(2)
    MsgBox tArr(3)

    End Sub
    [/code]
    The second option is slower since a ParamArray [italic]must[/italic] be of type Variant, but it comes in handy sometimes...
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion