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
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
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.
where the second argument ensures that the file is added as a link rather than being copied in each and every project.