Interoperating with Different Programming Languages


The objective of this section is to illustrate that .NET really supports cross-programming language use. You will learn how classes defined in one programming language can be subclassed in another and utilized in yet another. First, define the Base class in C#.

 
 using System; namespace mixed {   public class Base   {     public Base()     {     }     public virtual String GetLanguage()     {         return "C#";     }   } } 

This program is compiled into a library using the C# compiler.

 
 csc /t:library CSharp.cs 

Next, a Visual Basic .NET class is declared as a subclass of the Base class.

 
 Imports System Namespace mixed     Public class Derived         Inherits Base         Public Sub New()         End Sub         Public Overrides Function GetLanguage() As String             return "Visual Basic .NET"         End Function     End Class End Namespace 

To compile the Visual Basic program, the Visual Basic .NET compiler is invoked, passing the reference to the previously created base class assembly.

 
 vbc /reference:CSharp.dll /target:library VBasic.vb 

Now you eventually develop a Java class that actually uses the classes defined earlier. The Java class has a main method and so can be compiled into an executable.

 
 vjc /reference:CSharp.dll,VBasic.dll VJSharp.jsl 

The program VJSharp.exe can be executed to get the results.

 
 Visual Basic .NET Derived is an instance of Base 

This small exercise should remove any doubts regarding the .NET interoperability of the various programming languages.

SHOP TALK : SELECTING THE RIGHT PROGRAMMING LANGUAGE

The .NET programming model provides developers a true choice when selecting a programming language to implement an application or when creating a component library. Selecting the language to use primarily depends on your existing skills and is your personal choice. If you are a Visual Basic programmer, you will probably pick Visual Basic .NET. If you are a C++ programmer, you will either continue with Visual C++ .NET or consider C# as your major programming language. If you have come from a Java programming background, you will probably end up with either Visual J# or Visual C#. Another factor that would influence the choice of programming language is ubiquity and market demand in your area. For instance, currently the New York City metropolitan area (where I live) seems to be headed in the C# direction, based on its high C++ influence. Although it is definitely possible to mix different programming languages in a single solution, in my opinion, it is rarely done. In many ways, different programming languages can be utilized to achieve similar results; sometimes the choice of a language is influenced by what sort of application is being written. For instance, Perl might be an excellent candidate for a bunch of .NET components that do heavy pattern matching and text processing. In a nutshell , when you select a programming language, you should look at your existing skills and combine them with the programming constructs provided to you by the different languages to make an informed decision.




Microsoft.Net Kick Start
Microsoft .NET Kick Start
ISBN: 0672325748
EAN: 2147483647
Year: 2003
Pages: 195
Authors: Hitesh Seth

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