Cross the Line

Build and debug projects inheriting multiple .NET languages.

A Day in the Life of a Developer

LANGUAGES: VB .NET | C#

TECHNOLOGIES: Cross-Language Integration | Cross-Language Debugging

 

Cross the Line

Build and debug projects inheriting multiple .NET languages.

 

By Doug Seven

 

This past week, I've been doing the keynote presentation for the Microsoft Visual Studio .NET Academic fall road show. One of the things we noticed university students seem to have in common is knowing more than one programming language. So we decided it would be great to demonstrate how the .NET Framework and the Common Language Runtime (CLR) enable not only cross-language integration of assemblies, but also cross-language inheritance and debugging of assemblies.

 

During the keynote, I opened Visual Studio .NET and built an interface and a class that implement the interface in a Visual Basic .NET project. Then I built a class in a new C# project. The C# class inherited the Visual Basic .NET class and extended it. And if that weren't enough, I then ran the project with debugging on and stepped through the code, across the language barrier. Check out the accompanying download file - which incorporates both C# and VB .NET to see what I did (see end of article for download details).

 

The first piece is the IPerson interface in a Visual Basic .NET class library project named BaseClasses. The IPerson interface defines the properties that must be implemented in any class that implements the IPerson interface. Two properties are defined, FirstName and LastName - properties that all Person instances will have:

 

Public Interface IPerson

 

    Property FirstName() As String

    Property LastName() As String

 

End Interface

 

The Person class exists in the same project as the IPerson interface and implements the interface. This requires the Person class to expose the FirstName and LastName properties. Additionally, I exposed an Email property not required by the IPerson interface:

 

Public Class Person

    Implements IPerson

 

    Private _firstName As String

    Public Property FirstName() As String Implements IPerson.FirstName

        Get

            Return _firstName

        End Get

        Set(ByVal Value As String)

            _firstName = Value

        End Set

    End Property

 

    Private _lastName As String

    Public Property LastName() As String Implements IPerson.LastName

        Get

            Return _lastName

        End Get

        Set(ByVal Value As String)

            _lastName = Value

        End Set

    End Property

 

    Private _email As String

    Public Property Email() As String

        Get

             Return _email

        End Get

        Set(ByVal Value As String)

            _email = Value

        End Set

    End Property

 

End Class

 

In a C# class library project (named MiddleClasses), I created a class named Employee. The employee class represents employees in a company. Of course, employees are people, so the Employee class inherits the BaseClasses.Person class, which was developed in Visual Basic .NET, demonstrating cross-language inheritance:

 

using System;

 

namespace MiddleClasses

{

  /// <summary>

  /// The Employee is a Person, so it derives from the

    /// BaseClasses.Person class. This gives the Employee

    /// all of the properties, methods, and events of the

    /// Person class.

  /// </summary>

  public class Employee : BaseClasses.Person

  {

    public Employee()

    {}

 

    private Guid _employeeID;

    public Guid EmployeeID

    {

      get{return _employeeID;}

      set{_employeeID = value;}

    }

  }

}

 

Lastly, to demonstrate the cross-language debugging capabilities of Visual Studio .NET, I created a third project, a C# Web application (named WebSite in the sample solution). In the C# Web application, I added references to the BaseClasses and MiddleClasses assemblies and created an instance of the MiddleClasses.Employee class. I used a Web form to set the values of the Employee object, and using the debugger I am able to step from the C# Web application into the C# Employee class, and also into the Visual Basic .NET Person class.

 

The file referenced in this article is available for download.

 

As a co-founder of DotNetJunkies, a content-based online training resource for .NET developers, Doug Seven has been building applications with the .NET Framework since summer 2000. Seven has co-authored five books related to the .NET Framework: Programming Data-Driven Web Applications with ASP.NET (Sams), ASP.NET: Tips, Tutorials & Code (Sams), Professional ADO.NET (Wrox), Developing Custom Controls for ASP.NET (Sams), and ASP.NET Security (Wrox). Seven's professional .NET consulting clients include Microsoft, the Massachusetts Institute of Technology (MIT), and Tricordia LLC, and the work he has been involved in includes both C# and Visual Basic .NET, Web applications, mobile device applications, XML Web Services, Windows Forms development, and console and service applications. E-mail Doug at mailto:[email protected].

 

 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish