|I l @ ve RuBoard|
VB.NET introduces some new features that many developers have been asking for. While these features can provide greater functionality for your programs, they make programming in VB.NET more complex, so it is essential that you understand these new features before putting them to use.
One of the most anticipated new features found in VB.NET is support for structured exception handling. In addition to the unstructured exception handling supported in previous versions of Visual Basic using On Error statements, VB.NET now supports robust structured exception handling via the Try...Catch...Finally statement:
Try Customer = CustomerList(1) Catch e as IndexOutOfRangeException 'Handle error gracefully Customer = New Customer() Finally 'Do any final clean up here CustomerList = Nothing End Try
In this example, the Try block of the Try...Catch...Finally statement contains the code to monitor for errors. When an error is raised, each of the Catch block expressions is evaluated to find a match. If a matching Catch block is found, control is transferred to that block. Otherwise, control is passed back up to any enclosing Catch blocks. If a match still does not exist, the error is not caught and is raised as usual. Note that multiple Catch blocks are allowed within a single Try...Catch...Finally statement.
Structures Replace UDTs
Another major change in the language is the evolution of User-Defined Types (UDTs). In Visual Basic 6.0, UDTs and their members had public access and were created using the Type...End Type declaration.
In VB.NET, UDTs have been evolved into (or replaced with, depending on your view of things) structures. Structures are created with the Structure...End Structure construction and their members must have an access modifier ( Public , Protected , and so on).
Constructors and Destructors
A powerful new feature of VB.NET that has been long available in other OOP languages is that of class constructors and destructors. These special methods , Sub New and Sub Finalize , allow for the initialization and disposal of resources associated with the instantiation of classes such as database connections or network connections. These methods replace the Class_Initialize and Class_Terminate methods from VB 6.0 and run before any other code from the class is executed. Coupled with the new overloading feature, multiple parameterized constructors may be created for added flexibility. Declaration of constructors and destructors is discussed in the section on classes.
One of the more powerful features of VB.NET that C and C++ developers should be familiar with is that of overloading. Methods can be overloaded, meaning that they share the same name but have unique signatures. This is a useful means of providing computation on different kinds of parameters while avoiding creating unique method names for a variety of different input combinations. Consider the following example:
Class Shape Public Height, Width As Integer Public Function Perimeter() As Integer Return ((Height * 2) + (Width * 2)) End Function Public Function Perimeter(Length As Integer) As Integer ' assume square Return (Length * 4) End Function Public Function Perimeter(Length As Integer, Width As Integer) As Integer ' assume rectangle Return ((Length * 2) + (Width * 2)) End Function End Class
In this example, we are able to calculate the perimeter of a variety of shapes using the overloaded method Perimeter .
Namespaces and Assemblies
In Visual Basic 6.0, the VB project name served as the hierarchical organization of classes in the project. VB.NET extends this concept to include robust capabilities to organize types into namespace hierarchies and file assemblies. This topic is discussed in depth later in this appendix.
Visual Basic.NET supports inheritance, the ability to derive classes from pre-existing classes. Derived classes extend the members of the base class by overriding inherited members or by adding functionality by adding members. All classes created with Visual Basic.NET are inheritable by default.
One of the drawbacks of the VB 6.0 language was the threading models available. With Visual Basic.NET, you can write multithreaded and free-threaded applications that perform multiple tasks simultaneously . This becomes very useful when developing scalable applications. Please consult another text on VB.NET or the .NET Framework docs for a complete discussion of this topic.
Like the C# language, VB.NET employs automatic memory management. Transparent to developers, the language uses garbage collection to move objects around in memory and clean up after unused objects.
|I l @ ve RuBoard|