Passing Optional Arrays to a Sub

In Visual Basic 2005, I am trying to create a sub in a class that takes an optional string(). However, I am unable to figure out how to set the default value. I have already tried:

Public Sub getData(Optional ByVal postData() as String = {"", ""})

However, I get an expression required error at the {. Also, I tried the New String(), but then I get the error saying it must be a constant. What am I missing

Thanks.



Answer this question

Passing Optional Arrays to a Sub

  • Padmaja T Chavali

    You can indeed have an array as an optional parameter - you just need to alter that slightly:

    Public Sub getData(Optional ByVal postData() As String = Nothing)

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    Instant C++: C# to C++ converter, VB to C++ converter
    Clear VB: Cleans up VB code
    C# Code Metrics: Quick metrics for C#



  • Teo97917

    Optional Pararmeters require a constant and as a constant cannot be declared as an array you can't do this.

    Why not use an overloaded function -

    Private Sub GeData(ByVal postData As String())

    ' my work

    End Sub

    Private Sub GeData()

    Dim strings() As String = {"", ""}

    Me.GeData(Strings)

    End Sub


  • Tomys

    Okay, thanks a lot.
  • Heinz Krug

    As per David's post

    It turns out that I am incorrect.

    Declaring the parameter with a default value of nothing will suffice. However, you cannot pass a default array of values.


  • Passing Optional Arrays to a Sub