Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼


Thirteen Ways to Loathe VB

1. Procedure and function call. This area of Basic has come on in leaps and bounds. Whereas in the bad old days you had to use GOSUB, these days you have subs ('subs' is the preferred baby-speak for what grown-ups call procedures or void functions) and functions. You write:

Subname Param1, Param2

to call sub Subname and:

Result = FuncName(Param1, Param2)

to call function FuncName. Notice the useful difference in syntax, with and without parentheses, which serves more purposes than I can describe. It is of course a syntax error to write:

Subname(Param1, Param2)

but the good news is you can write:

FuncName(Param1, Param2)

to call a function and ignore its return. However, if Param1 or Param2 are reference parameters — and they will be unless you have specifically demanded value parameters — they will be treated in this specific case as value parameters, and any assignment to them discarded on exit from FuncName.

Obviously the syntax:

Call FuncName(Param1, Param2)

fixes this, and causes Param1 and Param2 to be treated as reference parameters.


2. Variable declaration. This is achieved using the intuitive keyword Dim. To declare an integer I write

Dim I As Integer

To declare a whole load of integers write:

Dim I, J, K, L As Integer

Actually (haha got you!) this doesn't work. This declares I, J, and K as variants and only L as an Integer. This almost never matters, except quite often.

3. Calling functions and accessing arrays. In most languages you can distinguish between a call to function F with parameter 3 and a reference to array F index 3 because one is written F(3) and the other F[3]. In Visual Basic they are both written F(3). Yes.

4. Another thing about arrays. The index of the first element is 0, unless it is set to 1 by a directive.

5. But there are also collections, modern object-oriented versions of arrays. And the first element of these is usually 1, unless it happens to be 0. Sometimes it is 0 and sometimes it is 1, depending on where you found it. Do you feel lucky, punk? Well, do ya?

6. Did I mention 'object-oriented' back there? Hahahahahahahahahahahahaha.

7. Initialisation. This area of Basic has come on in leaps and bounds. Whereas in the bad old days you had to use a completely barbaric mechanism based on the keywords DATA and READ, this has now been swept away. The fragment below illustrates the modern way to initialise an array in code.

Dim A(20) As Double

A(0) = 4.5 ' May work, may not — who can tell?
A(1) = 4.71
A(2) = 4.82
A(3) = 4.92

You get the idea.

8. Arrays of constants. No such thing. Anyway, what would you do with 'em if you had 'em?

9. The type Integer declares a 16-bit integer. That's right, sixteen bits. Yes I am using the latest version. Unbelievable, isn't it? Let's have a big warm EXE welcome back to code that dies suddenly around the 33 KB mark.

10. Assignment. This area of BASIC has come on in leaps and bounds. Whereas in the bad old days you used the = operator for assignment, preceding it with LET if you were a fusspot of the first order, these days you use the = operator for assignment, preceding it with Let if you are a fusspot of the first order. Or Set if it's an object. Which is compulsory not optional.

11. Logic. This particular language is supposed to be easy and intuitive, so here's a test for you. Suppose that Check1 is a checkbox on a form, and you execute the code:

Dim b As Boolean, c As Boolean
b = Check1.Value
c = Not Check1.Value

Then b as expected will contain True if the checkbox is checked and False if the checkbox is unchecked. What do you think c will contain? (Clue: always True. No, really.)

12. The four magic constants of the apocalypse: Nothing, Null, Empty, and Error.

12.5 The stupid editor, which by default will put up a whining dialog if you try to leave a line which it recognises as syntactically incorrect. Like when you leave an incomplete line temporarily to go and copy a long identifier into the clipboard, for example.

12.7 The stupid compiler, which by default does a 'compile' so superficial that you can get runtime errors caused by an If missing its End If.

12.8 Procedures, sorry 'Subs', can be declared Public, Private, or Static. Two points to anybody who correctly guesses what Static does. Three points to anybody who can suggest a sane use for it.

13. Bill is making even more money out of this. And I am powerless to stop him. In fact, I am helping him.

(Next week: Java. Verity Stob is currently appearing as a troll in every single tiresome religious discussion about languages on Usenet.)

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.