Selected .NET Programming Innovations

This section presents short summaries of three selected innovations that will impact the way developers create solutions with Visual Basic .NET. While the subject is programming innovations, I specifically avoid presenting code samples to expose you to concepts. You will find this background valuable when we turn to code samples and step-by-step techniques for creating solutions.

Namespaces

A namespace is a way of organizing and exposing functionality in .NET Framework solutions. The .NET Framework types enable all .NET applications, and the way to tap .NET Framework types is through namespaces. (Selected namespaces for supporting Web and data functionality in the .NET Framework were discussed earlier.)

Your applications can reference .NET Framework types by following a two-part naming convention for denoting types. The first denotes the namespace. The second part denotes the type within a namespace. The names for .NET Framework type namespaces have a hierarchical structure that typically starts with System . Visual Basic .NET has additional namespaces: these start with Microsoft.VisualBasic . You will likely use namespaces starting with System most often, other times use Microsoft.VisualBasic . You can also create your own custom namespaces. When used like this, namespaces offer a familiar means for organizing the objects within an assembly.

A useful way for understanding namespaces is to examine specific instances of them. The Introduction to the .NET Framework Class Library in Visual Studio topic in the Microsoft Visual Studio .NET documentation includes an overview of the namespaces for the .NET Framework base class library along with links for drilling down further on namespaces for individual parts of the base class library. The System.Windows.Forms namespace facilitates the management of Windows applications containing forms with controls on them. Nested within this namespace is the System.Window.Forms.Form namespace, which points at the Windows Form class. The class name corresponds to the namespace for the class. When you start a Windows application within Visual Studio .NET, the code for the Windows Form Designer automatically inherits from the System.Window.Forms.Form class. This class exposes properties, methods , and events for controlling your application. When a Windows application initializes, the caption for the default form is Form1. You can use the form s load event procedure to reset the caption by assigning a new value to the form s Text property.

Object Orientation

The .NET Framework is object-oriented, and since Visual Basic .NET is a language supported by .NET, it is object-oriented. One interesting indication of the switch is that the Set statement for assigning object references to variables representing object instances is gone. What s the reason? All assignment statements are for objects ”even assignments to a data type. For example, the Visual Basic .NET Integer data type is derived from the System.Int32 type in the .NET Framework base class library. Since the System.Int32 type offers a collection of properties and methods, so does the corresponding Integer data type in Visual Basic .NET. Visual Basic .NET permits you to convert an Integer to a String with the ToString method. You can code int1.ToString instead of CStr(int1) . Since ToString is a method, IntelliSense reminds you of its availability as soon as you click . after the integer s name.

Visual Basic .NET offers three standard object-oriented features ”encapsulation, inheritance, and polymorphism. Encapsulation embodies the core notion of a class: that you can group properties, methods, and other members together and treat them as a single unit, such as a residence. Inheritance allows the creation of new classes ”such as single-family homes , condominiums, and cooperatives ”based on a previously existing class, such as a residence. The new class is called a derived class , and the existing class is called a base class . For example, the System.Object class is the base class in the .NET Framework. All other classes inherit originally from this class. When a derived class inherits a base class, it can add new members, change the definition of some inherited members, and hide other inherited members. Polymorphism refers to the capability to define an identically named member differently in both the base class and the derived class.

As you begin to investigate the object orientation of Visual Basic .NET, you are likely to encounter terms such as overloading, overriding, and shadowing. Overloading refers to the practice of defining multiple procedures with the same name but different argument lists. Developers can use overloading to define methods in classes that accept different arguments. Without this feature, you would have to define a separate method for each type of argument or include elaborate code in a single procedure to analyze the arguments for acting appropriately from a single procedure. Both overriding and shadowing offer different approaches for redefining members in a derived class. Either of these approaches enables polymorphism.

Exception Handling

An exception is a fault that occurs while a program is executing. Exceptions throw an instance of the Exception class. The CLR can throw an Exception class instance when a fault occurs during the execution of an application. One way to bullet-proof your applications from these Exception instances is to raise exceptions while you are testing your applications before their production deployment. If an exception goes unhandled by your program s code, your application crashes and your program ends abruptly with a system error message.

The whole purpose of exception handling is to avoid abrupt ends. With exception handling, your program can diagnose the cause of a fault and pass back guidance to the user on how to correct the fault. This kind of solution is particularly appropriate when the error causing an exception results from wrong user input. In other situations, the user cannot remedy a problem (for example, if a database file was moved or deleted). In the second situation, your exception handling can provide feedback on the cause of the error and return control to an application s form so that your user can try another task until the problem generating the error is resolved.

One exciting innovation of Visual Basic .NET is the introduction of structured exception handling. With this new technique for processing errors, you can avoid the spaghetti logic associated with unstructured exception handling. Recall that unstructured exception handling used the On Error statement with a GoTo clause. The On Error condition could trap only whether an exception occurred. You then had to work with error codes from different systems, such as Visual Basic, ADO, or DAO.

Structured exception handling removes the introduction of spaghetti code based on a GoTo clause in an On Error statement by allowing you to test for the validity of code in its normal flow. Open a Try Catch Finally statement to start structured exception handling. Insert the Try keyword just before the block of code that you want to test. At the end of a code block you want to test for run-time errors, open one or more successive Catch clauses. You can use multiple Catch clauses with a Try clause to trap different types of exceptions in a code block. By knowing the exception thrown by the CLR, you can develop the most constructive feedback to a user. This may allow the remedy of a fault by the user without any input from the program s designer.

Your sequence of Catch clauses should generally close with a trap for any error not detected by a specific filter for a previous Catch clause. After the last Catch clause in a structured error-handling statement, you can optionally include a Finally clause. If present, this clause will always execute. You can use it for actions such as releasing a database connection or closing a file. Whether or not your structured exception-handling statement includes a Finally clause, the Try Catch Finally statement must end with the End Try keyword phrase. The following layout is a template that illustrates the general design of a Try Catch Finally statement:

 Try Code block to test goes here. Catch [optional filters] Code to process a filtered exception goes here. [Additional Catch blocks] Finally This code always runs immediately before the Try statement exits. End Try 

The Try Catch Finally statement represents a new syntax for exception handling by Visual Basic developers. To use this new syntax effectively, you will have to develop an understanding of the kinds of exceptions that can occur and the range of remedial actions appropriate for different kinds of errors. Chapter 4 will illustrate typical scenarios for using structured exceptions with different kinds of errors.

 


Programming Microsoft Visual Basic. NET for Microsoft Access Databases
Programming Microsoft Visual Basic .NET for Microsoft Access Databases (Pro Developer)
ISBN: 0735618194
EAN: 2147483647
Year: 2006
Pages: 111
Authors: Rick Dobson

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