Datasheet

Language Features
Visual Basic has an intuitive, self-documenting syntax but it also has a few annoying quirks. In an
attempt to make every customer happy, Microsoft has left in some features and options that it probably
should have removed. These are generally syntactic annoyances and don’t cause much trouble if you are
aware of them:
Strict Type Checking Type checking in Visual Basic .NET is stricter than it was in Visual Basic 6,
but it’s still looser than in some other languages. For example, by default, Visual Basic 2005
doesn’t let you assign an Integer to a String (
s=12), but it will allow you to assign an Integer to
a Long (
long_var = integer_var). Sometimes that’s what you want to do, but sometimes it’s
an indication that there’s something wrong with your design. It also costs some execution time
as the value is converted from one data type to another.
Option Explicit This option shouldn’t be optional, it should always be on. I’ve never seen an
example where turning this option off was necessary.
Option Strict This option should also always be on. Unfortunately, there are very rare cases
when it’s convenient to bypass type checking and turn Option Strict off (for example, when
coercing parameters in and out of library function calls). If you must turn Option Strict off, do
so in the smallest module you can build to get the job done. Use partial classes, if necessary, so
that Option Strict is on in the rest of the application.
IIF Generally, an IIF statement is harder to read than a corresponding multi-line
If Then
Else
statement, although I have seen cases where IIF can make a series of very simple state-
ments easier to compare. IIF is also less efficient than
If Then Else because it always evaluates
both results even though only one is used. Usually you can avoid IIF.
Pointers Visual Basic uses references to refer to objects rather than actual pointers. That makes
working with true pointers somewhat awkward. For example, if an API routine returns a
pointer to a chunk of memory that contains one of several different data structures, it’s more
difficult to bully the result into the right kind of Structure type in Visual Basic than it is in lan-
guages such as C++ that use pointers explicitly. However, explicit use of pointers and manual
memory allocation leads to some of the most difficult kinds of bugs to find in C++. The times
when I’ve missed pointers have been pretty rare, so on the whole, Visual Basic is better off with
references. The Marshal class provides methods that coerce pointers from one data type to
another, so usually a Visual Basic program can produce the correct results with a little extra
work.
Strings In Visual Basic, a String is more than a pointer to a series of characters in memory as it
is in C and C++. If your application performs extremely complex string manipulation, those lan-
guages may give better performance. On the other hand, you can convert Strings in Visual Basic
into an array of characters, and then manipulate the entries in the array. You couldn’t use C-
style character pointer arithmetic, but you can use array indices.
Garbage Collection The garbage collection techniques used by .NET take control of memory
management away from the developer. The garbage collector can run at any time, and you have
almost no control over when or how it does its job. You also cannot tell when an object will
finally be destroyed and its resources freed (this is called non-deterministic finalization). That
forces you to implement other methods such as the IDispose interface to allow the program to
free resources before an object is garbage-collected. The garbage collector makes some of these
issues more complicated than necessary, but they should only be a real showstopper in real-time
applications.
6
Part I: Design
05_053416 ch01.qxd 1/2/07 6:28 PM Page 6