Declaration and Grammar Changes


Visual Basic .NET Unleashed
By Paul Kimmel
Table of Contents
Appendix A.  VB6 Programming Element Changes in VB .NET

Declaration and Grammar Changes

This section describes changes to variable declarations in Visual Basic .NET.

As Any

VB6 supported the As Any clause used for Declare statements. As Any suppresses type checking and allows you to pass any data type to the library procedure, as demonstrated.

 Private Declare Sub Sleep Lib "kernel32" _   (ByVal Milliseconds As Any) Private Sub Command1_Click()   Call MsgBox("Press [OK] to sleep")   Dim WaitFor As Long   WaitFor = 1000   Call Sleep(WaitFor)   Call MsgBox("Awake!") End Sub 

The first statement declares the API procedure Sleep. The type is declared as an As Any type. The API procedure still requires a valid argument, but if the parameter is invalid, we are not aware of it until we use File, Make or actually run and invoke the procedure. (We could also call Sleep(1000&), using the ampersand to coerce the integer 1000 to a long type.)

As Any is a weak construct anyway. When we are writing code, we want to be notified of errors as early as possible. The sooner we are aware of errors, the earlier we can reconcile them.

In Visual Basic .NET, you must specifically declare the data type of the arguments.

Variable Declaration

Visual Basic 6 supported comma-delimited, multivariable declarations.

 Dim I, J, K As Integer 

Unfortunately, the variables I and J are Variant types, which can lead to confusing code like the following:

 Dim I, J, K As Integer I = "This is not an integer!" MsgBox I, vbExclamation 

The message box displays the text "This is not an integer!" However, if you examine the variable declaration, you might infer that the author intended for I and J to be integers, but in actuality I and J are Variants and the code compiles and runs. Using Variant types results in weaker code that is more likely to behave in an unruly manner.

Visual Basic .NET provides for a more intuitive understanding. In Visual Basic .NET, Dim I, J, K As Integer interprets the intent intuitively and makes I, J, and K all integer types. You can also mix types in a single statement:

 Dim I As Integer, D As Double 

And, statements such as Dim O, where no explicit data type is used, are implemented as Object types. The Variant type is not supported. Refer to the section "Replace Variant with Object" at the end of this appendix.

Using Line Numbers

VB6 supported line numbers directly in your code. For example, we could have written the fragment from the last section as

 1 Dim I, J, K As Integer 2 I = "This is not an integer!" 3 MsgBox I, vbExclamation 

resulting in some strange -looking code. And, of course, if you change code at line 150 and have a thousand more lines of code after 150, you would have the pleasure of renumbering 850 lines of code. In addition, VB6 did not require that the line numbers be sequential or even unique.

Visual Basic .NET still supports line numbers added directly to your code text, but you must follow the line number with a semicolon.

You do not need internal line numbers for reference. The property page, which you can access by choosing Tools, Options, Text Editor, All Languages, has a Line Numbers check box and will add and maintain line number references for you automatically. If you choose File, Page Setup in Visual Studio .NET and check Line Numbers (see Figure A.1), Visual Studio .NET will print the line numbers for you (see Figure A.2).

Figure A.1. Printing line numbers for code listings in Visual Studio .NET from the Page Setup dialog box.


Figure A.2. Line numbers maintained automatically and displayed in Visual Studio .NET.


Letting the IDE manage line numbers for you is much more convenient in Visual Studio .NET.


Visual BasicR. NET Unleashed
Visual BasicR. NET Unleashed
Year: 2001
Pages: 222 © 2008-2017.
If you may any questions please contact us: