Visual Basic

Miscellaneous Conventions

This section describes some other conventions worth noting.

Dimensioning variables

Variables should each be dimensioned individually, meaning there should be only one Dim, Public, Private, or Static per variable. Multiple dimensions on a line can lead to ambiguity about the scope and type of a variable.

Defining arrays

Both the upper and lower bounds in array definitions must be specified. Do not rely on the Option Base setting (Option Base should not be used).

  Public anDigits(10) As Integer

This dimensions 11 entries in the array if the Option Base is equal to 0, which is its default.

  Public anDigits(1 to 10) As Integer

This dimensions 10 entries in the array, regardless of the Option Base setting, and is the way to dimension arrays.

Using control arrays

Control arrays must be used whenever possible and whenever appropriate in Visual Basic. These make smaller executables, which therefore lead to better performance.

Using operators

Use only the addition operator (+) in numerical expressions. Do not use it to facilitate some string concatenation. This is especially true if using variables of type Variant.

Comparing strings

StrComp is by far the fastest way to compare strings. Use it instead of a language operator (such as =) whenever possible. Binary Comparison is faster than Text Comparison (especially if you're using the Like operator), and should be used whenever it is appropriate. Generally, using fixed strings is slower, with the exception of passing strings to API or DLL procedures.

Using parentheses

While a parenthesized expression might be slower to evaluate than a non-parenthesized expression, parentheses should be applied where appropriate to ensure the readability of code.

Initializing variables

Initialize variables through assignment. Initialization or pre-assignment (in the case of Visual Basic) of variables is a good programming habit to get into.

Initialize variables as close as possible to where they are used. This avoids having to hunt for the initialization:

  Dim nCount As Integer : nCount = 1 ' for counting iterations

or

  Dim nCount As Integer ' for counting iterations
  nCount = 1             ' initialized

Calling subroutines

The Call keyword should prefix any call to a subroutine, since it makes explicit what is happening in the code. Using Call also avoids the problem of an apparent parameter being merely evaluated.

Setting control properties at design time vs. run time

As a general rule, it is a good idea to do as much in the code as possible. For example, the Mask property of a masked-edit control is best set "in code" at run time rather than at design time, because setting it in code generally makes clearer what is going on. This method also makes code more portable (because operational logic is not tied to any form property, such as in the previous example, the code is more portable and generic).

Using For...Next statements

In For loops, use Next <counter> instead of just Next. In For Each loops (VBA), use Next <element> instead of just Next. These recommendations are faster.

For...Next loops are more than twice as fast as any equivalent Do...Loop and While....Wend loops. Not surprisingly, Integer counters are the fastest of all the variable types to use as a counter.

by BrainBellupdated
Advertisement: