By Irina Medvinskaya
Last time, in "Making the transition from VB6 to VB.NET,” we discussed the changes that are coming in VB.NET and considered various modifications, both automatic and manual, that are necessary to make your VB6 projects work in VB.NET. In this article, we will discuss some of the new features available in VB.NET that make it a truly object-oriented language and offer ease of development for client/server and Web applications.
Since VB.NET is going to be a true object-oriented language, there are some obvious additions to the language that are not present in VB6.
Inheritance and overriding
Implementation inheritance was one of the most-requested features by Visual Basic programmers, and the wait is finally over. Developers get full implementation inheritance and visual inheritance for forms in VB.NET. You can save time by creating a base form for your applications that contains all controls you want to be included on other forms that will inherit from it. This is a good idea if all of the forms in a project have some similar characteristics, like a company logo or some common controls.
The ability to reuse code with inheritance results in less time spent coding. The keyword Inherits allows deriving from existing classes.
Function GetData ()
Function ReadData ()
Instances of the derived class will support all methods and interfaces that are supported by the base class. However, the derived class can override some methods of the base class with the Overrides keyword. In order to override a function, it has to be declared as Overridable to prevent programmers from accidentally overriding the function that shouldn't be overridden.
VB.NET allows function overloading. Overloading offers additional flexibility by allowing creation of different versions of the same function or procedure with the same name but different argument types:
Overloads Sub InsertInfo (strInfo as String)
Overloads Sub InsertInfo (intInfo as Integer)
Without overloading, you would have to create procedures with different names or use one procedure that accepts a Variant data type parameter.
Constructions allow creating a new object instance while initializing it at the same time. Using a separate Initialize method is no longer necessary.
A necessary feature for highly scalable asynchronous applications, free threading offers concurrent processing. Developers are able to spawn a thread, which can perform a time-consuming task or execute a complex query, while the rest of the application continues. This provides asynchronous processing.
VB.NET allows declaring and initializing variables on the same line. This is not a major change, but it does save a line of code for each variable:
Dim intValue as Integer =18
is the same as
Dim intValue as Integer
Structured exception handling
Error handling can be streamlined in VB.NET with structured exception handling. Developers can nest the error handling with Try…Catch…Finally statements. If an error occurs in the code between the Try and Catch keywords, a Catch block will run. After the code runs in the Catch block, the code in the Finally block runs and allows cleaning up after the error.
Shared members allow sharing a single instance of a class data member or method among all instances of the class. A shared member exists independently of any particular class instance.
Strict type checking with Option Strict
Option Strict improves type safety and generates errors when a conversion is required that could fail in a runtime or that’s unexpected by the user. Currently, Visual Basic allows converting most data types into most other data types, but often the part or the precision of the original value can be lost when a conversion is performed. VB.NET solves this problem by adding Option Strict.
Garbage collection allows disposing of useless data. Systems with garbage collections can allocate and free objects efficiently and manage memory well. However, using garbage collection comes at the cost of not knowing exactly when the system will free objects that are no longer in use. As a result, you can no longer be certain that the Terminate event of a class will fire when you expect it to. In fact, it may not fire at all.
ADO.NET is a new version of ADO. It's an XML-based data access subsystem. ADO.NET is designed to provide data access services for applications and services under Visual Studio.NET. It offers a number of high-performance APIs for connected and disconnected data models. One of the advantages of ADO.NET is that it was designed around the disconnected architecture. This means that applications are connected to the database only while the actual data is passed, and they’re not hanging around when not necessary, which makes applications more scalable. In ADO.NET, XML is the fundamental format of data, and ADO.NET automatically converts the data into and from XML. However, knowing XML is not necessary in order to use ADO.NET because programmers can use ADO.NET's data with standard programming methods.
ADO.NET offers a number of benefits, which include the following:
- Scalability—ADO.NET offers disconnected data access.
- Interoperability—ADO.NET is based on XML, an industry standard for transmitting datasets. Any component that can read XML will be able to process an ADO.NET dataset.
The end of “DLL hell”
VB.NET promises to nearly eliminate DLL hell. The issue of versioning is greatly simplified in the .NET framework. What often happens is that one application overwrites some shared DLL file, and some other application no longer works. The .NET framework includes a strong internal naming system that will make it less likely for one file to be mistaken for another. In addition, a feature referred to as "side by side deployment" allows for an application to repair itself in case one of its libraries has been overwritten by another application. When an application is started, it checks all its shared files. If one of those files has been modified and has therefore become incompatible, the application can ask the runtime to obtain a correct version of the file.
The framework is able to handle versioning of shared components separately without causing DLL problems. Another advantage of the new framework is that applications created in Visual Studio.Net will not need the Windows Registry, so installing an application just involves copying all necessary files into a designated directory.
Visual Studio macros
Visual Studio offers an extensibility model for extending, automating, and customizing the IDE; developers can automate repetitive tasks and record macros.
Windows forms allow control anchoring and docking for better form design and layout. To support this, VB.NET offers some new controls.
Control anchoring and docking
Control anchoring provides automatic control resizing when the form is resized. Control docking allows docking a control to any side of the form.
The new controls to support Windows Forms include the following:
- Link label—This control provides linking to a specified URL and appears underlined.
- Tray icon—This control enables developers to create applications that run in the Windows tray.
- Print Preview—Windows Forms offer a printing framework for easier printing, including a Print Preview window and Print Preview control.
In addition to Windows Forms, Visual Studio.NET offers Web Forms that work with ASP.NET and Web services to allow creating applications for the Web. Web Forms are similar to Windows Forms. They offer familiar features, such as drag-and-drop and code-behind-objects. Web services are built and accessed using XML and Simple Object Access Protocol (SOAP).
Microsoft designed the .NET framework with deep integration of XML and SOAP. XML, an industry standard format for data transferring, is text based and enables transferring data with any protocol. The applications' data components will exchange data with other components in other applications that can understand XML. Visual Studio.NET simplifies development of Web services by integrating XML throughout the tool. SOAP is a set of rules used to interpret the data and commands. It offers a standard XML grammar for application interoperability.
Expected changes in VB.NET Beta 2
Additional changes are expected in the Beta 2 release of VB.NET. These changes include the following:
- The value of True—True will equal –1, as in previous versions of Visual Basic.
- Array declaration—Arrays will be declared by specifying the upper bound, not the size, as in previous versions of Visual Basic.
- Behavior of And/Or/Not/Xor—These will continue to act as bitwise operators.
VB.NET has many long-awaited features, including some that will facilitate Web development. Of course, if you have an inventory of VB6 code, some effort will be required to convert those existing programs to the new framework. However, in the end, it should be worth the effort.
Irina Medivnskaya is a programmer/analyst at Citigroup for the Treasury Systems department in New York.
What are your VB.NET plans?
Do any of the new features in VB.NET interest you? What benefits will you be looking for in VB.NET? Send us an e-mail with your thoughts and suggestions or post a comment below.