Declarations in VB.NET

Some of the most productivity-enhancing changes in VB.NET are related to dimensioning and instantiating variables.

There are several things about VB.NET that are very different from previous versions of the language. So if you're now going to start programming in VB.NET as a part of Visual Studio.NET, then it's a good idea to get an overview of those changes. This tip, excerpted from InformIT, discusses just the differences in declarations in the .NET version of the language.

Dan Fox is the author of Building Distributed Applications with Visual Basic.NET.


Several of the most obvious and productivity-enhancing changes in the language are related to dimensioning and instantiating variables. As you might be aware through painful experience, in prior versions, a declaration like so:

 

  • Dim x,y,z As Integer

    did not result in three Integer variables. In fact, the first two variables were declared as Variant, which wastes memory and has the potential of causing type conversion problems down the road. In VB.NET, multiple declarations work as expected, and so the same declaration will result in three Integer variables. In addition, VB.NET supports the capability to use the declaration:

     

  • Dim z

    when the Option Strict statement is set to Off. In this case, rather than result in a Variant, the variable z is of type System.Object. Also note that unlike in previous versions, Option Explicit is defaulted to On so that variables cannot be used without first declaring them.

    VB.NET also supports parameterized constructors so that objects can be initialized during the declaration. This, coupled with support for initialization during declaration, allows VB.NET to support the following types of syntax:

     

  • Dim dtAaron As New DateTime(1974, 4, 8)

     

  • Dim strName As String = "Hank Aaron"

     

  • Dim arTheropods() As String = {"Tyrannosaur", "Allosaurus", "Deinonychus"}

    In this example, the dtAaron variable is declared as a DateTime structure and instantiated with arguments passed to the constructor specifying the day Hank Aaron hit his 715th homerun. This is shorthand for the statement:

     

  • Dim dtAaron As DateTime = New DateTime(1974, 4, 8)

    In the second example, VB.NET takes care of calling the constructor for the String variable to allow developers to work with strings in a familiar fashion. The third example shows how to initialize an array of strings during declaration.

    Although the constants you would expect (those prefixed with "vb") exist within the Microsoft.VisualBasic namespace, they also are exposed through enumerated types. Because the methods of the namespace expect the enumerated types, it is recommended that you use them rather than the constants.


    To read the article from which this tip comes, click over to InformIT. You have to register there, but the registration is free.

This was first published in November 2002

Dig deeper on Win Development Resources

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close