VB Migration Partner

KNOWLEDGE BASE - Language


Previous | Index | Next 

[HOWTO] Generate auto-implemented properties for Visual Basic 2010

Most VB6 properties are just wrappers for a private variable, as in the following example:

        Private m_ID As Long
        Private m_Name As String
		
        Public Property Get ID() As Long
            ID =  m_ID
        End Property
		
        Public Property Get Name() As String
            Name =  m_Name
        End Property

        Public Property Let Name(ByVal value As String)
            m_Name = value
        End Property

When converted through VB Migration Partner, the above code generates these VB.NET statements:

        Private m_ID As Integer
        Private m_Name As String = ""
  
        Public Readonly Property ID() As Integer
            Get
                Return  m_ID
            End Get  
        End Property
  
        Public Property Name() As String
            Get
                Return m_Name
            End  Get
            Set(ByVal value As String)
                m_Name  = value
            End Set
        End Property

In cases like this – that is, when a property is merely a wrapper for a private variable and doesn’t perform any additional action – you can generate a new cool feature of Visual Basic 2010: auto-implemented properties. In fact, in VB2010 you can rewrite the above code block with just two lines:

        Public ReadOnly Property ID As Integer
        Public Property Name As String = ""

VB Migration Partner doesn’t include an explicit option to generate auto-implemented properties. However, you can easily tweak the converted VB2010 code to reach this goal. In fact, you just need three pragmas:

        '## REM this pragma tranforms the variable declaration into an implemented property
        '## PostProcess "\b(Dim|Private)\s+m_(?<prop>Name)(?<type>\s+As\s+.+\r\n)", 
               "Public Property ${prop}${type}"

        '## REM same as previous pragma, but this is for readonly properties
        '## PostProcess "\b(Dim|Private)\s+m_(?<prop>ID)(?<type>\s+As\s+.+\r\n)", 
               "Public ReadOnly Property ${prop}${type}"

        '## REM This pragma deletes the Property procedures
        '## PostProcess "[\t]*(Public|Friend|Private)\s+(ReadOnly\s+)?Property\s+
               (ID|Name)\b.+\r\n(.+\r\n)+?\s*End Property\r\n", "" 

The interesting point is that you need only three pragmas regardless of how many properties you want to process. For example, suppose that you have a class that contains 8 properties that can be transformed into auto-implemented properties:
            FirstName, LastName, Address, City, ZipCode, Country (read-write)
            ID, Age (read-only)
These are the three pragmas that you need:

    '## PostProcess "\b(Dim|Private)\s+m_(?<prop>FirstName|LastName|Address|City|ZipCode|Country)
           (?<type>\s+As\s+.+\r\n)", "Public Property ${prop}${type}"

    '## PostProcess "\b(Dim|Private)\s+m_(?<prop>ID|Age)(?<type>\s+As\s+.+\r\n)", 
           "Public ReadOnly Property ${prop}${type}"

    '## PostProcess "[\t]*(Public|Friend|Private)\s+(ReadOnly\s+)?Property\s+
           (FirstName|LastName|Address|City|ZipCode|Country|ID|Age)\b.+\r\n(.+\r\n)+?
           \s*End Property\r\n", "" 

If the original VB6 code accesses the private variable outside the property procedure, you need one more pragma to ensure that all references to the variable are rendered as referenced to the property

    '## PostProcess "\bm_(?<prop>FirstName|LastName|Address|City|ZipCode|Country|ID|Age)\b", 
           "${prop}"

Please notice that if the property is marked as ReadOnly and you access the inner variable in places other than the Class_Initialize event handler, than you can't render the property as an auto-implemented property.

NOTE: even if generating auto-implemented properties is very simple, we recommend that you adopt this technique only during the optimization phase, at the very end of your migration project. A standard (non auto-implemented) property gives you more flexibility, allow you to trace assignments, allow you to set breakpoints, to validate incoming values, and so forth.

Previous | Index | Next 




Follow Francesco Balena on VB6 migration’s group on

LinkedIn





Read Microsoft Corp’s official case study of a VB6 conversion using VB Migration Partner.




Code Architects and its partners offers remote and onsite migration services.

More details




Subscribe to our free newsletter for useful VB6 migration tips and techniques.

newsletter



To learn more about your VB6 applications, run VB6 Analyzer on your source code and send us the generated text file. You will receive a detailed report about your VB6 applications and how VB Migration Partner can help you to quickly and effectively migrate it to .NET.

Get free advice



A fully-working, time-limited Trial Edition of VB Migration Partner allows you to test it against your actual code

Get the Trial




The price of VB Migration Partner depends on the size of the VB6 application, the type of license, and other factors

Request a quote




Migrating a VB6 application in 10 easy steps

Comparing VB Migration Partner with Upgrade Wizard

Migration tools: Feature Comparison Table

All whitepapers