VB to .NET

Detailed Code Improvements

As part of the VB6 AI Migrator’s advanced functionalities, the tool makes the converted code more .NET-like, while also improving aesthetic aspects.

Some of the enhancements are related to code simplifications. The following examples show some of these cases:

Source pattern Target .NET pattern
if <cond> then return true else return false return <cond>
if <cond> then <var> = true else <var> = false <var> = <cond>
if <cond> then <var> = false else <var> = true <var> = !<cond>
<booleanexpression> compared to true or false [!] <booleanexpression>
Operations simplifications like:
(<exp> + 1) > 0
<exp> >= 0
!(!<expression>) <expression>
Arithmetic simplifications Several simplifications like:
(<expression> + 1) - 1 => <expression>
Err.Raise throw new Exception()
 

 Other enhancements are related to library members that were previously converted to the .NET Visual Basic 6 Compatibility Library. The VB6 AI Migrator converts them to .NET Framework native libraries, as shown in the following table:

Source pattern Target .NET pattern
<timer_reference>.Interval compared to 0 patterns. Patterns of <timer_reference>.Enabled
Dir(<path>, vbDirectory) compared to empty string. [!] System.IO.Directory.Exists(<filename>)
vb.App.LogEvent System.Diagnostics.EventLog.WriteEntry
VBA.FileSystem.Kill System.IO.File.Delete
vba.FileSystem.FileCopy System.IO.File.Copy
VBA.Strings.Format
VBA.Strings.FormatNumber
VBA.Strings.FormatPercent
Different patterns of p.ToString(...)
VBA.DateTime.Date Uses of System.DateTime.Now
VBA.DateTime.DateValue Uses of System.DateTime.Parse
VBA.DateTime.DateAdd
(<interval>,<num>,<date>)
<date>.AddYears(<num>)
<date>.AddMonths(<num>)
<date>.AddDays(<num>)
<date>.AddHours(<num>)
<date>.AddMinutes(<num>)
<date>.AddSeconds(<num>)
VBA.Information.IsEmpty(p)
where “p” can be a:
- numeric
- boolean
- date
- string
- object
numeric -> p.Equals(0)
boolean -> p.Equals(false)
date -> p.Equals(DateTime.FromOADate(0))
string -> p.IsNullOrEmpty()
object -> p.Equals(param,Nothing)
VBA.Strings.Split(...) Various patterns of <string-object>.Split(…)
VBA.VbMsgBoxResult Patterns of System.Windows.Forms.DialogResult
VBA.VbDayOfWeek System.FirstDayOfWeek
VBA.Strings.Left
VBA.Strings.Right
VBA.Strings.Mid
Uses of <str-object>.Substring(…)
VBA.Constants.<char-const> Strings.Chr(<value>) / chr(13)
File IO operations Converted using System.IO namespace


If-ElseIf Block to Switch Block

The VB6 AI Migrator detects If-ElseIf blocks with all its conditions, comparing the same expression against different values and converting them to a more natural Switch block.

Replacement of “for” Loops by “foreach” Loops

When VB6 For loops iterate over collections and some arrays, VB6 AI Migrator upgrades the statement to .NET ForEach statements, applying the necessary transformations to the collection/array references inside the For body and the counter variables.

Counter variables that are no longer needed after the Foreach transformation are removed from the resulting code block.

Unify Code Blocks

Some classes in VB6 have two events that correspond to one unique event in the corresponding .NET class. In those cases, the bodies of the two events are merged to achieve the same behavior.

Some examples of these transformations are:

  • MSComctlLib.StatusBar PanelClick and PanelDblClick events
  • MSComctlLib.ListView ItemClick and Click events

New Objects Declaration

This rule allows adding the variable initialization value to its declaration:

[VB6]

Dim _myvar as MyType
... <Code which doesn’t reference _myvar>
_myvar = InitValue 

[C#]

MyType _myvar = InitValue;
... <Code which doesn’t reference _myvar>

Automatic “import” Clause Generation

Besides the option of generating references to the .NET library elements by using their fully qualified names, the VB6 AI Migrator introduces an additional transformation stage that recognizes fully qualified names and simplifies them while automatically generating the necessary import / using causes.

This new feature makes the converted code look much better providing an improved readability and maintainability.

Talk To An Engineer