Application & Data Migration Blog Posts | Mobilize.Net

VBUC 10.1: Your VB6 to C# Migration Just Got a Lot Cooler

Written by DeeDee Walsh | Apr 14, 2024 2:17:37 PM

I'm excited to share our new VBUC release which is gonna knock your socks off - especially all you C# folks! We've been busy behind the scenes pushing some serious upgrades when it comes to migrating VB6 to C#, making your C# code slicker and more efficient. Without belaboring too long, let's get to the good stuff:

Fancy New C# Tricks

1. Auto Default Structs: Ain't Nobody Got Time for That

You know those annoying struct initialization calls in your old VB6 code? Poof! Gone! VBUC 10.1 now auto-magically initializes your structs with default values. Imagine structs just springing to life ready to use - that's what we're talking about. Code gets cleaner, you get happier.

Example

VB6 Code:
Private Type RECT
  Left As Long
  Top As Long
  Right As Long
  Bottom As Long
End Type

 

C# Previous Code:
private struct RECT
{
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;

    public static RECT CreateInstance()
    {
        RECT result = new RECT();
        result.Left = 0;
        result.Top = 0;
        result.Right = 0;
        result.Bottom = 0;
        return result;
    }
}

 

C# (with feature on):
private struct RECT
{
  public int Left;
  public int Top;
  public int Right;
  public int Bottom;
}

 

2. Global Usings: Declutter Your Code

Tired of typing those same old using statements over and over? VBUC feels your pain. Now there's this cool "GlobalUsings" file where you stash all your common using declarations once, and they magically work everywhere. It's like spring cleaning for your code!

Example

C# Previous Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        // ...
    }
}

 

C# Current Code:
namespace WindowsApplication1;

public partial class Form1 : Form
{
    // ...
}

 

GlobalUsings.cs
global using System;
global using System.Collections.Generic;
global using System.ComponentModel;
global using System.Data;
global using System.Drawing;
global using System.Linq;
global using System.Text;
global using System.Threading.Tasks;
global using System.Windows.Forms;

 

3. Expression Bodied Members: Short and Sweet

If you've got a method, event or property that's just doing one little thing, why waste space? Now you can use expression bodied members to make 'em snappy and readable. It's the little things in life, amirite? (Note: If you're an old school coder, there's an option to turn this off.)

Methods

VB6 Code:
Private Function GetName() As String
    GetName = "John Doe"
End Function

 

C# feature turned off:
private string GetName()
{
    return "John Doe";
}

 

C# feature turned on:
private string GetName() => "John Doe";

 

Events

VB6 Code:
Private Sub Button1_Click()
    MsgBox "Hello, World!"
End Sub

 

C# feature turned off:
private void Button1_Click(object sender, EventArgs e)
{
    MessageBox.Show("Hello, World!");
}

 

C# featured turned on:
private void Button1_Click(object sender, private void Button1_Click(object sender, EventArgs e) => MessageBox.Show("Hello, World!");

 

Properties

VB6 Code:
Public Property Let Name(Value As String)
    m_Name = Value
End Property

Public Property Get Name() As String
    Name = m_Name
End Property

 

C# feature turned off:
public string Name
{
    get { return m_Name; }
    set { m_Name = value; }
}

 

C# feature turned on:
public string Name
{
    get => m_Name;
    set => m_Name = value;
}

 

Libraries and Components We ❤️

1. DataEnvironment Files: Let the Data Flow

Got DataEnvironment files in your VB6 project? No problem! VBUC 10.1 now handles those migrations like a pro. Upgrading shouldn't break your data connections. 

Previous C# Code:
// DataEnvironment migration not supported

 

Current C# Code:
public class DataEnvironment1 : DataEnvironment
{
    private Connection1 connection1;
    private Command1 command1;

    public DataEnvironment1()
    {
        connection1 = new Connection1(this);
        command1 = new Command1(this);
    }

    public Connection1 Connection1 => connection1;
    public Command1 Command1 => command1;

    private sealed class Connection1 : ConnectionBase
    {
        public Connection1(DataEnvironment parent) : base(parent)
        {
            // ...
        }
    }

    private sealed class Command1 : CommandBase
    {
        public Command1(DataEnvironment parent) : base(parent)
        {
            // ...
        }
    }
}

 

2. MSMQ Interop or Native, You Decide

Working with Microsoft Message Queue? VBUC's got you covered. You can choose to generate MSMQ as Interop, or if you're feeling adventurous, go with native .NET classes. (Just a heads-up - native .NET stuff won't work if you're targeting .NET 6 or higher).

VB6 Code:
Dim objQueue As MSMQQueueInfo
Set objQueue = New MSMQQueueInfo

objQueue.FormatName = ".\Private$\MyQueue"
objQueue.Create

 

Using Interop:
MSMQQueueInfo objQueue = new MSMQQueueInfo();
objQueue.FormatName = @".\Private$\MyQueue";
objQueue.Create();

 

Using Native .NET Classes:
MessageQueue objQueue = MessageQueue.Create(@".\Private$\MyQueue");

 

3. VBScript Regular Expressions: .NET Style

Love those VBScript regular expressions? Well, now you can have them in proper .NET flavor with the System.Text.RegularExpressions class, or stick with the Interop wrapper if you like. Regex power the way you want it. 

VB6 Code: 
Dim regEx As New RegExp
Dim strPattern As String
Dim strInput As String
Dim strReplace As String
Dim strOutput As String

strPattern = "\b(Hello)\b"
strInput = "Hello, World!"
strReplace = "Hi"

With regEx
    .Global = True
    .MultiLine = True
    .IgnoreCase = False
    .Pattern = strPattern
End With

strOutput = regEx.Replace(strInput, strReplace)

 

Using Interop:
RegExp regEx = new RegExp();
string strPattern = @"\b(Hello)\b";
string strInput = "Hello, World!";
string strReplace = "Hi";
string strOutput;

regEx.Global = true;
regEx.MultiLine = true;
regEx.IgnoreCase = false;
regEx.Pattern = strPattern;

strOutput = regEx.Replace(strInput, strReplace);

 

Using Native .NET with Helpers classes:
string strPattern = @"\b(Hello)\b";
string strInput = "Hello, World!";
string strReplace = "Hi";
string strOutput;

Regex regex = new Regex(strPattern, RegexOptions.None);
strOutput = regex.Replace(strInput, strReplace);

 

Extra Awesomeness, Important Notes and Upgrade Info

VBUC 10.1 - VB6 to C# Migration includes several other features that improve the migration experience:

  • Refined databases mappings for improved accuracy and compatibility
  • Optimized maps for VSIndex to enhance performance
  • Reorganized upgrade options for better usability and clarity
  • Updated GUI colors for a fresher, more modern look

Important note: 

Support for Visual Studio 2010, 2012, 2013 and 2015 has been deprecated in this release.

Upgrading to VBUC 10.1:

To take advantage of the new C# features, expanded library support and other improvements, we recommend upgrading to VBUC 10.1. The VB6 to C# migration upgrade process is straightforward and our team is ready to assist you with any questions or concerns.