VB Migration Partner

KNOWLEDGE BASE - Language


Previous | Index | Next 

[HOWTO] Modify the behavior of methods and classes in VB.NET library and VB Migration Partner support library

For example, a developer might be puzzled by the fact that the following VB6 statement

        Dim res As Variant, num As Integer
        res = Trim(rs("Name").Value)
        num = Len6(rs("Name").Value)

assigns Null to the res variable if the Name field is NULL, whereas both the Trim method (in VB.NET library) and the Len6 method (in VB Migration Partner’s library) throw an exception:

        Dim res As Object = Trim(rs("Name").Value)
        Dim num As Short = Len6(rs("Name").Value)

The reason for the exception is that the VB6 Trim and Len methods take and return a Variant value, whereas the VB.NET’s Trim and Len6 methods only work with strings and aren’t capable to deal with DBNull values arriving from the database.

Some customers might believe that this is a limitation of VB Migration Partner’s support library, but the truth is, there is no way to perfectly mimick the behavior of VB6 Variants under VB.NET, even though our VB6Variant class often does a decent job.

In this article we show a simple technique to customize the way VB.NET and VB Migration Partner’s methods work, without any major impact on the migrated code. In fact, the technique is as simple as “shadowing” the VB.NET and VB Migration Partner’s methods with a method with same name defined in the migrated project.

For the sake of illustration, let’s say that you want to modify the Trim function so that it returns an Object (instead of a string) and that it returns a DBNull value when the argument is a DBNull, and that you want to modify the Len6 method so that it returns 0 when the argument is a DBNull value.

To shadow these two methods, create a VB.NET module and name it appropriately, for example CustomMethods, then write this code

        Public Module CustomMethods

            Public Function Trim(ByVal value As Object) As Object
                If TypeOf value Is DBNull Then
                    Return value
                Else
                    ' forward to the method in VB.NET library
                    Return Microsoft.VisualBasic.Trim(value)
                End If
            End Function
  
            Public Function Len6(ByVal value As Object) As Integer
                If TypeOf value Is DBNull Then
                    Return  0
                Else
                    ' forward to the method in VB Migration Partner’s library
                    Return CodeArchitects.VB6Library.Len6(value)
                End If
            End Function
  
        End Module

You can then save the module in a file – e.g. CustomMethods.vb – and share it with all the projects of your application. You can automatically include this file in all your migrated projects by means of the AddSourceFile pragma.

        '## AddSourceFile "c:\files\custommethods.vb", True

where the second argument ensures that the file is added as a link rather than being copied in each and every project.

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