VB Migration Partner

KNOWLEDGE BASE - Language


Previous | Index | Next 

[PRB] Passing a ParamArray to another method

Consider the following VB6 code:

    Sub Main()
        One "abc"
    End Sub
    
    Private Sub One(ParamArray args() As Variant)
        Debug.Print args(0)          ' => displays "abc"
        Two args  
    End Function

    Private Sub Two(ParamArray args() As Variant)
        Debug.Print args(0)(0)       ' => displays "abc"
        Three args
    End Function

    Private Sub Three(ParamArray args() As Variant)
        Debug.Print args(0)(0)(0)    ' => displays "abc"
    End Function

The remarks in code make it clear that the args ParamArray in method One is passed into the first element of the args ParamArray in method Two, and the args ParamArray in method Two is passed in the first element of the args ParamArray in method Three.

Let’s consider now a similar code snippet in VB.NET:

    Sub Main()
        One("abc")
    End Sub
    
    Private Sub One(ParamArray args() As Object)
        Debug.Print(args(0))          ' => displays "abc"
        Two(args)
    End Function

    Private Sub Two(ParamArray args() As Object)
        Debug.Print(args(0))          ' => displays "abc"
        Three(args)
    End Function

    Private Sub Three(ParamArray args() As Object)
        Debug.Print(args(0))          ' => displays "abc"
    End Function

The remarks in the VB.NET code suggest that the args ParamArray in method One is passed into the args ParamArray in method Two, which in turn is passed in the args ParamArray in method Three. In other words, the original args array is passed along to all methods in the call chain.

VB Migration Partner can’t compensate for this important difference between VB6 and VB.NET. If the original piece of VB6 code is migrated, then the converted VB.NET code is likely to throw either compilation or runtime errors. For example, you have a compilation error if the call statement includes additional arguments, as in:

    Private Sub Two(ParamArray args() As Object)
        Debug.Print(args(0))          ' => displays "abc"
        Three(args, "def")            ' => compilation error
    End Function

The simplest way to avoid compilation errors and reproduce the VB6 behavior is to manually create the array passed to further methods in the call chain, as in this example:

    Sub Main()
        One("abc")
    End Sub
    
    Private Sub One(ParamArray args() As Object)
        Debug.Print(args(0))          ' => displays "abc"
        Dim tempArr() As Object = {args}
        Two(tempArr)
    End Function

    Private Sub Two(ParamArray args() As Object)
        Debug.Print(args(0)(0))       ' => displays "abc"
        Dim tempArr() As Object = {args, "def"}
        Three(tempArr)
    End Function

    Private Sub Three(ParamArray args() As Object)
        Debug.Print(args(0)(0)(0))   ' => displays "abc"
    End Function
Previous | Index | Next 




Follow Francesco Balena on VB6 migration’s group on

LinkedIn





Read Microsoft Corp’s official case study of a VB6 conversion using VB Migration Partner.




Code Architects and its partners offers remote and onsite migration services.

More details




Subscribe to our free newsletter for useful VB6 migration tips and techniques.

newsletter



To learn more about your VB6 applications, run VB6 Analyzer on your source code and send us the generated text file. You will receive a detailed report about your VB6 applications and how VB Migration Partner can help you to quickly and effectively migrate it to .NET.

Get free advice



A fully-working, time-limited Trial Edition of VB Migration Partner allows you to test it against your actual code

Get the Trial




The price of VB Migration Partner depends on the size of the VB6 application, the type of license, and other factors

Request a quote




Migrating a VB6 application in 10 easy steps

Comparing VB Migration Partner with Upgrade Wizard

Migration tools: Feature Comparison Table

All whitepapers