KnowDotNet Visual Organizer

VB6 To VB.NET Gotchas

VB6 Compatability Functions

by Les Smith
Print this Article Discuss in Forums

In converting VB6 Applications to VB.NET, I many times do not use the migration tool that comes with VB.NET.  There are several reasons for this.  First, if ADO is involved, the tool does convert to ADO.Net.  That would take a tool that is much more powerful than the migration tool and I am not sure that any such tool could catch everything.  Secondly, the migration tool generates hundreds of diagnostics and warnings, many of which could be ignored, except that you have to go to the trouble of deleting them after you have looked at them to see whether they can be ignored.  This is certainly not to imply that you can ignore all of the warnings or errors.  

I often find it easier to do the conversion manuallly, becuase the errors will be obvious once you drop a method from VB6 into VB.Net, especially if you do not have an import and Reference to the classic ADO object library.  There are several gotchas that will grab at run-time, and not necessarily at compile time.

The first and one of the most dangerous is the old ByRef/ByVal problem.  Many times developers in VB6 did not declare whether a value is expected to be passed to a method
ByVal or ByRef.  In VB6, the compiler would not automatically fill this in for the developer as VB.Net does.  For example, consider the following code snippet in VB6.  The developer is passing a reference to a string that he expects to have a value returned in.  The developer used a Sub rather than a Function because he/she needed to have the method return more than one value.

     Dim s As String
     Dim s2 As String
     MethodOne(s, s2)

     ........

     Private Sub MethodOne(p1 As String, p2 As String)
        p1 = "A"
        p2 = "B"
     End Sub


In VB6, when the return is made to the calling code, s will be set to "A" and s2 will be set to "B".  But in VB.Net, s and s2 will both be
Nothing.  The reason for this is that VB6 default passing was ByRef.  In VB.Net, it changed to ByVal.  The references must be passed ByRef, or nothing will be returned.  

Here's another one that is just as subtle!  The Format function is a great tool for VB programmers.  The same functionality is not provided to C# programmers, and this may be one of the many things that causes C# developers to say that VB Developers are pampered...whatever.  I use both languages and see pros and cons to both.  The problem between VB6 and VB.Net with respect to the Format function is shown by the following code line.

   fileName = Format(Now, "MMddyy") & Format(CStr(seqNum), "000") & ".ERR"

This works fine in VB6, but in VB.NET, the compatability Format function will not Format when a String type is passed to it.  The obvious fix is to remove the CStr function from the mix as shown below.

   fileName = Format(Now, "MMddyy") & Format(CStr(seqNum), "000") & ".ERR"

I previously documented a problem with the Replace function in in VB.Net.  Click here to read about that problem.  Obviously, you should convert to Replace method of the String object, but you must be aware that it is case-sensitive.

Ask a Question, or give your feedback on my articles or products by going to the KnowDotNet Forum or by clicking on My Blog.
  



Writing Add-Ins for Visual Studio .NET
Writing Add-ins for Visual Studio .NET
by Les Smith
Apress Publishing