Click here to Skip to main content
15,885,366 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Exposing Class Members from .NET to Visual Basic 6

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
8 May 2012CPOL2 min read 25.2K   243   7   4
Expose those class members written in .NET, in VB6

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.

C#
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.

Image 1

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.

Image 2

Click Extract Interface and this window will show:

Image 3

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:

C#
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:

C#
public class Person : COMTesting.IPerson
{
	~
}

Then finally, we have to add ClassInterface and ServicedComponent.

C#
[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:

Image 4

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.

Image 5

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:

Image 6

Then our code:

VB.NET
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.

Image 7

License

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


Written By
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

Comments and Discussions

 
QuestionWe have a error in Vb6.0 Pin
killedman26-Jun-14 11:11
killedman26-Jun-14 11:11 
QuestionExample is limited! Pin
brown715767-Jun-13 6:20
brown715767-Jun-13 6:20 
GeneralFunciona correctamente Pin
morningstar17108-May-12 6:04
morningstar17108-May-12 6:04 
GeneralRe: Funciona correctamente Pin
Jayson Ragasa8-May-12 6:05
Jayson Ragasa8-May-12 6:05 
english please Smile | :)
Software Developer
Jayzon Ragasa
Baguio City, Philippines

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

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