Code Conversion

This section provides some transformation that we apply so that some elements will be upgraded as you need it.

1. ActiveX

1.1. .NET Assemblies with COM Visible

This option will generate special attributes for the converted classes and their members in order to keep the resulting assemblies exposed through a COM interface.

This feature enables the resulting .NET components to be called from other non-managed components or applications via COM.

General Description:

The VBUC converts Visual Basic 6 ActiveX binaries to .NET assemblies. By default, the resulting assemblies can be used only from managed code. This option should be selected to generate the .NET attributes required to expose these assemblies as COM Objects if it’s necessary to use them from non-managed code.

In addition to the “COMVisible” attribute on the class members, the resulting project is also marked as COM Visible.

Original VB6 code:

Code declared in an Activex EXE:

Module1.bas
Public myVar As String
Customer.cls
Attribute VB_Name = "Customer"

Private g_TheNumber As Integer

Public Property Let TheNumber(ByVal new_value As Integer)
   g_TheNumber = new_value
End Property

Public Property Get TheNumber() As Integer
   TheNumber = g_TheNumber
End Property

Code located in the main application that uses the Activex EXE:

Private m_Customer1 As ExeBillingObjects.Customer
Private m_Customer2 As ExeBillingObjects.Customer

Private Sub Form_Load()
   Set m_Customer1 = New ExeBillingObjects.Customer
   Set m_Customer2 = New ExeBillingObjects.Customer
End Sub

Private Sub cmdGet_Click()
   txtTheNumber.Text = m_Customer1.TheNumber
End Sub

C# code:

Code declared in an Activex EXE:

Module1.cs
internal static class Module1
{
	public static string myVar = "";
}
Customer.cs
[ComVisible(true)][ProgId("ExeBillingObjects.Customer")][ClassInterface(ClassInterfaceType.AutoDual)]
public class Customer
{
	public int TheNumber
	{
		get
		{
			return Module1.g_TheNumber;
		}
		set
		{
			Module1.g_TheNumber = value;
		}
	}	
}

Code located in the main application that uses the Activex EXE:

private ExeBillingObjects.Customer m_Customer1 = null;
private ExeBillingObjects.Customer m_Customer2 = null;
			
private void  cmdSet_Click( Object eventSender,  EventArgs eventArgs)
{
	m_Customer1.TheNumber = Convert.ToInt32(Double.Parse(txtTheNumber.Text));
	txtTheNumber.Text = "";
}

private void  Form1_Load( Object eventSender,  EventArgs eventArgs)
{
	m_Customer1 = new ExeBillingObjects.Customer();
	m_Customer2 = new ExeBillingObjects.Customer();
}

VB.NET code:

Code declared in an Activex EXE:

Module1.vb
Module Module1
	Public myVar As String = ""
End Module
Customer.vb
<ComVisible(True)> _
<ProgId("ExeBillingObjects.Customer")> _
<ClassInterface(ClassInterfaceType.AutoDual)> _
Public Class Customer
	Public Property TheNumber() As Integer
		Get
			Return g_TheNumber
		End Get
		Set(ByVal Value As Integer)
			g_TheNumber = Value
		End Set
	End Property
End Class

Code located in the main application that uses the Activex EXE:

Private m_Customer1 As ExeBillingObjects.Customer
Private m_Customer2 As ExeBillingObjects.Customer

Private Sub cmdGet_Click(ByVal eventSender As Object, ByVal eventArgs As EventArgs) Handles cmdGet.Click
	txtTheNumber.Text = CStr(m_Customer1.TheNumber)
End Sub

Private Sub Form1_Load(ByVal eventSender As Object, ByVal eventArgs As EventArgs) Handles MyBase.Load
	m_Customer1 = New ExeBillingObjects.Customer()
	m_Customer2 = New ExeBillingObjects.Customer()
End Sub

1.2. Out process and Application

This optional feature enables the conversion of ActiveX DLLs and EXEs (with global variables in a .bas module) via a helper class (ComponentClassHelper) that controls the instantiation of the generated classes.

  • ActiveX EXEs are converted to out-of-process .NET EXEs.

  • ActiveX DLLs are converted to .NET class libraries using application domains.

  • Component instantiation is performed using helper classes. With this approach most of the ActiveX functionality is replicated in the converted code.

General Description:

The VBUC converts Visual Basic 6 ActiveX EXEs to .NET EXE projects (out-of-process) and ActiveX DLLs to .NET assemblies (class library projects).

One of the main differences between Visual Basic 6 ActiveX binaries and .NET assemblies is how the components are instantiated and how their initialization determines its lifetime behavior.

When this feature is active, a helper class is used to replicate the following ActiveX instantiation types:

  • Private

  • PublicNotCreatable

  • SingleUse

  • GlobalSingleUse

  • MultiUse

  • GlobalMultiUse

Original VB6 code:

Code declared in an Activex EXE:

Module1.bas
Public myVar As String
Customer.cls
Attribute VB_Name = "Customer"

Private g_TheNumber As Integer

Public Property Let TheNumber(ByVal new_value As Integer)
   g_TheNumber = new_value
End Property

Public Property Get TheNumber() As Integer
   TheNumber = g_TheNumber
End Property

Code located in the main application that uses the Activex EXE:

Private m_Customer1 As ExeBillingObjects.Customer
Private m_Customer2 As ExeBillingObjects.Customer

Private Sub Form_Load()
   Set m_Customer1 = New ExeBillingObjects.Customer
   Set m_Customer2 = New ExeBillingObjects.Customer
End Sub

Private Sub cmdGet_Click()
   txtTheNumber.Text = m_Customer1.TheNumber
End Sub

C# code:

Code declared in an Activex EXE:

Module1.cs
internal static class Module1
{
	public static string myVar = "";
	internal static void InitGlobalVars()
	{
		myVar = "";
	}
}
Customer.cs
public class Customer : UpgradeHelpers.Activex.ComponentClassHelper
{
	public int TheNumber
	{
		get
		{
			return g_TheNumber;
		}
		set
		{
			g_TheNumber = value;
		}
	}	
}

Code located in the main application that uses the Activex EXE:

private ExeBillingObjects.Customer m_Customer1 = null;
private ExeBillingObjects.Customer m_Customer2 = null;
			
private void cmdGet_Click( Object eventSender,  EventArgs eventArgs)
{
	txtTheNumber.Text = Convert.ToString(m_Customer1.TheNumber);
}

private void Form1_Load( Object eventSender,  EventArgs eventArgs)
{
	m_Customer1 = ExeBillingObjects.ExeBillingObjectsFactory.Create <ExeBillingObjects.Customer>(m_Customer1);
	m_Customer2 = ExeBillingObjects.ExeBillingObjectsFactory.Create <ExeBillingObjects.Customer>(m_Customer2);

VB.NET code:

Code declared in an Activex EXE:

Module1.vb
Module Module1
	Public myVar As String = ""
	Public Sub InitGlobalVars()
		myVar = ""
	End Sub
End Module
Customer.vb
Public Class Customer Inherits UpgradeHelpers.Activex.ComponentClassHelper
	Public Property TheNumber() As Integer
		Get
			Return g_TheNumber
		End Get
		Set(ByVal Value As Integer)
			g_TheNumber = Value
		End Set
	End Property
End Class

Code located in the main application that uses the Activex EXE:

Private m_Customer1 As ExeBillingObjects.Customer
Private m_Customer2 As ExeBillingObjects.Customer
	
Private Sub cmdGet_Click(ByVal eventSender As Object, ByVal eventArgs As EventArgs) Handles cmdGet.Click
	txtTheNumber.Text = CStr(m_Customer1.TheNumber)
End Sub
	
Private Sub Form1_Load(ByVal eventSender As Object, ByVal eventArgs As EventArgs) Handles MyBase.Load
	m_Customer1 = ExeBillingObjects.ExeBillingObjectsFactory.Create(Of ExeBillingObjects.Customer)(m_Customer1)
	m_Customer2 = ExeBillingObjects.ExeBillingObjectsFactory.Create(Of ExeBillingObjects.Customer)(m_Customer2)
End Sub

1.3. Standard .NET Assemblies

This optional feature converts the ActiveX DLLs and EXEs to .NET class libraries (DLLs).

  • All ActiveX binaries are converted to native .NET assemblies (class libraries).

  • With this approach, the converted code can be used only from managed code and the different ActiveX instantiation models (e.g MultiUse, GlobalMultiuse, etc) won’t be supported.

General Description:

The VBUC converts Visual Basic 6 ActiveX EXEs and ActiveX DLLs to .NET class libraries (DLLs).

One of the main differences between Visual Basic 6 ActiveX binaries and .NET assemblies is how the components are instantiated and how their initialization determines its lifetime behavior.

With this option switched on, the code is converted to pure .NET code and the ActiveX instantiation models won’t be supported.

Original VB6 code:

Code declared in an Activex EXE:

Module1.bas
Public myVar As String
Customer.cls
Attribute VB_Name = "Customer"

Private g_TheNumber As Integer

Public Property Let TheNumber(ByVal new_value As Integer)
   g_TheNumber = new_value
End Property

Public Property Get TheNumber() As Integer
   TheNumber = g_TheNumber
End Property

Code located in the main application that uses the Activex EXE:

Private m_Customer1 As ExeBillingObjects.Customer
Private m_Customer2 As ExeBillingObjects.Customer

Private Sub Form_Load()
   Set m_Customer1 = New ExeBillingObjects.Customer
   Set m_Customer2 = New ExeBillingObjects.Customer
End Sub

Private Sub cmdGet_Click()
   txtTheNumber.Text = m_Customer1.TheNumber
End Sub

C# code:

Code declared in an Activex EXE:

Module1.cs
internal static class Module1
{
	public static string myVar = "";
}
Customer.cs
public class Customer
{
	public int TheNumber
	{
		get
		{
			return g_TheNumber;
		}
		set
		{
			g_TheNumber = value;
		}
	}	
}

Code located in the main application that uses the Activex EXE:

private ExeBillingObjects.Customer m_Customer1 = null;
private ExeBillingObjects.Customer m_Customer2 = null;
			
private void  cmdGet_Click( Object eventSender,  EventArgs eventArgs)
{
	txtTheNumber.Text = m_Customer1.TheNumber.ToString();
}
			
private void  Form1_Load( Object eventSender,  EventArgs eventArgs)
{
	m_Customer1 = new ExeBillingObjects.Customer();
	m_Customer2 = new ExeBillingObjects.Customer();
}

VB.NET code:

Code declared in an Activex EXE:

Module1.vb
Module Module1
	Public myVar As String = ""
End Module
Customer.vb
Public Class Customer
	Public Property TheNumber() As Integer
		Get
			Return g_TheNumber
		End Get
		Set(ByVal Value As Integer)
			g_TheNumber = Value
		End Set
	End Property
End Class

Code located in the main application that uses the Activex EXE:

Private m_Customer1 As ExeBillingObjects.Customer
Private m_Customer2 As ExeBillingObjects.Customer

Private Sub cmdGet_Click(ByVal eventSender As Object, ByVal eventArgs As EventArgs) Handles cmdGet.Click
	txtTheNumber.Text = CStr(m_Customer1.TheNumber)
End Sub

Private Sub Form1_Load(ByVal eventSender As Object, ByVal eventArgs As EventArgs) Handles MyBase.Load
	m_Customer1 = New ExeBillingObjects.Customer()
	m_Customer2 = New ExeBillingObjects.Customer()
End Sub

2. Comment Out Dead Code

2.1. On

Private methods that are not referenced will be commented out.

Original VB6 code:

Public Sub PublicMethod()
	Call CalledPrivateMethod
End Sub

Private Sub CalledPrivateMethod()
	MsgBox "This method is called."
End Sub

Private Sub UncalledPrivateMethod()
	MsgBox "This method is not called."
End Sub

C# code:

public void PublicMethod()
{
	CalledPrivateMethod();
}

private void CalledPrivateMethod()
{
	MessageBox.Show("This method is called.", AssemblyHelper.GetTitle(System.Reflection.Assembly.GetExecutingAssembly()));
}

//UPGRADE_NOTE: (7001) The following declaration (UncalledPrivateMethod) seems to be dead code
//private void UncalledPrivateMethod()
//{
	//MessageBox.Show("This method is not called.", AssemblyHelper.GetTitle(System.Reflection.Assembly.GetExecutingAssembly()));
//}

VB.NET code:

Public Sub PublicMethod()
	CalledPrivateMethod()
End Sub

Private Sub CalledPrivateMethod()
	MessageBox.Show("This method is called.", My.Application.Info.Title)
End Sub

'UPGRADE_NOTE: (7001) The following declaration (UncalledPrivateMethod) seems to be dead code'
'Private Sub UncalledPrivateMethod()'
	'MessageBox.Show("This method is not called.", My.Application.Info.Title)'
'End Sub'

2.1. Off

This feature will be turned off and no changes pertaining to this feature will be made.

Original VB6 code:

Public Sub PublicMethod()
	Call CalledPrivateMethod
End Sub

Private Sub CalledPrivateMethod()
	MsgBox "This method is called."
End Sub

Private Sub UncalledPrivateMethod()
	MsgBox "This method is not called."
End Sub

C# code:

public void PublicMethod()
{
	CalledPrivateMethod();
}

private void CalledPrivateMethod()
{
	MessageBox.Show("This method is called.", AssemblyHelper.GetTitle(System.Reflection.Assembly.GetExecutingAssembly()));
}

private void UncalledPrivateMethod()
{
	MessageBox.Show("This method is not called.", AssemblyHelper.GetTitle(System.Reflection.Assembly.GetExecutingAssembly()));
}

VB.NET code:

Public Sub PublicMethod()
	CalledPrivateMethod()
End Sub

Private Sub CalledPrivateMethod()
	MessageBox.Show("This method is called.", My.Application.Info.Title)
End Sub

Private Sub UncalledPrivateMethod()
	MessageBox.Show("This method is not called.", My.Application.Info.Title)
End Sub

3. Default Property Resolution

3.1. Static code analysis and helpers classes

By switching this feature on, the majority of default property cases that could not be resolved with a static code analysis are managed at runtime by the use of a helper class.

  • Manages special cases of default properties at runtime by using a helper class.

  • Increases the automation ratio and reduces the manual effort required to achieve functional equivalence.

General Description:

The Visual Basic Upgrade Companion uses a powerful static code analyzer to determine the correct type for each object. By having the correct type for each object, the VBUC is able to determine its default property and expand this property in the converted code.

However, there are some cases where it’s not possible to determine the type of an object by using this technique. For these scenarios, the VBUC provides a helper class that is able to determine the type of these objects at runtime and get or set their default property.

In the following example, the variable ctl is used to iterate over a different type of controls. Each of these controls has different default properties, thus, during runtime, the default property of ctl will change. If this example is converted using this option, the helper class will determine the type of each use of the ctl variable at runtime and will get the appropriate default property for each instance.

Original VB6 code:

Dim s As String
'Display the default property of controls of a Form'
For Each ctl In Me.Controls
	s = ctl
	MsgBox s
Next ctl
'Display the default property of TextBox'
MsgBox Me.TextBox1

C# code:

string s = String.Empty;
//Display the default property of controls of a Form
foreach (Control ctl in ContainerHelper.Controls(this))
{
	s = ReflectionHelper.GetPrimitiveValue(ctl).ToString(); 
	MessageBox.Show(s, Application.ProductName);
}
//Display the default property of TextBox
MessageBox.Show(this.TextBox1.Text, Application.ProductName);

VB.NET code:

Dim s As String = ""
'Display the default property of controls of a Form'
For Each ctl As Control In ContainerHelper.Controls(Me)
	s = ReflectionHelper.GetPrimitiveValue(ctl).ToString()
	MessageBox.Show(s, Application.ProductName)
Next ctl
'Display the default property of TextBox'
MessageBox.Show(Me.TextBox1.Text, Application.ProductName)

3.2. Static code analysis only

By switching this feature on, the VBUC will use a powerful static code analysis process to determine the correct type for each object and expands its default property.

  • Expands the default properties by using only static code analysis.

  • There are some cases where it’s not possible to determine the type of an object by using static code analysis and it may require manual intervention to achieve functional equivalence.

General Description:

The Visual Basic Upgrade Companion (VBUC) uses a static code analysis process to infer/determine the correct type for each object. By having the correct type of each object, the VBUC can determine the default property of the object and expand this property in the converted code.

However, there some cases where it is not possible to determine the type of an object by using a static code analysis. For these cases manual changes are required to achieve functional equivalence.

Original VB6 code:

Dim s As String
'Display the default property of controls of a Form'
For Each ctl In Me.Controls
	s = ctl
	MsgBox s
Next ctl
'Display the default property of TextBox'
MsgBox Me.TextBox1

C# code:

string s = String.Empty;
//Display the default property of controls of a Form
foreach (Control ctl in ContainerHelper.Controls(this))
{
	//UPGRADE_WARNING: (1068) ctl of type VB.Control is being forced to string
	s = ctl.ToString();
	MessageBox.Show(s, Application.ProductName);
}
//Display the default property of TextBox
MessageBox.Show(this.TextBox1.Text, Application.ProductName);

VB.NET code:

Dim s As String = ""
'Display the default property of controls of a Form'
For Each ctl As Control In ContainerHelper.Controls(Me)
	'UPGRADE_WARNING: (1068) ctl of type VB.Control is being forced to String.'
	s = ctl.ToString()
	MessageBox.Show(s, Application.ProductName)
Next ctl
'Display the default property of TextBox'
MessageBox.Show(Me.TextBox1.Text, Application.ProductName)

4. Error Handling

4.1. Leave on Error Statements (VB.NET Only)

By choosing this option the VBUC will generate .NET source code that employs the “On Error” statements just like the Visual Basic 6 code.

  • This option increases the automation and reduces the manual effort required to get functional equivalence.

  • This feature is optional while upgrading to Visual Basic .NET but is not available for C#.

General Description:

The “On Error Go To” and “On Error Resume Next” error handling statements present in the Visual Basic 6 code will remain the same on the Visual Basic .NET output code. This feature removes the need for manual changes to bring the error handling statements to functional equivalence since it will be used the same in the upgraded code.

Original VB6 code:

Public Sub OnErrorGotoLabelMethod(arg As String)
    On Error GoTo errHnd
        Dim s As Integer
        s = CInt(arg)
        Foo s
        Exit Sub
errHnd:
        MsgBox "Invalid Argument"
End Sub

Public Sub OnErrorResumeNextMethod(arg As String)
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 If
End Sub

VB.NET code:

Public Sub OnErrorGotoLabelMethod(ByVal arg As String)
	On Error GoTo errHnd
	Dim s As Integer
	s = CInt(arg)
	Foo(s)
	Exit Sub
errHnd:
	MessageBox.Show("Invalid Argument", My.Application.Info.Title)
End Sub

Public Sub OnErrorResumeNextMethod(ByVal arg As String)
	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 If
End Sub

4.2. Convert to Try-Catch

The most common occurrences of error handling patterns will be automatically upgraded to Try-Catch blocks using this feature. It covers a vast amount of On Error Goto patterns and very basic cases of On Error Resume Next.

  • This option generates more .NET-like code.

  • This feature is optional while upgrading to Visual Basic .NET but is compulsory for C# code generation.

General Description:

The Visual Basic Upgrade Companion analyzes the execution control of the Visual Basic 6 code detecting the “On Error GoTo” error handling patterns to generate .NET inherent Try-Catch blocks. When "On Error Resume Next" error handling patterns are found, they will be transformed to a basic Try-Catch block, which will have a different behavior. For the most common scenarios this transformation generates error-free pure .NET code out of the box. For more radical cases, the spaghetti-like error handling code found on Visual Basic 6 might require either some changes prior to the upgrade process or manual efforts once it is upgraded to .NET.

Original VB6 code:

Public Sub OnErrorGotoLabelMethod(arg As String)
    On Error GoTo errHnd
        Dim s As Integer
        s = CInt(arg)
        Foo s
        Exit Sub
errHnd:
        MsgBox "Invalid Argument"
End Sub

Public Sub OnErrorResumeNextMethod(arg As String)
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 If
End Sub

C# code:

public void OnErrorGotoLabelMethod(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()));
	}
}

public void OnErrorResumeNextMethod(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 As String)
	Try
		Dim s As Integer
		s = CInt(arg)
		Foo(s)

	Catch
		MessageBox.Show("Invalid Argument", My.Application.Info.Title)
	End Try
End Sub

Public Sub OnErrorResumeNextMethod(ByVal arg As String)
	'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 Try
End Sub

4.3. To Try-Catch with Lambdas (C# Only)

This feature covers the most common patterns of On Error Goto and, additionally, generates special patterns to support most On Error Resume Next patterns. These special patterns are based on the concept of lambda expressions.

  • This option provides a higher automation ratio, reducing the subsequent required manual work.

  • This feature is available for C# code generation only.

  • The feature will only work for the .NET framework 4.0 or higher / VS2010 or higher.

General Description:

This option applies all the transformations present in the more basic “ErrorHandlingToTryCatch” feature and additionally supports most of the On Error Resume Next patterns by introducing a special pattern based on lambda expressions.

The “On Error Resume Next” functionality can be upgraded to .NET by using a combination of lambda expressions and Try catch blocks. This allows the resulting source code to behave like the legacy source code but on pure .NET technology. This feature can be used only for target Visual Studio solutions that support lambda expressions.

Original VB6 code:

Public Sub OnErrorGotoLabelMethod(arg As String)
    On Error GoTo errHnd
        Dim s As Integer
        s = CInt(arg)
        Foo s
        Exit Sub
errHnd:
        MsgBox "Invalid Argument"
End Sub

Public Sub OnErrorResumeNextMethod(arg As String)
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 If
End Sub

C# code:

public void OnErrorGotoLabelMethod(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()));
	}
}

public void OnErrorResumeNextMethod(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()));});
	}
}

5. Form Load

5.1. FormLoad mechanism upgraded using a helper

These optional features apply special transformation rules to the form load and unload methods in order to provide different functionality over the .NET platform.

  • This approach will use a helper class to expose the load and unload functionality.

  • This approach offers a higher automatic conversion ratio.

General Description:

Visual Basic 6 offered the “load” and “unload” methods to handle how the forms are loaded into memory and displayed. This functionality is not available by default in .NET and requires some extra considerations during the automated migration stage.

The VBUC is equipped with special transformation rules to port the Load and Unload methods and related functionality (show, hide and more) into .NET equivalent constructions by several means.

Original VB6 code:

Public RedColor As Boolean

Private Sub Form_Load()
If RedColor Then
	Text1 = "The selected color is red"
Else
	Text1 = "The selected color is blue"
End If
End Sub

C# code:

//UPGRADE_WARNING: (2080) Form_Load event was upgraded to Form_Load event and has a new behavior.
[FormsHelper.LoadAttribute]
private void Form2_Load(Object eventSender, EventArgs eventArgs)
{
	if (!this.IsFormLoaded())
	{
		this.SetFormLoaded(true, visualControls);
		if (RedColor)
		{
			Text1.Text = "The selected color is red";
		}
		else
		{
			Text1.Text = "The selected color is blue";
		}
	}
}

VB.NET code:

'UPGRADE_WARNING: (2080) Form_Load event was upgraded to Form_Load event and has a new behavior.'
<FormsHelper.LoadAttribute> _
Private Sub Form2_Load(ByVal eventSender As Object, ByVal eventArgs As EventArgs) Handles MyBase.Load
	If Not Me.IsFormLoaded() Then
		Me.SetFormLoaded(True, visualControls)
		If RedColor Then
			Text1.Text = "The selected color is red"
		Else
			Text1.Text = "The selected color is blue"
		End If
	End If
End Sub

5.2. FormLoad event upgraded as a method invoked from form constructor

These optional features apply special transformation rules to the form load and unload methods in order to provide different functionality over the .NET platform.

  • Upgrade the Load event to a method invoked from the InitializeComponents method.

  • This approach might require manual adjustments to achieve functional equivalence.

General Description:

Visual Basic 6 offered the “load” and “unload” methods to handle how the forms are loaded into memory and displayed. This functionality is not available by default in .NET and requires some extra considerations during the automated migration stage.

The VBUC is equipped with special transformation rules to port the Load and Unload methods and related functionality (show, hide and more) into .NET equivalent constructions by several means.

Original VB6 code:

Public RedColor As Boolean

Private Sub Form_Load()
If RedColor Then
	Text1 = "The selected color is red"
Else
	Text1 = "The selected color is blue"
End If
End Sub

C# code:

//UPGRADE_WARNING: (2080) Form_Load event was upgraded to Form_Load method and has a new behavior.
private void Form_Load()
{
	if (RedColor)
	{
		Text1.Text = "The selected color is red";
	}
	else
	{
		Text1.Text = "The selected color is blue";
	}
}

VB.NET code:

'UPGRADE_WARNING: (2080) Form_Load event was upgraded to Form_Load method and has a new behavior.'
Private Sub Form_Load()
	If RedColor Then
		Text1.Text = "The selected color is red"
	Else
		Text1.Text = "The selected color is blue"
	End If
End Sub

5.3. FormLoad event upgraded to .NET native event

These optional features apply special transformation rules to the form load and unload methods in order to provide different functionality over the .NET platform.

Upgrade the Form load event to .NET inherent load event

General Description:

Visual Basic 6 offered the “load” and “unload” methods to handle how the forms are loaded into memory and displayed. This functionality is not available by default in .NET and requires some extra considerations during the automated migration stage.

The VBUC is equipped with special transformation rules to port the Load and Unload methods and related functionality (show, hide and more) into .NET equivalent constructions by several means.

Original VB6 code:

Public RedColor As Boolean

Private Sub Form_Load()
If RedColor Then
	Text1 = "The selected color is red"
Else
	Text1 = "The selected color is blue"
End If
End Sub

C# code:

//UPGRADE_WARNING: (2080) Form_Load event was upgraded to Form_Load event and has a new behavior.
private void Form2_Load(Object eventSender, EventArgs eventArgs)
{
	if (RedColor)
	{
		Text1.Text = "The selected color is red";
	}
	else
	{
		Text1.Text = "The selected color is blue";
	}
}

VB.NET code:

'UPGRADE_WARNING: (2080) Form_Load event was upgraded to Form_Load event and has a new behavior.'
Private Sub Form2_Load(ByVal eventSender As Object, ByVal eventArgs As EventArgs) Handles MyBase.Load
	If RedColor Then
		Text1.Text = "The selected color is red"
	Else
		Text1.Text = "The selected color is blue"
	End If
End Sub

6. Free COM Objects

6.1. Free COM Objects memory using helper

This option will cause the resulting code to use a helper class to free the COM objects' allocated memory in the same way as Visual Basic 6.0 does.

  • Generates a call to a helper class in order to release the memory used by COM Objects.

  • This option is not required if all the COM objects are converted to .NET.

General Description:

Visual Basic 6.0 automatically releases the memory used by COM Objects. In .NET, there are some cases that require executing some functions in order to release the memory occupied by COM objects.

If this option is selected the VBUC will generate a call to a helper class in charge of releasing the memory used by COM Objects when a method is finished, a class is being destroyed or a COM reference is changing its value.

Original VB6 code:

Private Sub Command1_Click()
    Dim conConnection As New ADODB.Connection
    Dim cmdCommand As New ADODB.Command
    Dim rstRecordSet As New ADODB.Recordset
    
    conConnection.Open
    cmdCommand.CommandText = "SELECT * FROM TestTable;"
    rstRecordSet.Open
End Sub

C# code:

private void  Command1_Click( Object eventSender,  EventArgs eventArgs)
{
	ADODB.Connection conConnection = null;
	ADODB.Command cmdCommand = null;
	ADODB.Recordset rstRecordSet = null;
	try
	{
		conConnection = new ADODB.Connection();
		cmdCommand = new ADODB.Command();
		rstRecordSet = new ADODB.Recordset();
						
		conConnection.Open("", "", "", -1);
		cmdCommand.CommandText = "SELECT * FROM TestTable;";
		rstRecordSet.Open(Type.Missing, Type.Missing, 
		ADODB.CursorTypeEnum.adOpenUnspecified, ADODB.LockTypeEnum.adLockUnspecified, -1);
	}
	finally
	{
		MemoryHelper.ReleaseAndCleanObject(rstRecordSet);
		rstRecordSet = null;
		MemoryHelper.ReleaseAndCleanObject(cmdCommand);
		cmdCommand = null;
		MemoryHelper.ReleaseAndCleanObject(conConnection);
		conConnection = null;
	}								
}

VB.NET code:

Private Sub Command1_Click(ByVal eventSender As Object, ByVal eventArgs As EventArgs)
	Dim conConnection As New ADODB.Connection
	Dim cmdCommand As New ADODB.Command
	Dim rstRecordSet As New ADODB.Recordset
	Try
		conConnection.Open()
		cmdCommand.CommandText = "SELECT * FROM TestTable;"
		rstRecordSet.Open()
	Finally
		MemoryHelper.ReleaseAndCleanObject(rstRecordSet)
		rstRecordSet = Nothing
		MemoryHelper.ReleaseAndCleanObject(cmdCommand)
		cmdCommand = Nothing
		MemoryHelper.ReleaseAndCleanObject(conConnection)
		conConnection = Nothing
	End Try
End Sub

6.2. Free COM Objects memory manually

By using this option the VBUC will not generate special code to handle the COM Objects memory usage.

The VBUC will not generate any special code to release memory used by COM Objects.

General Description:

Visual Basic 6.0 automatically releases the memory used by COM Objects. In .NET, there are some cases that require executing some functions in order to release the memory occupied by COM objects

If this option is selected, the VBUC will not generate any special code to release the memory used by COM Objects and manual changes might be required to make sure that all the references to the COM Objects are properly released.

Original VB6 code:

Private Sub Command1_Click()
Dim conConnection As New ADODB.Connection
Dim cmdCommand As New ADODB.Command
Dim rstRecordSet As New ADODB.Recordset

conConnection.Open
cmdCommand.CommandText = "SELECT * FROM TestTable;"
rstRecordSet.Open
End Sub

C# code:

private void  Command1_Click( Object eventSender,  EventArgs eventArgs)
{
	ADODB.Connection conConnection = new ADODB.Connection();
	ADODB.Command cmdCommand = new ADODB.Command();
	ADODB.Recordset rstRecordSet = new ADODB.Recordset();
					
	conConnection.Open("", "", "", -1);
	cmdCommand.CommandText = "SELECT * FROM TestTable;";
	rstRecordSet.Open(Type.Missing, Type.Missing, ADODB.CursorTypeEnum.adOpenUnspecified, ADODB.LockTypeEnum.adLockUnspecified, -1);
}

VB.NET code:

Private Sub Command1_Click(ByVal eventSender As Object, ByVal eventArgs As EventArgs) Handles Command1.Click
	Dim conConnection As New ADODB.Connection
	Dim cmdCommand As New ADODB.Command
	Dim rstRecordSet As New ADODB.Recordset
		
	conConnection.Open()
	cmdCommand.CommandText = "SELECT * FROM TestTable;"
	rstRecordSet.Open()
End Sub

7. Convert HelpFile and HelpContexID Feature

7.1 Do not Convert HelpFile and HelpContextID Feature

Choosing this option, the VBUC will not apply any special pattern or behavior. Help File and HelpContextID VB6 features statements are not supported by default in .NET structured code, so VBUC solve this feature with a stub.

Original VB6 code

Private Sub Form_Load()
    App.HelpFile = "clcshelp.chm"
End Sub

C# code

private void Form_Load()
{
	//UPGRADE_ISSUE: (2064) VB method VB.Global was not upgraded. More Information: https://docs.mobilize.net/vbuc/ewis#2064
	//UPGRADE_ISSUE: (2070) Constant App was not upgraded. More Information: https://docs.mobilize.net/vbuc/ewis#2070
	//UPGRADE_ISSUE: (2064) App property App.HelpFile was not upgraded. More Information: https://docs.mobilize.net/vbuc/ewis#2064
	UpgradeStubs.VB.getGlobal().getApp().setHelpFile("clcshelp.chm");
}

VB.Net code

Private Sub Form_Load()
    'UPGRADE_ISSUE: (2064) VB method VB.Global was not upgraded. More Information: https://docs.mobilize.net/vbuc/ewis#2064
    'UPGRADE_ISSUE: (2070) Constant App was not upgraded. More Information: https://docs.mobilize.net/vbuc/ewis#2070
    'UPGRADE_ISSUE: (2064) App property App.HelpFile was not upgraded. More Information: https://docs.mobilize.net/vbuc/ewis#2064
    UpgradeSolution1Support.UpgradeStubs.VB.getGlobal().getApp().setHelpFile("clcshelp.chm")
End Sub

7.2 Convert HelpFile and HelpContextID Feature using Helper Class

Enable this feature provides a helper class to raise an information event for each control with HelpContextID property defined.

  • Generates an event for each control with HelpContextID

  • Verifies if .chm help file exists

General Description

Visual Basic 6 has offered the possibility to use HelpContextID and a .chm file to display information about the current form and controls. That feature does not exist in .Net, so Visual Basic Upgrade Companion provides the opportunity to use a Helper class to have a relative behavior.

The Helper Class transforms the number of HelpContextID, assigning an event for each control with the property defined and checking if the .chm file exists.

Original VB6 Code

Private Sub Form_Load()
    App.HelpFile = "clcshelp.chm"
End Subvb

C# code:

//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
private void Form_Load()
{
	UpgradeSupport.helpSupport.HelpFile = "clcshelp.chm";
}
//In Designer File
protected void InitializeHelp()
{
	UpgradeSupport.helpSupport.SetHelpContextId(this, 523);
	UpgradeSupport.helpSupport.SetHelpContextId(Slider1, 509);
}

VB.NET Code

'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
Private Sub Form_Load()
	UpgradeSupport.helpSupport.HelpFile = "clcshelp.chm"
End Sub
'In Designer File
Protected Sub InitializeHelp()
	UpgradeSupport.helpSupport.SetHelpContextId(Me, 523)
	UpgradeSupport.helpSupport.SetHelpContextId(Slider1, 509)
End Sub

8. Late Binding Resolution

8.1. Static code analysis + helper classes

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.

  • Manage late-bound reference scenarios by using a helper class.

  • This option increases the automation ratio and reduces the manual effort required to get functional equivalence.

General Description:

Visual Basic 6.0 provides the ability to use late-bound variable references. This means the variables may have not been declared with their actual type, but with a generic type. E.g. “control” instead of “label”, “form” instead of “form1” or “variant” instead of any other type. At runtime Visual Basic 6 determines the actual type of the variable and invokes the appropriate methods and/or members. On the other hand, .NET will throw a compilation error whenever a member is not in the declared type of the variable.

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 the VBUC provides a helper class that is able to determine the type of these objects at runtime and invoke or get the appropriate member.

This helper class reduces the amount of compilation errors in the resulting source code and provides an equivalent behavior in the most common scenarios. Nevertheless, not all the cases will achieve 100% functional equivalence since the upgrade process lacks information about object data type and its members during the application runtime.

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 1
End Sub

C# code:

public void Func(Control myControl, Form myForm, object myVar)
{
	ReflectionHelper.LetMember(myControl, "controlProp1", ReflectionHelper.GetMember<object>(myVar, "varProp1"));
	ReflectionHelper.LetMember(myForm, "formProp1", ReflectionHelper.GetMember<object>(myControl, "controlProp1"));
	ReflectionHelper.LetMember(myVar, "varProp1", ReflectionHelper.GetMember<object>(myForm, "formProp1"));

	ReflectionHelper.Invoke(myControl, "Func", new object[]{1});
	ReflectionHelper.Invoke(myForm, "Func", new object[]{1});
	ReflectionHelper.Invoke(myVar, "Func", new object[]{1});
}

VB.NET code:

Public Sub Func(ByVal myControl As Control, ByVal myForm As Form, ByVal myVar As Object)
	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", New Object() {1})
	ReflectionHelper.Invoke(myForm, "Func", New Object() {1})
	ReflectionHelper.Invoke(myVar, "Func", New Object() {1})
End Sub

8.2. Static code analysis only

By selecting this option, the VBUC will use its static code analysis process to determine the correct type for each object and resolve the late binding access.

  • Resolve Late Binding Access by using only static code analysis.

  • There are some cases where it is not possible to determine the type of an object by using a static code analysis and it may require manual intervention.

General Description:

Visual Basic 6.0 provides the ability to use late-bound variable references. This means the variables may have not been declared with their actual type, but with a generic type. E.g. “control” instead of “label”, “form” instead of “form1” or “variant” instead of any other type. At runtime Visual Basic 6 determines the actual type of the variable and invokes the appropriate methods and/or members. On the other hand, .NET will throw a compilation error whenever a member is not in the declared type of the variable.

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.

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 1
End Sub

C# code:

public void Func(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 As Object)
	'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