Export (0) Print
Expand All

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 2005. 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 2005 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.

    NoteNote

    The Upgrade Wizard is a tool used to upgrade a Visual Basic 6.0 application to Visual Basic 2005. It is automatically launched when you open a Visual Basic 6.0 project in Visual Basic 2005. 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 As String
        Dim LastName As String
        Dim Id As Integer
    
        Public Function FormatFullName() As String
            Return FirstName & " " & LastName
        End Function
    End Structure
    
    

The Upgrade Wizard will automatically upgrade a Visual Basic 6.0 type to a Visual Basic 2005 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 As String
        Dim LastName As String
        Private idValue As Integer
    
        Public Property Id() As Integer
            Get
                Return idValue
            End Get
            Set(ByVal Value As Integer)
                idValue = Value
            End Set
        End Property
    
        Public Function FormatFullName() As String
            Return FirstName & " " & LastName
        End Function
    End Class
    
    

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() As String
        Property LastName() As String
        Property Id() As String
    
        Function FormatFullName() As String
    End Interface
    
    
  6. The Interface can then be implemented by another class. The definition is shown below.

    Class NewCustomer
        Implements Customer
    
        Public Property FirstName() As String Implements Customer.FirstName
            Get
                ' Add code here.
            End Get
            Set(ByVal Value As String)
                ' Add code here.
            End Set
        End Property
    
        Public Property Id() As String Implements Customer.Id
            Get
                ' Add code here.
            End Get
            Set(ByVal Value As String)
                ' Add code here.
            End Set
        End Property
    
        Public Property LastName() As String Implements Customer.LastName
            Get
                ' Add code here.
            End Get
            Set(ByVal Value As String)
                ' Add code here.
            End Set
        End Property
    
        Public Function FormatFullName() As String _
            Implements Customer.FormatFullName
            ' Add code here.
        End Function
    End Class
    
    

See Also

Community Additions

ADD
Show:
© 2014 Microsoft