Previous | Index | Next 

[PRB] Arrays held in Variant and Object variables are converted in a special way

Under VB6 it is legal to apply the ReDim, ReDim Preserve, and Erase keywords to a scalar (non-array) Variant variable:

        Dim arr() As Double
        Dim v As Variant
        v = arr
        ReDim v(20) As Double
        v(10) = 123

VB Migration Partner supports this syntax and generates a call to the Redim6 or RedimPreserve6 helper methods, defined in the support library. These two methods work exactly like the VB6 keywords, except for one detail: if the Object or VB6Variant variable holds an array of UDTs (i.e. .NET structures), then the structure might not be initialized correctly. For example, if the UDT contains fixed-length strings, auto-instantiating (As New) objects, or static arrays, then those elements won’t be initialized to their correct value.

The only workaround for this problem is that you manually initialize each element of the array after performing the Redim6 method (and the RedimPreserve6 method, if the array has been extended)

        ' in this VB.NET example, MyUDT is a structure that must be initialized
        Dim arr() As MyUDT
        Dim v As Object
        v = arr
        ReDim6(v, 1, 20)
        ' initialize each array element, using the extra constructor that
        ' has been generated by VB Migration Partner
        For i As Integer = 0 To 20
            v(i) = New MyUDT(False)
        Next
        v(10) = 123

A similar problem occurs when the Erase keyword is applied to an Object or VB6Variant variable that holds a static array. In this case it is recommended that you manually re-initialize each element of the array.

 

Previous | Index | Next