October 16, 1999

Write concise code: a collection of simple tips

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

Simplify and optimize expressions with And, Or and Xor operators

Let’s assume you must test whether the most significan bit of an integer value is set or not. This is the code that you usually write: ‘ two cases, depending on whether the value is Integer or LongIf intValue And &H8000 Then ‘ most significant bit is setEnd IfIf lngValue

Write concise code with the InStr function

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: ‘ test whether CHAR contains a vowel’ the standard wayIf UCase$(char) = “A” Or UCase$(char) = “E” Or UCase$(char) = “I” Or

Write concise code with the IIf function

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. ‘long wayIf Var1 > Var2 Then Var3 = Var1 / Var2Else Var3 = Var2 / Var1End If’this is more conciseVar3 = IIf(Var1

Write concise code with Boolean expressions

When setting a Boolean value based upon the result of an expression, avoid using an unnecessary If/Then/Else structure. ‘Instead of using this lengthy syntax . . .If SomeVar > SomeOtherVar Then BoolVal = TrueElse BoolVal = FalseEnd If’Use this one, which is easier to read and executes faster. The’parenthesis are

Write concise code with the Switch function

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

Write concise code with the Choose function

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: Select Case index Case 1 result = 100 Case 2 result = 250 Case 3 result = 400 Case 4 result =

Counter in For-Next Statement

It is always easy for people who know Visual Basic to write code inVBScript. But sometimes you make some common mistakes because of the implementation differences between VB and VBScript.One such common mistake is in usage of For-Next statement. In VB you canoptionally specify the name of counter variable in

Rebuild Master Database

If you want to rebuild the master database you can run Rebuildm.exe command prompt utility. This utility allows you to rebuild the master database to change the character set, sort order, or Unicode collation. You can also use it to fix a corrupted master database. The Rebuild Master utility reinstalls

No more posts to show