Introduction
This tip discusses how to use the .NET class library in Visual Basic 6.
Background
Recently, I was working with a client who wanted his application written in Visual Basic 6 to have Picasa pictures implemented. Unfortunately, there are a few ASP Classic implementations and it could be the only way I can get an idea and also, no VB6 implementation.
I knew I could do it in .NET, but the problem was that the app was written in VB6.
Now, it gave me a thought ... if it's possible to expose those class members written in .NET in VB6 because from Visual Studio 2005 to the latest version of VSs offers "Register for COM Interop" option. Though the trick doesn't end there.
Here in this tip/trick, I will show you how to make this possible.
Implementation in Visual Studio 2010
The Visual Studio version I will be using is 2010. So first, we need to create a new Class Library project and then we write our members for example like the one below.
public class Person
{
string _lastname = string.Empty;
public string LastName
{
set { _lastname = value; }
}
string _firstname = string.Empty;
public string FirstName
{
set { _firstname = value; }
}
int _age = 0;
public int Age
{
set { _age = value; }
}
public string Greet()
{
return string.Format("You're {0} {1} and {2} years old", _firstname, _lastname, _age);
}
}
Then we need to let the compiler know that we need the class to be visible in COM so in AssemblyInfo.cs, we need to set ComVisible
to true
.
from [assembly: ComVisible(false)]
to [assembly: ComVisible(true)]
.
Now going back to the class we just created, we need to create an interface based from those members by easily using the Extract
Interface feature.
Click Extract
Interface and this window will show:
This will create a new interface class based on the Person
class members. Just click OK button and the new interface and you will have something like this:
using System;
namespace COMTesting
{
public interface IPerson
{
string FirstName { set; }
string LastName { set; }
int Age { set; }
string Greet();
}
}
and your Person
class will be updated and add the IPerson
:
public class Person : COMTesting.IPerson
{
~
}
Then finally, we have to add ClassInterface
and ServicedComponent
.
[ClassInterface(ClassInterfaceType.None)]
public class Person : System.EnterpriseServices.ServicedComponent, COMTesting.IPerson
{
string _lastname = string.Empty;
public string LastName
{
set { _lastname = value; }
}
string _firstname = string.Empty;
public string FirstName
{
set { _firstname = value; }
}
int _age = 0;
public int Age
{
set { _age = value; }
}
public string Greet()
{
return string.Format("You're {0} {1} and {2} years old", _firstname, _lastname, _age);
}
}
So after that, we need to build our Class Library and by opening the BIN\DEBUG folder, we'll have something like this:
That .TLB (Type Library) file is what we need in Visual Basic 6.
Implementation in Visual Basic 6
We have now built our class and have our .tlb file to be referenced in Visual Basic 6. In the screen shot below, as you can see, I also created COMMath
. But we are currently interested in COMTesting
.
Now that we have the Type Library file, we can now use the properties and methods we created in .NET in Visual Basic 6.
Having a simple GUI:
Then our code:
Option Explicit
Dim person As New COMTesting.person
Private Sub Command1_Click()
With person
.Age = CInt(txAge.Text)
.FirstName = txFirstname.Text
.LastName = txLastname.Text
End With
MsgBox person.Greet()
End Sub
running it, then clicking the Greet button, we now have a result like this.