Dela via


How to: Convert a User-Defined Type to a Visual Basic Structure

The Type statement of Visual Basic 6.0 is upgraded to the Structure statement of Visual Basic 2008. Two other options for upgrading the type are also discussed in this topic: converting to a class and converting to an interface.

The following Visual Basic 6.0 type is discussed in the procedures that follow:

Type Customer
    Dim FirstName As String
    Dim LastName As String
    Dim Id As Long
End Type

Private Function FormatFullName(aCustomer As Customer) As String
    FormatFullName = aCustomer.FirstName & " " & aCustomer.LastName
End Function

The Upgrade Wizard automatically upgrades a Visual Basic 6.0 type to a Visual Basic 2008 structure. The Structure is a type that supports members, such as methods and properties.

To upgrade a type to a structure

  1. Run the Upgrade Wizard.

    Note

    The Upgrade Wizard is a tool used to upgrade a Visual Basic 6.0 application to Visual Basic 2008. It is automatically launched when you open a Visual Basic 6.0 project in Visual Basic 2008. For more information, see How to: Upgrade a Project with the Visual Basic Upgrade Wizard.

  2. If you are interested in taking advantage of other features of the Structure type, look for methods that more properly belong in the structure. In this example, the FormatFullName method could be converted to a FormatFullName method in the structure:

    Structure Customer
        Dim FirstName AsStringDim LastName AsStringDim Id AsIntegerPublicFunction FormatFullName() AsStringReturn FirstName & " " & LastName
        EndFunctionEndStructure
    

The Upgrade Wizard will automatically upgrade a Visual Basic 6.0 type to a Visual Basic 2008 structure. The Class type supports the same members as a Structure, but is a reference type. A class can serve as a base class, while a structure cannot.

To upgrade to a class

  1. Run the Upgrade Wizard.

  2. Replace Structure with Class.

  3. If you are interested in taking advantage of other features of the Class type, look for methods that more properly belong in the structure. The following code shows the incorporation of FormatFullName into the class, and the Id property.

    Class Customer
        Dim FirstName AsStringDim LastName AsStringPrivate idValue AsIntegerPublicProperty Id() AsIntegerGetReturn idValue
            EndGetSet(ByVal Value AsInteger)
                idValue = Value
            EndSetEndPropertyPublicFunction FormatFullName() AsStringReturn FirstName & " " & LastName
        EndFunctionEndClass
    

A third option is to convert the structure to an interface.

To upgrade to an interface

  1. Run the Upgrade Wizard.

  2. Replace Structure with Interface.

  3. Replace variables with property declarations. Do not include the implementation.

  4. Add methods, but remove the implementation.

  5. The code might look like this:

    Interface Customer
        Property FirstName() AsStringProperty LastName() AsStringProperty Id() AsStringFunction FormatFullName() AsStringEndInterface
    
  6. The Interface can then be implemented by another class. The definition is shown below.

    Class NewCustomer
        Implements Customer
    
        PublicProperty FirstName() AsStringImplements Customer.FirstName
            Get            ' Add code here.EndGetSet(ByVal Value AsString)
                ' Add code here.EndSetEndPropertyPublicProperty Id() AsStringImplements Customer.Id
            Get            ' Add code here.EndGetSet(ByVal Value AsString)
                ' Add code here.EndSetEndPropertyPublicProperty LastName() AsStringImplements Customer.LastName
            Get            ' Add code here.EndGetSet(ByVal Value AsString)
                ' Add code here.EndSetEndPropertyPublicFunction FormatFullName() AsString _
            Implements Customer.FormatFullName
            ' Add code here.EndFunctionEndClass
    

See Also

Tasks

How to: Upgrade a Project with the Visual Basic Upgrade Wizard

Reference

Structure Statement

Class Statement (Visual Basic)

Interface Statement (Visual Basic)