April 20, 1999

Enum constants that include spaces

If you’re writing an ActiveX control, you can create properties that return an enumerated value, as in: Public Enum SizeConstants SizSmall = 1 SizMedium SizLargeEnd EnumPublic Size As SizeConstants When another developer is using your control, an enumerated property appears in the Property Window at design time as a combo

Never use the GetLastError API function

If you heavily use API calls and strictly follow the SDK documentation, you might be tempted to use the GetLastError API to retrieve the error returned by your last API function or procedure. However, this function doesn’t always return valid error codes, because Visual Basic internally does a lot of

The status of mouse buttons

Visual Basic lets you test the state of mouse buttons only inside a MouseDown, MouseMove, or MouseUp event procedure. To determine the current state of mouse buttons you can use one of the following functions: Private Declare Function GetAsyncKeyState Lib “user32” (ByVal vKey As Long) As _ IntegerFunction LeftButton() As

LowWord – The least significant word of a Long value.

Private Declare Sub CopyMemory Lib “kernel32” Alias “RtlMoveMemory” (dest As _ Any, source As Any, ByVal bytes As Long)’ Return the low word of a Long valueFunction LowWord(ByVal value As Long) As Integer CopyMemory LowWord, value, 2End Function

Interpreted or Compiled?

It is highly unfortunate that Visual Basic doesn’t offer a way to execute a group of statements only when the program is interpreted in the environment or only when it has been compiled (indifferently as native code or p-code) to an EXE file. What we badly need is a conditional

The number of dimensions of an array

Using “pure” VB, the only way to build a generic routine that returns the number of dimensions of an array passed as an argument is using a loop that repeatedly tests the LBound (o UBound) function until it fails: Function ArrayDims(arr As Variant) As Integer Dim i As Integer, bound

Short-circuit evaluation with Select Case

Short-circuit evaluation is an optimization technique automatically adopted by most modern compilers, including all flavors of C++, Borland Delphi and many others. Unfortunately, the Visual Basic compiler is not in this group. This optimization cuts down the time needed to evaluate a boolean expression, such as: If x > 0

Create a GUID

When you build your ActiveX controls and components, Visual Basic automatically creates all the GUIDs as necessary. The same also happens in other cases, without you even realizing it: for instance when you make a MDB database replicable, the Jet Engine adds new fields and uses GUIDs to mark their

The status of shift keys

Visual Basic lets you test the state of the Shift, Alt and Ctrl keys only within the KeyUp, KeyDown and all the mouse-related event procedures. If you want to test the state of these keys from within another routine, you can resort to the following functions: Private Declare Function GetAsyncKeyState

No more posts to show