This documentation is archived and is not being maintained.

Initialize must be called to initialize instances of this structure

Visual Studio .NET 2003

In Visual Basic 6.0, all members of a user-defined type (UDT) were initialized when they were declared inside the Type statement block.

In Visual Basic .NET, user-defined types are replaced by structures. Members that require parameterless constructors (such as fixed-length strings, fixed-size arrays, and variables declared with the New keyword) require initialization. During upgrade, an Initialize() subroutine is added to the structure. Calls to the Initialize subroutine must be added whenever the structure is declared.

The following code illustrates the upgrade of code that contains a fixed-size array in a user-defined type:

' Visual Basic 6.0
Public Type DriveInfo
   DriveNumber(25) As Integer
   DriveType As String
End Type

Function AddDrive(DriveNumber As Integer, DriveLabel As String)
   Dim Drives As DriveInfo
   Drives.DriveNumber(0) = 123
   Drives.DriveType = "Fixed"
End Function
   
' After upgrade to Visual Basic .NET
Public Structure DriveInfo
   ' Note that the fixed size was removed.
   Dim DriveNumber() As Short
   Dim DriveType As String
   ' UPGRADE_TODO: "Initialize" must be called to initialize instances of this structure.
   Public Sub Initialize()
      ReDim DriveNumber(25)
   End Sub
End Structure

Function AddDrive(ByRef Number As Short, ByRef DriveLabel As String) As Object
   Dim Drives As DriveInfo
   ' This will fail; the array has not been initialized.
   Drives.DriveNumber(0) = 123
   Drives.DriveType = "Fixed"
End Function

What to do next

  • Find each occurrence in your code where an instance of the user-defined type is declared.
  • Add a call to the Initialize() subroutine. In the example above, the AddDrive function would look like the following.
    Function AddDrive(ByRef Number As Short, ByRef DriveLabel As String) As Object
       Dim Drives As DriveInfo
       Drives.Initialize()
       Drives.DriveNumber(0) = 123
       Drives.DriveType = "Fixed"
    End Function
    

See Also

Array Size Declaration Changes in Visual Basic

Show: