April 27, 1999

Use the right type for constants

VB stores symbolic and literal constants internally using the simplest data type possible; this means that most common numeric constants-such as zero or one-are stored as Integers. If you use these constants in floating point expressions you can speed up your code using a constant of appropriate type, as in:

Use Sleep API to add pauses to your programs

Don’t use an empty For…Next loop to add pauses to your programs; instead, use the Sleep API function, that releases the CPU and lets other apps in the system effectively multitask: Declare Sub Sleep Lib “kernel32” (ByVal milliseconds As Long)’ pause for 5 secondsSleep 5000 Note that this technique only

GoSub are slower in compiled programs

In VB applications compiled as native code, GoSubs are 5-6 times slower than calls to regular Subs or Functions; conversely, in p-code they are considerably faster. This is one of the few examples of an optimization rule that is different in p-code from compiled applications.

Upgrade to the most recent Service Pack

OK, this is a silly suggestion, but I personally know a lot of developers that complain about Visual Basic’s bugs that were fixed in more recent Service Packs. This is especially true for the three Service Packs of VB5.As of this writing, Microsoft has released two Service Packs for Visual

And is faster the Mod

Use the And operator instead of Mod when the divisor is a number in the form 2^N. For instance, there are two methods to extract the least significant byte in an Integer: lowByte% = value% Mod 256lowByte% = value% And 255 The second one is slightly faster.

ShellSort – Sort Arrays using the ShellSort Algorithm

‘ ShellSort an array of any type” ShellSort behaves pretty well with arrays of any size, even’ if the array is already “nearly-sorted”, even though in’ particular cases BubbleSort or QuickSort can be more efficient.” LASTEL is the index of the last item to be sorted, and is’ useful if

Always declare objects with full library name

If your application uses objects from external components, either third-party or your own libraries, a good rule of thumb is to include the complete servername.classname string in the Dim statement, as in: Dim word As Word.Application This syntax ensures that if you then move the code to another application, it

Event logging doesn’t work in interpreted applications

The StartLogging and LogEvent methods of the App object only work in compiled applications. This behavior is by design and shouldn’t be considered a bug, even though it isn’t documented in the language manuals. To work around this problem, just create an ActiveX DLL component that exposes the StartLogging and

Don’t test auto-instancing variables using Is Nothing

You can’t reliably test an auto-instancing object variable using the Is Nothing test, because as soon as you reference the variable Visual Basic silently creates an object of the given class, and the test always returns False: Dim x As New MyClassIf x Is Nothing Then ‘ this line will

No more posts to show