VB Migration Partner

KNOWLEDGE BASE - Classes and COM objects


Previous | Index | Next 

[HOWTO] Work with auto-instancing arrays

VB6 supports auto-instancing arrays, that is, array of objects declared with the As New, as in this code:

    Dim arr(10) As New Company

Auto-instancing arrays are peculiar in that – as it happens with auto-instancing scalar (nonarray) variables – if your code references an element that is currently equal to Nothing then a new instance is automatically created

    Dim arr(10) As New Company
    arr(1).Name = "Code Architects"    ' no null reference error here in VB6

VB.NET doesn’t support auto-instancing variables or arrays. However, in most cases the VB6 developer isn’t really interested in auto-instantiation semantics – that is, the object being instantiated on the first reference to it – and would be satisfied with a plain instantiation of all the array elements as soon as the array itself is created. Following this assumption, VB Migration Partner migrates the above code as follows:

    Dim arr() As Company = CreateArray6(Of Company)(0, 10)
    arr(1).Name = "Code Architects"    ' no null reference error here in VB.NET either

where the CreateArray6 method correctly instantiates all the elements in the array. VB Migration Partner can deal with these arrays even when they appear in ReDim and ReDim Preserve statements. For example, the following VB6 code:

    Dim arr() As New Company
    Dim arr2(10) As New Company

    Sub Test()
        ReDim arr(10, 20) As Company	
        ReDim Preserve arr2(20) As Company
    End Sub

is converted to VB.NET as follows:

Dim arr() As Company
    Dim arr2() As Company = CreateArray6(Of Company)(0, 10)

    Sub Test()
        ReDim6(arr, 0, 10, 0, 20) 
        ReDimPreserve6(arr2, 0, 20)
    End Sub

where the special ReDim6 and ReDimPreserve6 methods take care of initializing all the array elements as necessary.

If auto-instancing semantics is really important, you can insert an AutoNew pragma that specifies that the auto-instancing semantics of the array must be preserved:

    '## arr.AutoNew
    Dim arr(10) As New Company
    arr(1).Name = "Code Architects"

When this pragma is used, all references to array elements are wrapped by a call to the AutoNew6 special methods, which ensures that the array element exists. For example, the previous code snippet would be converted to VB.NET as follows:

    Dim arr(10) As New Company
    AutoNew6(arr(1)).Name = "Code Architects"

Alternatively, you can decide to implement “true” auto-instancing arrays, by forcing VB Migration Partner to render the array as a VB6ArrayNew object. You do so by combining the AutoNew pragma with the ArrayBounds pragma, as in this example:

    '## arr.AutoNew
    '## arr.ArrayBounds ForceVB6Array
    Dim arr(10) As New Company
    arr(1).Name = "Code Architects"

which translates to:

    Dim arr As New VB6ArrayNew(Of Widget)(0, 10)
    arr(1).Name = "Code Architects"

A VB6ArrayNew object works exactly as a VB6Array object, except that it implements lazy instantiation: if you attempt to access an element that is currently Nothing, an instance is automatically created. The VB6ArrayNew class inherits from VB6Array, therefore a VB6ArrayNew object can be used whenever a VB6Array is allowed, as in this example:

    Dim arr As VB6Array(Of Integer)
    arr = GetArray(10)
    '...

    Function GetArray(ByVal numEls As Short) As VB6ArrayNew(Of Integer) 
        ' ...
    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