Click here to Skip to main content
Click here to Skip to main content

Exposing Class Members from .NET to Visual Basic 6

By , 8 May 2012
Rate this:
Please Sign up or sign in to vote.

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Jayson Ragasa
Founder Capploud
Philippines Philippines
I am an experienced Applications Developer and had worked professionally for over 6 years. I have been writing codes and building different applications for over 13+ years. My work is mostly for Microsoft technologies such as .NET. Yes I am Microsoft technology enthusiast.
 
My field of expertise in .NET technology are Desktop and Windows Mobile and Windows Phone. I occasionally write ASP.NET too for clients.
 
I have wide experience of different programming languages and scripts such as: Turbo Pascal, Batch Scripts, C/C++, Visual Basic Classic, Visual Basic .NET, Java, HTML, CSS, ASP Classic, VB Script, ASP.NET, T-SQL, MySQL, PHP, C#, Javascript, jQuery, HTML5, RegEx, XAML, XML, JSON, and XPath
 
I am also experienced in different platforms such as: Google Data API, Google Map API, Twitter API, Facebook API, Flickr API, Skydrive API, SVN, GitHub, Drupal, and Orchard.
 
I am interested in Microsoft technologies, User Experience and User Interfaces, Algorithms, Robotics, Astronomy, Architecture, Electrical, Mechanics, and Extra Therestrial Life on other planets.
 
I am also offering free coding and application development consultations with students having a problem with their Thesis projects.
 
View my full Curriculum Vitae here
http://ss.jaysonragasa.net/?mycv
Follow on   Twitter

Comments and Discussions

 
QuestionExample is limited! Pinmemberbrown715767-Jun-13 6:20 
GeneralFunciona correctamente Pinmembermorningstart17108-May-12 6:04 
GeneralRe: Funciona correctamente PinmemberJayson Ragasa8-May-12 6:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 8 May 2012
Article Copyright 2012 by Jayson Ragasa
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid