Microsoft .NET Framework offers a very good cross-language interoperability. This means that a class can be implemented in Visual Basic extra .NET can be derived from a class defined in C#. Similarly, a class in C# can derive from a class in Visual Basic .NET. The following code example (using C#) is found in the folder BaseClassLibrary for this chapter. It illustrates how language interoperability is achieved between C# and Visual Basic .NET. Consider a base class written in C# as follows : using System; namespace BaseClassLibrary { public abstract class BaseClass { public BaseClass() { } public virtual void Display() { } } } The class BaseClass defines a simple constructor and a method named Display . The C# keyword virtual indicates that the method can be overridden. The first class is made in a project called BaseClassLibrary . It is an application of type ClassLibrary . Imports System Public Class DerivedClass _ Inherits BaseClassLibrary.BaseClass Public Overrides Sub Display() Console.WriteLine("This is from the VB Code") End Sub End Class The derived class exists in a Visual Basic .NET console application. In this project, the BaseClassLibrary is added as a reference to the project so that we can access the classes from the BaseClassLibrary . Imports System Module Module1 Sub Main() Dim objClass1 As BaseClassLibrary.BaseClass objClass1 = New DerivedClass() objClass1.Display() Console.ReadLine() End Sub End Module When we execute the preceding console application, we get the output "This is from the VB code." The preceding code snippets illustrate the cross-language interoperability in which a class written in the Visual Basic .NET language overrides another class written in C#. |