VB Migration Partner

KNOWLEDGE BASE - Forms and controls


Previous | Index | Next 

[PRB] Serializable properties in UserControls using system objects may crash Visual Studio

Consider the following property added to a VB6 UserControl:

        Private m_AppVersion As String

        Property Get AppVersion() As String
            ' if not explicitly set, return the application’s version
            AppVersion = App.Major & "." & App.Minor & "." & App.Revision
        End Property
  
        Property Let AppVersion(ByVal value As String)
            m_AppVersion = value
        End Property

The above property is translated to VB.NET as follows:

        Private m_AppVersion As String

        Public Property AppVersion() As String
            Get
                Return App6.Major & "." & App6.Minor & "." & App6.Revision
            End Get
            Set(ByVal value As String)
                m_AppVersion = value
            End Set
        End Property

As innocent as it may look, this code can crash Visual Studio. In fact, if you migrate a VB6 form that contains this user control, then load the migrated form into Visual Studio and attempt to modify the form (for example, by resizing it or moving one of its controls), then Visual Studio is forced to serialize all the UserControl’s properties in the code-behind sections of the form.

The crash occurs because, when Visual Studio is in design-mode, the App6 object isn’t initialized and any reference to it causes an exception (usually a NullReference exception). In fact, the App6 object is valid only when the project is running.

You can solve this problem in a couple ways. The first solution: you specify that the property should not be serialized, by decorating it with the following attributes:

        <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
        <Browsable(False)> _
        Public Property AppVersion() As String
            ' ...
        End Property

If the property must be serialized somehow, you should modify the code inside the Get block so that the App6 object isn’t referenced if the project is in design-mode, as follows:

        Public Property AppVersion() As String
            Get
                ' if in design mode, return a dummy value
                If Me.DesignMode Then Return "1.0.0"
                Return App6.Major & "." & App6.Minor & "." & App6.Revision
            End Get
            Set(ByVal value As String)
                m_AppVersion = value
            End Set
        End Property

NOTE: the same problem can occur with other system objects, such as Screen or Printers.

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