Nik's Technology Blog

Travels through programming, networks, and computers

Language Interoperability in the .NET Framework

.NET is a powerful framework which was built to allow cross-language support.  All .NET code is compiled to Intermediate Language (IL) whether you are developing in C#, VB.NET, J# or any other .NET language.  This means it is possible to build applications with modules written in different languages, because when the application is compiled it will all be compiled to a common language, IL.

This means a class written in one language can inherit from a class written in another language, or an object can directly call a method of another class written in another .NET language.  Visual Studio also allows you to step through all the different modules in the debugger.

To demonstrate the Language Interoperability in .NET, I'm going to call a VB.NET method from the code-behind page of a C# ASP.NET page.  This isn't something you would ordinarily want to do, but its an easy way to show the power of .NET.

In Visual Studio, right-click your website root in Solution Explorer and select add reference.  Scroll down the .NET tab until you find Microsoft.VisualBasic and click ok.

This will modify your Web.Config file by adding an assembly reference to Microsoft.VisualBasic to the assemblies list.

                <add assembly="System.Core, Version=, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                <add assembly="System.Data.DataSetExtensions, Version=, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System.Xml.Linq, Version=, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System.Web.Extensions.Design, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                <add assembly="System.Design, Version=, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
                <add assembly="System.Windows.Forms, Version=, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="Microsoft.VisualBasic, Version=, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>

To use the VB.NET language within your C# webform you need to add a using Microsoft.VisualBasic; statement at the top of your code-behind page.

In my demo example below I am going to use the VB.NET MonthName method of DateAndTime to get the current month's name and display it on my page using a ASP.NET label control.  The bold code below is VB.NET interspersed with C#.

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.VisualBasic;

public partial class language_interop : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        int month = DateTime.Today.Month;
        string monthName = DateAndTime.MonthName(month, false);
        labelMonthName.Text = monthName;

Comments are closed