Example: Hello World


As stated previously, the .NET Framework is both a specification and an implementation. Given that this book is intended for use by developers, it is about time that we demonstrated some of the framework's abilities . This simple example will highlight just how powerful and flexible the language integration between .NET languages is.

Listing 1.1 defines a Python class called HelloWorldPY .

Listing 1.1 Python class: HelloWorldPY
 class HelloWorldPY:     def __init__(self):         self.message = "Hello World from Python!"     def SayHello(self):         _com_return_type_="System.Void"         COR.System.Console.WriteLine(self.message) if __name__=="__main__":     AnObject = HelloWorldPY()     AnObject.SayHello() 

The class HelloWorldPY defines two methods , a constructor ( __init__ ) and SayHello . Although not explicitly stated, it inherits from the CLR's base class Object ; this implicit inheritance may seem unusual to some developers, such as those familiar with C++. Nevertheless, many languages and architectures whose type systems have a standard base class also support implicit inheritance from the base class, even if that fact is not explicitly stated in the source code. The constructor sets an instance member called message to the value "Hello World from Python!" . The first line in the SayHello method designates that the method has no return value, _ com_return_type_= "System.Void" , and the second line writes the value of message to the console. Developers familiar with Python will realize that the first line is not standard Python; rather, it uses a compiler extension to support the CLR.

Listing 1.2 defines a C++ class called HelloWorldCPP , which inherits from the Python class. HelloWorldCPP overrides the method SayHello , which it inherited from its base class. This implementation of SayHello calls its base class's method and writes a single string to the console. The __gc keyword is an extension to standard C++; it indicates that the class is a managed class and that instances of this class are garbage collected by the CLR. This extended form of C++ is known as the Managed Extensions C++; for for brevity, in this book we use the term Managed C++ to refer to these extensions.

Listing 1.2 C++ class: HelloWorldCPP
 #using <mscorlib.dll> #using "Python.dll" #using "HelloWorldPY.dll" using namespace System; __gc public class HelloWorldCPP: public HelloWorldPY {   public:   void SayHello()   {     __super::SayHello();     Console::WriteLine("Hello World from C++!");   } }; 

Listing 1.3 defines a Visual Basic class that inherits from the C++ class. The Visual Basic class, which is called HelloWorldVB , also overrides the method called SayHello . Again, in this implementation, the base class's method is called and a string is then written to the console. As compared to Python and Managed C++, Visual Basic does not use language extensions to expose the CLR faculties ; instead, the actual language has been extended. As an example of these modifications is the addition of new keywords such as Inherits .

Listing 1.3 Visual Basic class: HelloWorldVB
 Imports System Imports HelloWorldCPP Public Class HelloWorldVB Inherits HelloWorldCPP   Overrides Sub SayHello()     MyBase.SayHello()     Console.WriteLine ("Hello World from Visual Basic!")   End Sub End Class 

Listing 1.4 defines a COBOL class that inherits from the Visual Basic class. Like the previous classes, HelloWorldCOB overrides SayHello , again calling its base class's method and then writing a string to the console. Unlike the previous classes, the COBOL class uses the COBOL keyword DISPLAY to write the string to the console.

Listing 1.4 COBOL class: HelloWorldCOB
 000010 CLASS-ID. HelloWorldCOB INHERITS HelloWorldVB. 000020 ENVIRONMENT DIVISION. 000030 CONFIGURATION SECTION. 000040 REPOSITORY. 000050     CLASS HelloWorldVB AS "HelloWorldVB". 000060 OBJECT. 000070 PROCEDURE DIVISION. 000080 METHOD-ID. SayHello. 000090 PROCEDURE DIVISION. 000100     INVOKE SUPER "SayHello". 000110     DISPLAY "Hello World from COBOL!". 000120 END METHOD SayHello. 000130 END OBJECT. 000140 END CLASS HelloWorldCOB. 

Listing 1.5 defines a C# class that inherits from the COBOL class. Like its predecessor classes, HelloWorldCS overrides the SayHello method, once again calling its base class's method and then writing a string to the console.

Listing 1.5 C# class: HelloWorldCS
 using System; class HelloWorldCS: HELLOWORLDCOB {   override public void SayHello()   {     base.SAYHELLO();     Console.WriteLine("Hello World from C#!");   }   public static int Main()   {     HelloWorldCS h = new HelloWorldCS();     h.SayHello();     return 0;   } } 

The class HelloWorldCS provides a function called Main , which serves as an entry point so that the C# file can be compiled into an executable file. Compiling and executing the HelloWorldCS class produces the following output:

 Hello World from Python!  Hello World from C++! Hello World from Visual Basic! Hello World from COBOL! Hello World from C#! 

Of course, the idea of creating classes where each individual function is written in a different language is definitely not the point of the .NET Framework. The benefit of using the framework is that now classes, libraries, and tools built in one language can be used in other languages. For example, developers need not know or care which language the Base Framework is written in ”they just use it. [3]

[3] Most of the Base Framework is written in C#, but this choice is more an implementation detail than a necessity.



Programming in the .NET Environment
Programming in the .NET Environment
ISBN: 0201770180
EAN: 2147483647
Year: 2002
Pages: 146

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