Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

.NET and the Revenge of VB

What is Microsoft .NET and why should you care? This article is a clear and simple introduction to the new generation of Visual Basic and other Microsoft languages, as well as an explanation of what the .NET framework is and how it relates to COM. The article also compares VB to C# and suggest when you should select either one as your language of choice under .NET.


advertisement

Few programmers have ever considered Visual Basic as a real object-oriented programming language, for example because of its lack of constructors and inheritance.

VB6 code can't access some Win32 and COM's features (e.g. multithreading) and you require other languages, such as C or C++, for low level programming jobs. Well, all this will soon be a memory, because VB's next version - Visual Basic .NET - is a fully object oriented and has all the C++ (or better, C#) features you have ever dreamt of.



Don't think that there are just some improvements to the language syntax: Microsoft entirely re-designed VB.NET to target the new development platform, the .NET Framework.

The .NET Framework

The .NET Framework is a new development environment for Windows and Web applications, and consists of a Common Language Runtime (CLR) and a Base Class Library (BCL). Becoming familiar with the .NET platform is important, because it is the infrastructure on which VB.NET is built. Microsoft delivers four .NET compilers: C++, C#, VB.NET and JScript (http://msdn.microsoft.com/net).

In theory you might write your code with Notepad and compile it with VBC.EXE (VB.NET's compiler), but the forthcoming Visual Studio .NET (whose official release is expected next autumn) provides a shared graphical environment to design, develop and debug .NET components (Figure 1).

The CLR replaces the VBRUNxx.DLL and MSVBVMxx.DLL libraries, and it is necessary for all the other .NET languages to run. The CLR manages several system services on behalf of applications, such processes, threads and objects' lifetime management.


Figure 1: Preview of Visual Studio .NET

Regardless of the language they choose, programmers spend most of their time in solving the same issues (e.g. file I/O or data access). The purpose of BCL is provide developers with a ready-to-use library for solving the most common programming problems such as OS interaction, data access (ADO.NET), and user interface (Windows Forms and WebForms). For instance, you have to invoke the Show() method of System.WinForms.MessageBox class to show a message box in VB.NET:

System.WinForms.MessageBox.Show("Hello VB.NET!")

VB's MsgBox statement is nothing more than a wrapper for System.WinForms.MessageBox.Show().

The System.Console class, instead, takes care of managing console application's I/O:

Dim str As String ' input str = System.Console.ReadLine ' output System.Console.WriteLine(str)

These are just a few examples to demonstrate that the Win32/COM mixture used so far is going to be replaced by a class library common to all .NET languages. The BCL comprises hundreds of classes in a single object model!

All for one and one for all

.NET languages are equivalent, have the same capabilities, and can interoperate at the binary level. All languages use the same set of types and the same calling and parameter passing conventions: the String type exists in C# as well as VB.NET, and both languages store it in memory in the same way.

A VB programmer can reuse C# code (and C++ or JScript as well) and vice versa: this simple interface:

public interface CSInterface { void PrintLine(string message); }

can be implemented by a VB class:

Public Class VBClass Implements CSInterface Sub PrintLine(ByVal message As String) Implements CSInterface.PrintLine System.Console.WriteLine(message) End Sub Sub OtherSub() ' do something... End Sub End Class

You can make a VB.NET class available to any other language, and VB.NET code can call C# or JScript libraries. VB.NET is finally a first-class language, as well as C# or C++.

As a consequence, cross-language development and debug are quite trivial, because all languages share the same execution environment, that is the CLR. Other companies have announced .NET languages, and there are about 20 languages in the work, not counting Microsoft's ones, including Eiffel and Smalltalk.

Microsoft has designed a series of specifications (known as Common Language Specifications, or CLS), which must be followed when designing a .NET compiler to guarantee the equivalence of all .NET languages. The VB.NET team introduced changes both in syntax and the semantics of the language to comply with these CLS specifications. For instance,.NET reports all errors - from math errors to operating system ones - through exceptions, and the CLS mandates that all .NET languages can manage them. The Try...Catch...Finally statement replaces the outdated On Error Goto statement. Here is an example demonstrating file I/O with exception handling:

' (Imports System.IO) Dim dout As Stream Dim path As String = "C:\file.txt" Try ' open the file dout = File.OpenWrite(path) Catch e As FileNotFoundException ' error, file doesn't exist, create a new file dout = File.Create(path) Finally : ' finally close the file dout.Close() End Try



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date