Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
This chapter is from the book

VB Language Enhancements

Former VB developers will be pleased to find that the edit-and-continue feature is back in Visual Studio 2005! However, that's really an IDE feature. In fact, many new IDE features are considered language-specific. We intend to cover most (if not all) of them throughout the book. The IDE enhancements specific to VB include all of the following:

  • Developing with My
  • Edit-and-continue
  • Code snippets
  • IntelliSense enhancements
  • Attribute editing in the Properties window
  • Error correction and warning
  • Exception Assistant
  • XML documentation
  • Document Outline window
  • Project Designer
  • Settings Designer
  • Resource Designer

These enhancements (and more) help make VB great. However, in the following sections, we intend to focus on the language of VB. We want to point out the VB-specific additions that are so compelling in the 2005 release.

The Continue Statement

The new Continue statement in VB allows developers to skip to the next iteration in a loop. You use the Continue statement in combination with either Do, For, or While depending on the type of loop you're working with. If you want to short-circuit the loop and skip immediately to the next iteration, you simply use Continue For|Do|While, as in the following example:

Sub ProcessCustomers(ByVal customers() As Customer)
  Dim i As Integer
  For i = 0 To customers.GetUpperBound(0)
    If customers(i).HasTransactions = False Then Continue For
End Sub

Unsigned Types

Visual Basic developers can now use unsigned integer data types (UShort, UInteger, and ULong). In addition, the latest version of VB provides the signed type SByte. These new types allow VB developers to more easily call functions in the Windows API because these functions often take and return unsigned types. However, these unsigned types are not supported by the common language specification (CLS). Therefore, if you write code that uses these new types, CLS-compliant code may not be able to work with this code.

IsNot Operator

The new IsNot operator in VB allows developers to determine whether two objects are the same. Of course, VB developers could do this in prior versions by combining Not and Is as in If Not myCustomer Is Nothing. However, VB developers can now use the less awkward syntax of the IsNot operator, as in the following line of code. Note that this example is functionally equivalent to using the prior Not ... Is syntax.

If cust IsNot Nothing Then

Using Block

VB developers who have spent some time with C# will undoubtedly love the capability to define an object's scope with a Using block. With this block, C# developers have been able to guarantee disposal of a resource when the application's execution left a given block for any reason. Good news: This feature has now been added to VB. Suppose, for example, that you want to open a connection to a database. You can now do so with the Using block. This way, when execution leaves this block for any reason, the object defined by the Using statement (SQL connection object) will be disposed of properly. The following code illustrates this new feature:

Using cnn As New System.Data.SqlClient.SqlConnection(cnnStr)
  'place code to use the sql connection here
End Using

Form Access Similar to VB6

Developers familiar with Visual Basic version 6 (prior to .NET) will recall having direct access to a form's properties and methods simply by using its name. In prior versions of .NET, developers were forced to create an instance of the form to access its properties. In VB8, developers can once again access a form's members by using its name directly.

Explicit Zero Lower Bound on an Array

In past incarnations of VB (prior to .NET) developers could indicate the upper and lower bounds of an array using the To keyword. Developers were able to define an array as starting at 1 and going "to" 10 for instance. This made code that used arrays very easy to read. However, with the advent of .NET and the common language specification (CLS), arrays were forced as zero (0) lower bounds. That is, every array in .NET starts with a zero element. This doesn't change with VB8. However, the ability to define your arrays as starting at 0 and going "to" an upper bound is back, simply for code readability. Therefore, you can define arrays as in the following line of code, but you must define the lower bound as zero (0):

Dim myIntArray(0 To 9) As Integer

Operator Overloading

If you have written class libraries long enough, you eventually need to define the behavior of your class when used with an operator such as addition (+), subtraction (-), multiplication (*), greater than (>), or similar. For example, if you need to calculate the result of how two versions of your class are added together with the + operator, you need to define the + operator behavior in your code. In prior versions of VB, you could not do this. VB8 allows for what is called operator overloading.

You define a new operator by using the keyword Operator (in place of Sub or Function), followed by the operator symbol you intend to overload (+, &, *, <>, and so on). You can then write this "function" as you would any other. It can take parameters and return a value. As an example, if you were going to define how two versions of your object are added together, you would define a + operator that took each version as a parameter and returned a third version as the result. The following code illustrates this structure:

Public Operator +(ByVal obj1 As MyObject, ByVal obj2 As MyObject) As MyObject
  'calculate objects and return a new version
End Operator

Custom Events

Visual Basic developers are now given control of what happens when delegates are registered with a given developer-defined event. VB has added the keyword Custom for use when declaring an event. When you use this keyword to declare an event, you are then required to define accessors for AddHandler, RemoveHandler, and RaiseEvent. These accessors override the default behavior of an event with your own custom code. This capability is useful in situations in which you want all your events to be fired asynchronously, or you need finite control over these operations.

  • + Share This
  • 🔖 Save To Your Account