Section F.3. Other New Features


F.3. Other New Features

Some features included in Visual Basic 2005 are completely new, unrelated to any existing 2003 version features.

F.3.1. ClickOnce Installation

Visual Studio has included features to develop Windows Installer-based setup projects since the initial .NET release. The 2005 releases of the .NET Framework and Visual Studio add a new installation type called ClickOnce that provides an alternative to the standard installation process. While not officially part of Visual Basic, this new feature impacts Visual Basic as if it were a language enhancement.

Normally, the installation of complex applications requires the person performing the installation to be at least a local administrator on the workstation. However, there may be instances where this is neither convenient nor possible. ClickOnce applications can be installed by any user without the assistance of a local administrator, although there may be limits on the system resources available to the application. ClickOnce applications can also be configured to support automatic updates of installed components from a web site or server.

F.3.2. Continue Statement

Visual Basic includes various loops, including For, For Each, and Do...Loop constructs. It has always been possible to exit a loop immediately using a related Exit statement, such as Exit For. But there was no feature that would let you immediately skip to the next pass through the loop. Such an action required complex conditional constructs, or at least a GoTo statement.

    For counter = 1 To 10       ' ----- Do some processing here, then       If (someCondition) Then GoTo NextIteration       ' ----- More code here.    NextIteration:       ' ----- This label exists simply to skip to the next loop counter.    Next counter 

Visual Basic 2005 includes a new Continue statement that returns to the top of the loop with the next iteration in place, as if the current pass through the loop had been completed successfully.

     For counter = 1 To 10        ' ----- Do some processing here, then        If (someCondition) Then Continue For        ' ----- More code here.     Next counter 

F.3.3. Generics

The collection classes included in the .NET Framework (through System.Collections) and the Visual Basic Collection class (through Microsoft.VisualBasic.Collection) are great for managing a lot of objects, but each collection stores the objects in a weakly typed manner. You can put any type of object into the collection; each object you add to a collection instance can be of a unique type.

Visual Basic 2005 adds a new feature called generics that allows you to create a strongly typed collection. A collection, once bound to a specific class through the generics declaration, can only be used to store that specific class or its descendants; any other use will generate an exception. Several new generics-specific collection classes appear in the System.Collections.Generic namespace.

A collection is bound to a type using the new Of keyword.

     Dim oneClassOnly As System.Collections.Generic.List(Of Employee) 

You can also use generics in the design of your own custom classes. Chapter 10 discusses the use and syntax of generics.

F.3.4. Operator Overloading

Visual Basic has supported method overloading in classes since the initial .NET release of the language. Other languages, such as C#, also supported operator overloading , where standard language operators, such as the + addition operator, could be given custom meanings when used with specific classes. Visual Basic now includes this feature in its 2005 release.

Consider a class that manages video media. The code to join together two portions of video would likely be quite complex, but the syntax to join the videos could be straightforward.

     Dim clip1, clip2, clip3 As VideoClip     ...     clip3 = clip1 + clip2 

The VideoClip class would include an overloaded operator definition that provides the logic to perform the custom addition.

     Friend Class VideoClip       Public Shared Operator +(ByVal firstClip As VideoClip, _             ByVal secondClip As VideoClip) As VideoClip          ' ----- Append one clip to another.          Dim largerClip As New VideoClip          ' ...more code here...          Return largerClip      End Operator   End Class 

Your class can also define custom conversions from one class or data type to another by overloading the CType conversion function. Operator overloading is discussed in the "Operator Overloading" section of Chapter 5.

F.3.5. Using Statement

Some classes acquire resources that must be manually released when an instance of the class is no longer needed. This is done by calling the class's Dispose method. Visual Basic 2005 includes the new Using statement to simplify the process of resource acquisition and release.

The Using statement is a block construct that includes code at the start that acquires the resource to use. For instance, "pens" used to draw on the display screen or other drawing surface, defined in the System.Drawing.Pen class, must be disposed of when no longer needed. This is normally done with a separate call to the class's Dispose method. But it can also be accomplished in a single Using statement.

     Using redPen As System.Drawing.Pen = _           New System.Drawing.Pen(Brushes.Red)        ' ----- Add drawing code here.     End Using 

The redPen instance is guaranteed to be disposed of properly, even if you jump out of the Using block abnormally. In this way, Using acts somewhat like the Finally block in a try...Catch statement.

For more information on implementing the Using statement in your code, see its entry in Chapter 12.

F.3.6. XML Comments

Visual Basic 2005 includes a new XML Comments feature that lets you decorate your class members with special XML-formatted comments. Visual Studio recognizes and uses these comments to enhance the development environment. To use XML comments , place the insertion point on a blank line in your code, just above a method definition, then type three single-quote marks. Immediately, even before pressing the Enter key, the following template (or one similar to it) appears.

     ''' <summary>     '''     ''' </summary>     ''' <param name="sender"></param>     ''' <param name="e"></param>     ''' <remarks></remarks> 

Once these parts are filled in with the appropriate content, Visual Studio uses the information to display more verbose IntelliSense details concerning the related method. It also exports the XML content to a documentation file during the compile of the assembly.




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

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