The Top 10 Visual Basic Surprises for Non-VB Programmers

Wednesday Jul 21st 1999 by Al Williams

Basic has a reputation for being easy to learn-and it is. However, if you're accustomed to using a different language (C++ or Pascal, for example), you're bound to run into a few "gotchas" in using VB.

Basic has a reputation for being easy to learn-and it is. However, if you're accustomed to using a different language (C++ or Pascal, for example), you're bound to run into a few "gotchas" in using VB.

Here, in no particular order, are the top 10 surprises you'll trip over when using Visual Basic.

10. Subroutine Calls Only Use Parentheses When Using Call

Assume you have the following definitions:

Function f(n as Integer) as Integer
Sub s(n as Integer)

When calling these routines, you have several choices:

n=f(10) ' call function; parentheses required
s 10 ' call sub; parentheses not allowed
call s(10) ' call sub (as above); _
  parentheses required

This is maddening if you're used to C/C++, where everything is really a function and you always use the parenthesis.

9. Use Set to Assign Objects to Variables

Technically, the Basic keyword Let introduces an assignment. For example:

Let I=0

However, most everyone omits the Let because it is optional, leading to the more common form:


When working with objects, you must use the Set keyword:

Dim sheet as Object
Set sheet =_

8. To Delete an Object, Assign Nothing to the Variable That Contains It

Basic automatically performs garbage collection automatically for you. It frees memory that your program no longer uses. What about times when you want an object to go away now? Just assign the special value nothing to the object reference. For example:

Set Sheet = nothing

7. Strings Start at Index 1; Arrays Begin at Index 0

The creators of Basic designed it to appeal to ordinary people-people who, unlike programmers, begin counting at 1, not 0. The end result is that the first letter in a string is at position 1. To manipulate a portion of a string, use the Mid function. It takes three arguments: the string, the starting position, and the length. You can use it on either side of an assignment:

x = Mid(aString,2,1) ' the second character in the sting
Mid(aString,5,2)="xx" ' two characters @ pos 5 now "xx"

Arrays start at zero, but Basic is afraid you might not know that, so it adds an extra array element for good meaure. For example, suppose in C++ you were to write:

in x[10]; /* almost the same as Dim x(10) as Integer */
This generates an array of integers ranging from x[0] to x[9]. That is a total of 10 elements, with the tenth element having an index of 9. In VB, you might write:
Dim x(10) as Integer

This statement results in an array with elements ranging from 0 to 10, for a total of 11 elements. This prevents people who start counting at 1 from becoming confused. However, it does waste one element of the array if you are not careful. There are four ways to approach this problem: (a) ignore it and waste one element; (b) convince yourself to use the maximum array index in Dim statements instead of the count of elements; (c) use the Option Base statement to set the starting element of arrays to 1 instead of 0; or (d) use the To keyword to explicitly specify the upper and lower bounds of the array.

6. Strings Can Contain the Null Character, but Most Components Can't

VB doesn't use a null-terminated string format like many other languages. That means a string can legitimately contain zero bytes. For example:

x="Hello" & Chr(0) & "Goodby"

However, most components (being C language-based somewhere down the road) don't understand this. For example, suppose you have a text box named Text1 and you execute the following code:

x="Hello" & Chr(0) & "Goodbye"

If you examine x, you'll see it was summarily truncated at the Chr(0).

5. Basic Programs Can Dynamically Allocate Memory

Older versions of Basic couldn't dynamically allocate memory like other languages. Visual Basic, however, lets you create arrays you can resize on the fly. The trick is the Redim statement. First, you use Dim to specify an array, but you omit the size information in the parentheses. Then, when you know the size you want (which could be in a variable), you call Redim. If you change your mind, you can call Redim again. Just be careful-each time you call Redim, VB destroys the previous contents of the array unless you specify the Preserve keyword. Here's an example:

Dim msg() as String ' Dynamic array
' later we find out how many lines are in msg
' old contents destroyed, but nothing there!
Redim msg(linecount)
' even later we add a line to msg
linecount = linecount+1
' Preserve contents this time
Redim Preserve msg(linecount)

4. VB Can't Create Ordinary DLLs-Only ActiveX DLLs

Without resorting to third-party tools, you can't create an ordinary Windows DLL using Visual Basic. You can, however, create an ActiveX DLL.

3. VB Can Call Most DLLs, Including Windows Internal DLLs

Although VB can't create ordinary DLLs, it is very good at calling them. You have to write a Declare statement to define the name of each DLL function you want to call and the arguments it expects. You can't call functions with the C calling convention (most DLL functions use the Pascal calling method). You also can't pass certain arguments (notable pointers to functions). To pass pointers to items, you must specify them as ByRef in the Declare. To pass the unadorned object, use ByVal.

2. Integers Are 16 Bits Wide

For historical reasons, VB maintains 16-bit wide integers as the default interger type. If you want a 32-bit interger, you must use Long. Don't forget: The default variable type is a Variant and can hold either type of integer. You only need to worry when you explicitly declare variables of a certain type.

1. You Can Create Controls at Runtime Using Control Arrays

The way VB allows you to create controls at design time is very simple and straightforward. However, sometimes you want to create items on the fly at runtime. Fortunately, VB allows you to do this with control arrays, Given a control array, you can call Load and Unload to dynamically create and destroy controls in the array.

The VB/C++ Rosetta Stone

The Rosetta Stone was found by Napoleaon's men in Egypt. It was a stone containing a message in Egyptian heiroglyphics and Greek. Until scholars had this stone, no one could read hieroglyphics. However, since many people speak Greek (not the least of whom are Grecians), it became possible to learn the ancient Egyptian language (at least the written language).

Sometimes you know what you want to do in C++, but you can't quite come to grips with the Visual Basic syntax. Table 1 shows some C++ idioms and their equivalent VB forms.


Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved