In the following section, will find the generated code (C# and VB.NET), important messages after upgrading your code (EWIs), and helpers information.
What does the VBUC generate? - C# and VB.NET Differences
There are some features that help to simulate VB6 behavior, making C# easier to use, but in some cases, VB.NET could be better. In this section, you will see a comparison of some features in C# and VB.NET.
Error Handling
It refers to the response and recovery procedures from error conditions present in a software application. The VBUC offers three options for error handling:
Most patterns will be upgraded to Try-Catch blocks. This option covers a huge amount of "On Error GoTo" patterns and very basic cases of "On Error Resume Next".
This feature covers the most common patterns of "On Error GoTo" and will generate special patterns to support most "On Error Resume Next" patterns.
Original VB6 Code:
Public Sub OnErrorGotoLabelMethod(arg AsString)On Error GoToerrHnd Dim s AsInteger s =CInt(arg) Foo s Exit SuberrHnd: MsgBox "Invalid Argument"End SubPublic Sub OnErrorResumeNextMethod(arg AsString)On Error Resume Next MsgBox arg MsgBox CInt(arg) MsgBox "This line will be executed allways" If Err.Number <>0 Then'This code should be executed if there were any error(s) MsgBox "OnErrorResumeNextMethod reached the last statement" MsgBox "OnErrorResumeNextMethod reached the last statement" MsgBox "OnErrorResumeNextMethod reached the last statement" End IfEnd Sub
Leave On Error Statements (VB.NET Only)
Public Sub OnErrorGotoLabelMethod(ByVal arg AsString)On Error GoToerrHnd Dim s AsInteger s =CInt(arg)Foo(s) Exit SuberrHnd: MessageBox.Show("Invalid Argument", My.Application.Info.Title)End SubPublic Sub OnErrorResumeNextMethod(ByVal arg AsString)On Error Resume Next MessageBox.Show(arg, My.Application.Info.Title) MessageBox.Show(CStr(CInt(arg)), My.Application.Info.Title) MessageBox.Show("This line will be executed allways", My.Application.Info.Title) If Information.Err().Number <>0 Then'This code should be executed if there were any error(s) MessageBox.Show("OnErrorResumeNextMethod reached the last statement", My.Application.Info.Title) MessageBox.Show("OnErrorResumeNextMethod reached the last statement", My.Application.Info.Title) MessageBox.Show("OnErrorResumeNextMethod reached the last statement", My.Application.Info.Title) End IfEnd Sub
Convert To Try-Catch
C# Code:
publicvoidOnErrorGotoLabelMethod(string arg){try {int s =0; s =Convert.ToInt32(Double.Parse(arg));Foo(s); }catch {MessageBox.Show("Invalid Argument",AssemblyHelper.GetTitle(System.Reflection.Assembly.GetExecutingAssembly())); }}publicvoidOnErrorResumeNextMethod(string arg){ //UPGRADE_TODO: (1069) Error handling statement (On Error Resume Next) was converted to a pattern that might have a different behavior.try {MessageBox.Show(arg,AssemblyHelper.GetTitle(System.Reflection.Assembly.GetExecutingAssembly()));MessageBox.Show(Convert.ToInt32(Double.Parse(arg)).ToString(),AssemblyHelper.GetTitle(System.Reflection.Assembly.GetExecutingAssembly()));MessageBox.Show("This line will be executed allways",AssemblyHelper.GetTitle(System.Reflection.Assembly.GetExecutingAssembly())); //UPGRADE_WARNING: (2081) Err.Number has a new behavior.if (Information.Err().Number!=0) { //This code should be executed if there were any error(s)MessageBox.Show("OnErrorResumeNextMethod reached the last statement",AssemblyHelper.GetTitle(System.Reflection.Assembly.GetExecutingAssembly()));MessageBox.Show("OnErrorResumeNextMethod reached the last statement",AssemblyHelper.GetTitle(System.Reflection.Assembly.GetExecutingAssembly()));MessageBox.Show("OnErrorResumeNextMethod reached the last statement",AssemblyHelper.GetTitle(System.Reflection.Assembly.GetExecutingAssembly())); } }catch (Exception exc) {NotUpgradedHelper.NotifyNotUpgradedElement("Resume in On-Error-Resume-Next Block"); }}
VB.NET Code:
Public Sub OnErrorGotoLabelMethod(ByVal arg AsString) Try Dim s AsInteger s =CInt(arg)Foo(s) Catch MessageBox.Show("Invalid Argument", My.Application.Info.Title) End TryEnd SubPublic Sub OnErrorResumeNextMethod(ByVal arg AsString)'UPGRADE_TODO: (1069) Error handling statement (On Error Resume Next) was converted to a pattern that might have a different behavior.' Try MessageBox.Show(arg, My.Application.Info.Title) MessageBox.Show(CStr(CInt(arg)), My.Application.Info.Title) MessageBox.Show("This line will be executed allways", My.Application.Info.Title) If Information.Err().Number <>0 Then'This code should be executed if there were any error(s)' MessageBox.Show("OnErrorResumeNextMethod reached the last statement", My.Application.Info.Title) MessageBox.Show("OnErrorResumeNextMethod reached the last statement", My.Application.Info.Title) MessageBox.Show("OnErrorResumeNextMethod reached the last statement", My.Application.Info.Title) End If Catch exc As Exception NotUpgradedHelper.NotifyNotUpgradedElement("Resume in On-Error-Resume-Next Block") End TryEnd Sub
To Try-Catch With Lambdas (C# Only)
publicvoidOnErrorGotoLabelMethod(string arg){try {int s =0; s =Convert.ToInt32(Double.Parse(arg));Foo(s); }catch {MessageBox.Show("Invalid Argument",AssemblyHelper.GetTitle(System.Reflection.Assembly.GetExecutingAssembly())); }}publicvoidOnErrorResumeNextMethod(string arg){Exception ex =null;ErrorHandlingHelper.ResumeNext(out ex, () => {MessageBox.Show(arg,AssemblyHelper.GetTitle(System.Reflection.Assembly.GetExecutingAssembly()));}, () => {MessageBox.Show(Convert.ToInt32(Double.Parse(arg)).ToString(),AssemblyHelper.GetTitle(System.Reflection.Assembly.GetExecutingAssembly()));}, () => {MessageBox.Show("This line will be executed allways",AssemblyHelper.GetTitle(System.Reflection.Assembly.GetExecutingAssembly()));});if (ex !=null) {ErrorHandlingHelper.ResumeNext( //This code should be executed if there were any error(s) () => {MessageBox.Show("OnErrorResumeNextMethod reached the last statement",AssemblyHelper.GetTitle(System.Reflection.Assembly.GetExecutingAssembly()));}, () => {MessageBox.Show("OnErrorResumeNextMethod reached the last statement",AssemblyHelper.GetTitle(System.Reflection.Assembly.GetExecutingAssembly()));}, () => {MessageBox.Show("OnErrorResumeNextMethod reached the last statement",AssemblyHelper.GetTitle(System.Reflection.Assembly.GetExecutingAssembly()));}); }}
Late Binding
This is a mechanism where the type is unknown until the variable is used during runtime; usually through the assignment. The VBUC offers three options for late binding:
This option will cause the late binding access cases that could not be resolved with a static code analysis to be managed at runtime by means of a helper class.
VBUC will use its static code analysis process to determine the correct type for each object and resolve the late binding access.
Note: The Visual Basic Upgrade Companion solves most of these issues by using a static code analysis process to infer and determine the correct data type for each variable. However, there are some variables that take several values with inconsistent types and there is no way to infer a proper type. For these cases, manual changes are required to reach functional equivalence.
This option will cause the late binding access cases that could not be resolved with a static code analysis to be managed at runtime with dynamic variables.
Original VB6 Code:
Public Sub Func(myControl As Control, myForm As Form, myVar As Variant) myControl.controlProp1 = myVar.varProp1 myForm.formProp1 = myControl.controlProp1 myVar.varProp1 = myForm.formProp1 myControl.Func 1 myForm.Func 1 myVar.Func 1End Sub
Public Sub Func(ByVal myControl As Control, ByVal myForm As Form, ByVal myVar AsObject) ReflectionHelper.LetMember(myControl,"controlProp1", ReflectionHelper.GetMember(myVar,"varProp1")) ReflectionHelper.LetMember(myForm,"formProp1", ReflectionHelper.GetMember(myControl,"controlProp1")) ReflectionHelper.LetMember(myVar,"varProp1", ReflectionHelper.GetMember(myForm,"formProp1")) ReflectionHelper.Invoke(myControl,"Func",NewObject() {1}) ReflectionHelper.Invoke(myForm,"Func",NewObject() {1}) ReflectionHelper.Invoke(myVar,"Func",NewObject() {1})End Sub
Static code analysis only
C# Code:
publicvoidFunc(Control myControl,Form myForm,object myVar){ //UPGRADE_TODO: (1067) Member controlProp1 is not defined in type VB.Control. //UPGRADE_TODO: (1067) Member varProp1 is not defined in type Variant.myControl.controlProp1=myVar.varProp1; //UPGRADE_TODO: (1067) Member formProp1 is not defined in type VB.Form. //UPGRADE_TODO: (1067) Member controlProp1 is not defined in type VB.Control.myForm.formProp1=myControl.controlProp1; //UPGRADE_TODO: (1067) Member varProp1 is not defined in type Variant. //UPGRADE_TODO: (1067) Member formProp1 is not defined in type VB.Form.myVar.varProp1=myForm.formProp1; //UPGRADE_TODO: (1067) Member Func is not defined in type VB.Control.myControl.Func(1); //UPGRADE_TODO: (1067) Member Func is not defined in type VB.Form.myForm.Func(1); //UPGRADE_TODO: (1067) Member Func is not defined in type Variant.myVar.Func(1);}
VB.NET Code:
Public Sub Func(ByVal myControl As Control, ByVal myForm As Form, ByVal myVar AsObject)'UPGRADE_TODO: (1067) Member controlProp1 is not defined in type VB.Control.''UPGRADE_TODO: (1067) Member varProp1 is not defined in type Variant.' myControl.controlProp1 = myVar.varProp1'UPGRADE_TODO: (1067) Member formProp1 is not defined in type VB.Form.''UPGRADE_TODO: (1067) Member controlProp1 is not defined in type VB.Control.' myForm.formProp1 = myControl.controlProp1'UPGRADE_TODO: (1067) Member varProp1 is not defined in type Variant.''UPGRADE_TODO: (1067) Member formProp1 is not defined in type VB.Form.' myVar.varProp1 = myForm.formProp1'UPGRADE_TODO: (1067) Member Func is not defined in type VB.Control.' myControl.Func(1)'UPGRADE_TODO: (1067) Member Func is not defined in type VB.Form.' myForm.Func(1)'UPGRADE_TODO: (1067) Member Func is not defined in type Variant.' myVar.Func(1)End Sub
Option Strict OffPublic Sub Func(ByVal myControl As Control, ByVal myForm As Form, ByVal myVar AsObject)CType(myControl,object).controlProp1 = myVar.varProp1CType(myForm,object).formProp1 =CType(myControl,object).controlProp1 myVar.varProp1 =CType(myForm,object).formProp1CType(myControl,object).Func(1)CType(myForm,object).Func(1) myVar.Func(1)End Sub
Go Sub
VB6 provides the ability to jump around in the code from one portion to another thru "labels" and create code that is not structured. By default Go Sub statements are not supported in .NET, but by turning on this option, the VBUC will apply the special pattern recognition mechanism to support the GoSub Statement.
This feature works only for C#. The user can enable or disable it.
Original VB6 Code:
Public Sub Main()Dim i AsIntegeri =4If i =1 Then GoSub printOneElseIf i =2 Then GoSub printTwoElseIf i =3 Then GoSub printThreeElse GoSub SomethingElseEnd IfGoToexitLabelprintOne:MsgBox "Value = 1"ReturnprintTwo:MsgBox "Value = 2"ReturnprintThree:MsgBox "Value = 3"ReturnSomethingElse:MsgBox "Invalid Value!"ReturnexitLabel:End Sub
C# Code:
publicstaticvoidMain(){int i =4;if (i ==1) {printOne(); }elseif (i ==2) { printTwo(); }elseif (i ==3) { printThree(); }else {SomethingElse(); }return;voidprintOne() {MessageBox.Show("Value = 1",AssemblyHelper.GetTitle(System.Reflection.Assembly.GetExecutingAssembly())); }voidprintTwo() {MessageBox.Show("Value = 2",AssemblyHelper.GetTitle(System.Reflection.Assembly.GetExecutingAssembly())); }voidprintThree() {MessageBox.Show("Value = 3",AssemblyHelper.GetTitle(System.Reflection.Assembly.GetExecutingAssembly())); }voidSomethingElse() {MessageBox.Show("Invalid Value!",AssemblyHelper.GetTitle(System.Reflection.Assembly.GetExecutingAssembly())); }}
VB.NET Code:
Public Sub Main() Dim i AsInteger=4 If i =1 Then'UPGRADE_ISSUE: (1014) GoSub statement is not supported. ' GoSub printOne ElseIf i =2 Then 'UPGRADE_ISSUE: (1014) GoSub statement is not supported. ' GoSub printTwo ElseIf i =3 Then 'UPGRADE_ISSUE: (1014) GoSub statement is not supported. ' GoSub printThree Else'UPGRADE_ISSUE: (1014) GoSub statement is not supported. ' GoSub SomethingElse End If Exit SubprintOne: MessageBox.Show("Value = 1", My.Application.Info.Title)'UPGRADE_WARNING: (2081) Return has a new behavior. ' Return printTwo: MessageBox.Show("Value = 2", My.Application.Info.Title)'UPGRADE_WARNING: (2081) Return has a new behavior. ' Return printThree: MessageBox.Show("Value = 3", My.Application.Info.Title)'UPGRADE_WARNING: (2081) Return has a new behavior. ' Return SomethingElse: MessageBox.Show("Invalid Value!", My.Application.Info.Title)'UPGRADE_WARNING: (2081) Return has a new behavior. ' Return End Sub
Getting information from the AssemblyInfo file
This feature refers to how the VBUC tool obtains the information contained in the AssemblyInfo file.
Original VB6 Code:
Private Sub Form_Load() MsgBox "Hello World!" EndEnd Sub
Private IntVar AsIntegerPrivate BoolVar AsBooleanPrivate StrVar AsString=""Public Property IntVarProp()AsInteger Get'some comments for IntVarProp property header' Return IntVar'some comments for IntVarProp property footer' End GetSet(ByVal Value AsInteger)'some comments for IntVarProp property header' IntVar = Value'some comments for IntVarProp property footer' End SetEnd PropertyPublic Property BoolVarProp()AsBoolean Get Return BoolVar End GetSet(ByVal Value AsBoolean) BoolVar = Value End SetEnd PropertyPublic Property StrVarProp()AsString Get Return StrVar End GetSet(ByVal Value AsString) StrVar = Value End SetEnd PropertyPublic Sub FieldsUsage() IntVar =0 BoolVar =False StrVar ="Hello World"parameterFieldsUsage(IntVar, BoolVar, StrVar)End SubPublic Sub PropertiesUsage() IntVarProp =0 BoolVarProp =False StrVarProp ="Hello World"parameterProperiesUsage(IntVarProp, BoolVarProp, StrVarProp)End SubPublic Sub parameterFieldsUsage(ByVal IntVar AsInteger, ByVal BoolVar AsBoolean, ByVal StrVar AsString)''End SubPublic Sub parameterProperiesUsage(ByVal IntVar AsInteger, ByVal BoolVar AsBoolean, ByVal StrVar AsString)''End Sub
Case Sensitive
.NET Common Language Runtime is case-sensitive. VB.NET code relies on the runtime, which means it must be case-sensitive at runtime, like when it is looking up variables and methods. However, the VB.NET compiler and editor let you ignore that because they correct the case in your code, whereas C# does not, making VB.NET a case-insensitive language (there are exceptions, such as XML literals, and string comparisons).
EWIs
When upgrading a VB6 project to .NET, the Visual Basic Upgrade Companion upgrades most of the code and objects from VB6 to their equivalents in either C# or VB.NET. However, there might be some items that are not fully upgraded, requiring manual changes in order to compile, run and achieve functional equivalence. The Visual Basic Upgrade Companion helps with this process by inserting different Errors, Warnings, and Issues (EWI) information messages into the upgraded code as comments.
EWIs are a valuable part of VB6 to .NET upgrade efforts. They alert you to potential issues, and the upgraded code provides information about how to fix them. An EWI is placed on the line before the problem and it consists of three parts:
Example:
//UPGRADE_WARNING: (2080) Form_Load event was upgraded to Form_Load method and has a new behavior. More Information: https://docs.mobilize.net/vbuc/ewis#2080
In the example above, an EWI is made up of:
Code Number.
(2080)
A message describing the problem.
Form_Load event was upgraded to Form_Load method and has a new behavior.
A link to the help topic.
More Information: https://docs.mobilize.net/vbuc/ewis#2080
EWIs are inserted into the code as comments, they do not affect the compilation or execution of the upgraded application.
Type
Description
Comment
Code that will cause a compilation error
Marks items that you need to fix before the upgraded code can run
Code partially upgraded, it requires some additional changes
Requires some manual intervention to avoid compilation and runtime errors
Behavior difference between VB6 and .NET. The code will compile and run, but it might have a difference at runtime
Most of the time code will work fine, but it should be reviewed as a precaution
Code was changed in a way that may confuse the developer
Normally will not require user intervention
Global Warning
EWI present on the Upgrade Report only, not to the source code
Upgrade Helpers
The VBUC is able to translate a large part of the VB6 code directly to simple C# statements, but some functionality is either too specific or too complex to allow a one-to-one conversion. In these cases, the VBUC uses helper classes that encapsulate complex commands into a single function call. These functions, if desired, are included in the generated code.
Upgrade Helper classes range from simple string manipulation functions to extensions of complex controls such as grids. The use of these classes allows the VBUC-generated code to remain legible while maintaining functional equivalence with VB6.
Helpers integration
Helper Classes can be integrated into the migrated solution in three ways. This option can be selected in the Helpers Integration combo box in the Upgrade Options tab of the VBUC.
Option
Description
Source Code
The source code for the Helper Classes will be added directly to the solution in the UpgradeSupport folder.
Binary
Compiled .dll files for the Helper Classes will be referenced in the solution and will be placed in the UpgradeSupport folder.
NuGet
References to the Helper Classes will be made through NuGet.
This option is available for certain .NET Platforms only:
.NET Framework 4.7.2
.NET Framework 4.8
.NET Core 3.1
.NET 5
.NET 6
Reflection Helper
The VB6 typing model allows late binding. This means variables may not have been declared with their actual type, but with a general type. E.g.: Control instead of Label, Form instead of Form1, or Variant instead of any other type.
Even when the variables are declared with a general type, the code can assume they contain an instance of a more specific type and access members from the latter. Neither the VB6 compiler nor the interpreter will raise an error if the actual type of the instance has the requested member.
On the other hand, .NET will give a compilation error whenever a member is not in the declared type of the variable.
VBUC solves most of these issues by using its typing mechanism. This module is able to identify and resolve most late binding cases and correct the declarations with their actual type before performing the conversion process. This mechanism allows solving the member access as well as many other typing-related upgrade issues.
However, some variables take several values with inconsistent types and there is no way to infer a proper type for them.
By selecting the Static code analysis + helper classes option in the Late Binding Resolution upgrade option, the VBUC generates ReflectionHelper calls for these last cases which were not solved by the typing mechanism.
The ReflectionHelper calls avoid .NET compilation errors and provide an equivalent behavior in many cases. However, it must be noted that not all the cases will behave in the same way since the upgrade process still lacks information about the actual type of the variable and its member during the application runtime. This is a simple example of the reflection helper usage:
Original VB6 Code:
Private Sub ShowInfo(useForm AsBoolean) Dim x As Variant If useForm Then Set x = Form1 Else Set x =NewClass1 End If If useForm Then MsgBox x.formProp Else MsgBox x.classProp End IfEnd Sub
Resulting C#.NET Code:
privatevoidShowInfo( bool useForm){object x =null;if (useForm) { x =Form1.DefInstance; }else { x =newClass1(); }if (useForm) { //UPGRADE_TODO: (1067) Member formProp is not defined in type Variant.MessageBox.Show(Convert.ToString(ReflectionHelper.GetMember(x,"formProp")),Application.ProductName); } else { //UPGRADE_TODO: (1067) Member classProp is not defined in type Variant.MessageBox.Show(Convert.ToString(ReflectionHelper.GetMember(x,"classProp")),Application.ProductName); }}
Printer Helper
The VBUC can handle the migration of the VB.Global.Printer class in two ways. The first is using Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6.Printer class provided by Microsoft. This is handled inside a small helper class that manages the Printer object as a singleton and uses the methods provided within.
The second option is a helper class that handles printing using the .NET System.Drawing.Printing namespace. The code for this class is included when the Helpers Integration option is set to Source Code.
Original VB6 code:
Private Sub Command1_Click() Printer.FontName ="Verdana" Printer.FontSize =16 Printer.FontBold =True Printer.FontItalic =True Printer.FontUnderline =True Printer.Print"This is an example" Printer.EndDocEnd SubPrivate Sub Form_Load() Dim prnPrinter As Printer For Each prnPrinter In Printers If prnPrinter.DeviceName ="Device1" Then Set Printer = prnPrinter Exit For End If NextEnd Sub
C# code with PowerPacks:
privatevoidCommand1_Click(Object eventSender,EventArgs eventArgs){PowerPacksPrinterHelper.Instance.FontName="Arial";PowerPacksPrinterHelper.Instance.FontSize=14;PowerPacksPrinterHelper.Instance.FontBold=true;PowerPacksPrinterHelper.Instance.FontItalic=true;PowerPacksPrinterHelper.Instance.FontUnderline=true;PowerPacksPrinterHelper.Instance.Print("This is an example");PowerPacksPrinterHelper.Instance.EndDoc();}privatevoidForm_Load(){PrinterHelper prnPrinter =null;foreach (PrinterHelper prnPrinterIterator inPrinterHelper.Printers) { prnPrinter = prnPrinterIterator;if (PowerPacksPrinterHelper.Instance.DeviceName=="Device1") {PrinterHelper.Printer= prnPrinter;break; } }}
C# code with native helper class:
privatevoidCommand1_Click(Object eventSender,EventArgs eventArgs){PrinterHelper.Printer.FontName="Arial";PrinterHelper.Printer.FontSize=14;PrinterHelper.Printer.FontBold=true;PrinterHelper.Printer.FontItalic=true;PrinterHelper.Printer.FontUnderline=true;PrinterHelper.Printer.Print("This is an example");PrinterHelper.Printer.EndDoc();}privatevoidForm_Load(){PrinterHelper prnPrinter =null;foreach (PrinterHelper prnPrinterIterator inPrinterHelper.Printers) { prnPrinter = prnPrinterIterator;if (PrinterHelper.Printer.DeviceName=="Device1") {PrinterHelper.Printer= prnPrinter;break; } }}