Tuesday, August 31, 2010

How to make a C# class available to VB6

At my company, we have a legacy application written in VB6 that still has a lot of life left in it.  Converting this application to C# is a daunting task, and we simply don't have the resources to do all in one step.

What we've decided to do is use C# to write new class objects that we need and use these from VB6.  As we get time and find that we can make improvements to the VB6 versions of existing classes, we'll replace those objects with new objects written in C#.  We'll try to use C# User Controls for new GUI elements.  What this does is allow us to develop new components in C# and slowly migrate existing code into C#, all while still releasing new versions.  As we get more pieces written in C# we can later make the full transition and move away from VB6 completely.

For this to work, we need to be able to do two things:
  1. Create class objects in C# that have properties, methods, and events that we can use from VB6.
  2. Create user controls in C# that we can place on VB6 forms, with properties, methods, and events.

I found that there are tons of web pages that talk about COM Interop from .NET, but very few that have step-by-step examples of what you need to do to actually get it to work.

For my first entry, I'll go through the steps for making a COM object in C# that you can directly use from VB6, written in Visual Studio 2008.  If there's interest, I'll step through how we're creating UserControls in C# that we can put on VB6 forms.