|
Static Variables are slower than Dynamic ones
|
Total Hit (3838) |
Referencing a static local variable in a procedure is 2-3 times slower than a regular local, dynamic variable; if you want to really speed up your procedures, convert all static variables into module-level variables.
The only drawback to this approach is that the procedure become less self-conta
....Read More |
Rating
|
|
|
Terminate the process with an ErrorLevel code
|
Total Hit (3573) |
If your VB application is meant to be called from within a Ms-Dos batch file, you must be able to return Dos an error code. This can be accomplished using the ExitProcess API function:
«Code LangId=1»
Private Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)
' terminate the pro
....Read More |
Rating
|
|
|
Testing a key in a PropertyBag without raising an error
|
Total Hit (3021) |
When you pass a non-existing key to the WriteProperty method of the PropertyBag object you get an error 327 "Data value named 'namekey' not found". This is more or less the same error - albeit with a different error code - that you receive when you pass an non-existing key to the Item method of a Co
....Read More |
Rating
|
|
|
The "Assume No Aliasing" compiler option
|
Total Hit (2872) |
A procedure is said to contain aliased values if it can refer to the same memory addresses in two or more distinct ways. A typical example is the following procedure:
«Code LangId=1»
Dim g_GlobalVariable As Long
...
Sub ProcWithAliases(x As Long)
x = x + 1
g_GlobalVariable = g_GlobalV
....Read More |
Rating
|
|
|
Use ActiveControl to stop a loop
|
Total Hit (3017) |
Often your user interface includes fields whose contents mutually depends on other fields. A typical example is when you deal with conversions from one measurement unit to another. Let's say that you want to let your users enter a temperature either in Celsius or Fahrenheit degrees, so you provide t
....Read More |
Rating
|
|
|
Use function name as a local variable
|
Total Hit (3214) |
Many programmers don't realize that it is perfectly legal to use the name of a function inside the Function itself, as if it were a local variable. This trick often lets you avoid the declaration of a temporary variable, and sometimes can speed up the code. Take for example the following code:
«
....Read More |
Rating
|
|
|
Use the right type for constants
|
Total Hit (3002) |
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 typ
....Read More |
Rating
|
|
|
Write concise code with Boolean expressions
|
Total Hit (2995) |
When setting a Boolean value based upon the result of an expression, avoid using an unnecessary If/Then/Else structure.
«Code LangId=1»
'Instead of using this lengthy syntax . . .
If SomeVar > SomeOtherVar Then
BoolVal = True
Else
BoolVal = False
End If
'Use this one, which is easi
....Read More |
Rating
|
|
|
Write concise code with the Choose function
|
Total Hit (2826) |
The Choose function lets you often make more concise, albeit not faster, code, because it lets you replace a lengthy Select Case block. For example, the following code:
«Code LangId=1»
Select Case index
Case 1
result = 100
Case 2
result = 250
Case 3
resu
....Read More |
Rating
|
|
|
Write concise code with the IIf function
|
Total Hit (2130) |
Use IIf when setting a value based upon an expression. This works only in situations where you have two choices, based upon the result of a Boolean expression.
«Code LangId=1»
'long way
If Var1 > Var2 Then
Var3 = Var1 / Var2
Else
Var3 = Var2 / Var1
End If
'this is more concise
Var
....Read More |
Rating
|
|
|
Write concise code with the InStr function
|
Total Hit (2041) |
You can often use the Instr function in an unorthodox way to write more concise code. A typical example is when you need to test a single character:
«Code LangId=1»
' test whether CHAR contains a vowel
' the standard way
If UCase$(char) = "A" Or UCase$(char) = "E" Or UCase$(char) = "I" Or UCas
....Read More |
Rating
|
|
|
Write concise code with the Switch function
|
Total Hit (1715) |
Many VB developers don't realize that the Switch built-in function can often save a lot of code. This function takes any number of (expr, value) pairs, it evaluates all expressions and return the value related to the first expression that is found to be True. Typically you can use a Switch function
....Read More |
Rating
|
|
|
Write concise code: a collection of simple tips
|
Total Hit (2109) |
If you fully understand how VB and VBA work, you can often save some statements. This makes your listings more concise and more readable, and indirectly optimizes your program. Here is a short list of the many tricks you can use with this goal. You don't need to initialize a numeric variable to 0 or
....Read More |
Rating
|
|
|
CompareValue - Check whether a value is in a list of values
|
Total Hit (1834) |
«Code LangId=1»' Compares a numeric or string value with a list of other values.
' Returns the 1-based index of the matching value, or zero if the
' value doesn't appear in the list.
' String comparisons are case-sensitive.
'
' This function can conveniently replace a Select Case or a list
'
....Read More |
Rating
|
|
|
KeepInRange - Ensure that a value is in a given range
|
Total Hit (1552) |
«Code LangId=1»' Keep the first argument in the range [lowLimit, highLimit]
' If the value is adjusted, the fourth (optional) argument is set to True
'
' Note that value and limit arguments are variant, so you can use
' this routine with any type of data.
Function KeepInRange(value As Varian
....Read More |
Rating
|
|
|
NZ - Check whether a value is Null
|
Total Hit (1687) |
«Code LangId=1»' Check if a value is Null. If not it returns the value,
' otherwise it returns the ValIfNull argument, or zero/null string
' if the second argument is omitted
'
' This function is patterned after the Access function with the same name.
Public Function NZ(CheckVar As Varian
....Read More |
Rating
|
|
|
ProjectName - The name of the current project
|
Total Hit (1836) |
«Code LangId=1»' Returns the name of the current project
' The first time it's called it clears the error code
'
Function ProjectName() As String
Static result As String
If Len(result) = 0 Then
On Error Resume Next
' cause a dummy, harmless error
Err.Raise 9
....Read More |
Rating
|
|
|
|
Dealing with Circular References
|
Total Hit (1601) |
VB and COM makes a lot of things easy to do, particularly using and dealing with objects: you just create them and normally they clear themselves up automatically once you're finished with them. However, one side effect of the way COM works is that it is possible to create objects which cannot termi
....Read More |
Rating
|
|
|
Implementing Unsigned Right and Left Shift Operators
|
Total Hit (1266) |
When translating C code to VB, often you come across the Right Shift and Left Shift operators ( << and >> respectively ). There is no inbuilt support for Right and Left Shift in VB, so you have to code your own. One of the main problems with replacing these functions is that C code often uses them i
....Read More |
Rating
|
|
|
HiWords and LoWords from Long Values
|
Total Hit (695) |
When translating C code to VB, you quite often come across the HiWord and LoWord operators, used to pack two integers into a long value. A simple translation of HiWord code will run into difficulties when unsigned integer arithmetic is being used in the C code and the highest bit of the long value c
....Read More |
Rating
|
|
|