Few programmers have ever consideredVisual Basic as a real object-oriented programming language, for example becauseof its lack of constructors and inheritance.
VB6 code can’t access some Win32 and COM’s features (e.g. multithreading) andyou 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#) featuresyou 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 .NETFramework
The.NET Framework is a new development environment for Windows and Webapplications, and consists of a CommonLanguage Runtime (CLR) and a Base Class Library (BCL). Becomingfamiliar with the .NET platform is important, because it is the infrastructureon 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 withNotepad and compile it with VBC.EXE (VB.NET’s compiler), but the forthcomingVisual Studio .NET (whose official release is expected next autumn) provides ashared graphical environment to design, develop and debug .NET components (Figure1).
The CLR replaces the VBRUNxx.DLL andMSVBVMxx.DLL libraries, and it is necessary for all the other .NET languages torun. The CLR manages several system services on behalf of applications, suchprocesses, 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/Oor data access). The purpose of BCL is provide developers with a ready-to-uselibrary 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.MessageBoxclass to show a message box in VB.NET:
System.WinForms.MessageBox.Show("Hello VB.NET!")
VB’s MsgBox statement is nothing more thana wrapper for System.WinForms.MessageBox.Show().
The System.Console class, instead, takescare of managing console application’s I/O:
Dim str As String' inputstr = System.Console.ReadLine' outputSystem.Console.WriteLine(str)
These are just a few examples todemonstrate that the Win32/COM mixture used so far is going to be replaced by aclass library common to all .NET languages. The BCL comprises hundreds ofclasses in a single object model!
All for one andone for all
.NET languages are equivalent, have thesame capabilities, and can interoperate at the binary level. All languages use thesame set of types and the same calling and parameter passing conventions: the Stringtype exists in C# as well as VB.NET, and both languages store it in memory inthe 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 SubEnd Class
You can make a VB.NET class available toany other language, and VB.NET code can call C# or JScript libraries. VB.NETis 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 languagesin the work, not counting Microsoft’s ones, including Eiffel and Smalltalk.
Microsoft has designed a series of specifications (known as Common LanguageSpecifications, or CLS), which must be followed when designing a .NET compilerto guarantee the equivalence of all .NET languages. The VB.NET team introducedchanges both in syntax and the semantics of the language to comply with theseCLS specifications. For instance,.NET reports all errors – from math errors tooperating system ones – through exceptions, and the CLS mandates that all .NETlanguages can manage them. The Try…Catch…Finally statement replacesthe outdated On Error Goto statement. Here is an example demonstrating file I/Owith exception handling:
' (Imports System.IO)Dim dout As StreamDim 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
Why all .NETlanguages are born equal
.NET compilers generateplatform-independent pseudo-code rather than machine code. This pseudo-code,known as Intermediate Language (or IL), is conceptually similar to VB’sp-code, which might sound as a step backwards, until you realize that IL codecan theoretically run on different CPU and operating systems, provided that a.NET runtime and a .NET framework is available for that platform (Figure 2).For the time being Microsoft has announced a CLR only for Windows operatingsystems (CE, 9x/ME, NT/2000), but they don’t exclude a porting to other operating systems.
An important difference with traditional compiled code is that p-code IL isn’t really interpreted – as the VB’s p-code is, – rather it is Just In Time compiled (or JITted) into native code when the application runs, and is optimized for the CPU on which the application is running. The JIT process tends to slow down application loading but Microsoft is implementing several optimizing techniques to minimize this problem. Moreover, they announced a JIT version that saves compile results on disk and therefore speeds up following executions. There will be also a light version (EconoJIT) that compiles one procedure at a time, discarding the native code after each execution (this version has been designed for CE-based systems with fewer resources and memory). At present, .NET applications run slower than Win32 ones, but Microsoft claims that the release versions of .NET languages will outperform existing Win32 languages, thanks to a tighter integration of the .NET framework with the operating system. |
![]() Figure2:VB.NETcode is compiled into IL p-codeand executed by the Common Language Runtime. |
Say goodbye toCOM
Broad and large, the VB programming modelremains the same, except for the syntax changes: you still work with forms, thetoolbox, properties and events (Figure 1).
You shouldn’t be cheated into believingthat nothing has changed, though: .NET is the evolution of COM technology andwill eventually replace it. Of course, the transition will be gradual and thetime you invest today on COM won’t be wasted: .NET and COM components are 100%compatible and can interoperate. This means that all COM concepts suchinterfaces, classes and components still exist and have the same fundamentalrole than ever, even though they are implemented on a different infrastructure:before you had OLEAUT32.DLL, now you work with the CLR.
Things are different for MTS/COM+:component services such transactions, object pooling, and queuing are fullyintegrated and accessible from within VB.NET. In the long run, CLR will replaceCOM+ services too, though.
VB revived
Same runtime, same libraries, and samep-code: Visual Basic is at the same level as other languages, but is this truealso as far as performance is involved? Yes, it is. Before .NET advent, VB’s badreputation was mainly due to slow execution, but now things are changed. With acorrectly designed and implemented compiler , apps will run at the same speed,regardless of which languages they are written in. More specifically, VB.NET isas fast as C# on the average, and the two compilers are structured in the sameway.
Several developers consider C# as the .NETlanguage for excellence. What has C# that VB.NET hasn’t? In my opinion, the onlyextra-features that are really relevant in .NET are operator overloading, and unsignedtypes.
Some tasks are out of reach for VB.NET,but this isn’t really important for most programmers. With VB.NET, for instance,you can finally develop Windows NT/2000 services, in a surprisingly effortlessway.
Finally, keep in mind that is alwayspossible to combine C# and VB code.. The two languages are equivalent and therearen’t many important reasons to consider either one better than the other. Youmight want to learn C#, however, if you want to better understand all theexamples and the documentation about the framework’s more advanced features.
Summary
The next version of VB will have a lot of changes: VB.NETis finally a first-class, fully object oriented language. At the same time it’salso more difficult to use, and requiresmore familiarity with object-orientedprogramming and design concepts. It’s important to not get carried away and dealwotj the changeover cautiously, and then you’ll have a new language anddevelopment environment.