Reader Level:

Language Interoperability

By Ankit Nandekar on Apr 20, 2011
.NET Language interoperate with each other that is code of one language can be consumed from other after getting converted to IL.

The .NET languages interoperate with each other; that is, code of one language can be used from another after getting converted to IL. To test this create a new project in VB.Net with a template Class Library and name it vbproject.

By default the project comes with a class class1.

Write the following code in it

Public Class Class1
    Public Function Add(ByVal x As Integer, ByVal y As Integer) As Integer
        Return x + y
    End Function
    Public Sub Hello(ByVal name As String)
        Console.WriteLine("Hello" & name)
    End Sub

As we can't execute the class we need to only compile it & convert it into an assembly. To do this open the solution explorer and right click on the project.

Select Build to build your project

Consuming the Class from C# programing-

Now create a new project in C#; name it Testvb and add a reference for the vb project assembly from its physical location which is in projects bin\Debug folders.
To add a reference follow following steps-

  1. Open solution explorer.
  2. Right Click on project .
  3. Click on Add reference..
  4. Click on Browse Button.
  5. Go to your project's physical path for example if your project name is myvb then assembly path will be C:\myvb\bin\Debug here you can find the assembly.
  6. Click the OK Button.

Here is a Screen shot which will help you to add a reference:


Now write Following code under Testvb

Note: don't Forget to add a namspace using VbProject; which is our  VB project name.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VbProject;
namespace Testvb
    class Program
        static void Main(string[] args)
            Class1 obj = new Class1();

Now Run your Console Application .



JetBrains ReSharper Ultimate
.NET code analysis, refactorings, navigation, unit testing, code coverage and profiling in Visual Studio, a bonus for C++ devs, bundled at 50% off.

Trending up