Export (0) Print
Expand All
Expand Minimize
1 out of 1 rated this helpful - Rate this topic

LSet may not be used to assign a UDT from one type to another

Visual Studio .NET 2003

In Visual Basic 6.0, the LSet statement could optionally be used to copy a variable of one user-defined type into a variable of another user-defined type. The primary use for the LSet statement was to left align a string within a string variable; the use of LSet to copy user-defined types was not recommended.

Visual Basic .NET does not allow the use of LSet to assign one user-defined type (now structure) to another. When a Visual Basic 6.0 application is upgraded, code that uses LSet for assignment will be flagged as an upgrade issue.

The LSet method in Visual Basic .NET can still be used to left align strings; the new PadLeft method provides additional capabilities and may be a more suitable replacement.

The following code illustrates the use of the LSet statement:

' Visual Basic 6.0
Private Type typTypeA
   x As Integer
End Type
Private Type typTypeB
   x As Integer
End Type

Sub AssignType()
   Dim aType As typTypeA
   Dim bType As typTypeB
   LSet aType = bType
End Sub

Sub StringAlign()
   Dim strString1 As String
   Dim strString2 As String
   LSet strString1 = strString2
End Sub

' After upgrade to Visual Basic .NET 
Private Structure typTypeA
   Dim x As Short
End Type
Private Structure typTypeB
   Dim x As Short
End Type

Sub AssignType()
   Dim aType As typTypeA
   Dim bType As typTypeB
   ' UPGRADE_ISSUE: LSet cannot assign one type to another.
   aType = LSet(bType)
End Sub

Sub StringAlign()
   Dim strString1 As String
   Dim strString2 As String
   ' The statement is replaced by a method.
   strString1 = LSet( strString2, Len(strString1) )
End Sub

What to do next

  • Review your code to determine if the user-defined types contain the exact same types. Visual Basic 6.0 sometimes used type coercion to automatically convert a variable of one type to another; in Visual Basic .NET, type mismatches will cause a compilation error.
  • If the user-defined types are an exact match, you can modify the above code as follows:
    Private Structure typTypeA
       Dim x As Integer
    End Type
    Private Structure typTypeB
       Dim x As Integer
    End Type
    
    Sub AssignType()
       Dim aType As typTypeA
       Dim bType As typTypeB
       AType.x = bType.x
    End Sub
    

    For structures that contain multiple elements, you will need to assign each element individually.

See Also

String.PadLeft Method

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.