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"
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"
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:
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:
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