Language Interoperability

Snoops

   

 
Migrating to .NET: A Pragmatic Path to Visual Basic .NET, Visual C++ .NET, and ASP.NET
By Dhananjay  Katre, Prashant  Halari, Narayana  Rao  Surapaneni, Manu  Gupta, Meghana  Deshpande

Table of Contents
Chapter 2.   New Features in Visual Basic .NET


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 :

 graphics/icon01.gif 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 .

 graphics/icon01.gif 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 .

 graphics/icon01.gif 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#.


Snoops

   
Top


Migrating to. NET. A Pragmatic Path to Visual Basic. NET, Visual C++. NET, and ASP. NET
Migrating to. NET. A Pragmatic Path to Visual Basic. NET, Visual C++. NET, and ASP. NET
ISBN: 131009621
EAN: N/A
Year: 2001
Pages: 149

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net