January 29, 2005

VBScript Operators for Formula Properties

Autodesk® Architectural Desktop formula properties are evaluated as VBScript expressions. VBScript has a number of operators, falling into four categories: arithmetic, concatenation, comparison and logical. Descriptions of those that, in my opinion, are most likely to be used in a formula property can be found below. Hard core programmers who want to push the envelope of what can be done in a formula should probably acquire a reference or two on VBScript. I have found VBScript in A Nutshell, A Desktop Quick Reference, 2nd Edition, by Paul Lomax, Matt Childs & Ron Petrusha, quite useful. It has way more on VBScript than you likely need to know for writing formula properties, but the reference section contains a listing of the available commands and their use.

Arithmetic Operators
Used to manipulate numeric values.

Adds two numbers. Can also be used to concatenate strings, but use the concatenation operator [see below] to avoid confusion.

Subtracts two numbers, or indicates a negative value.

Divides two numbers, returning a floating point number.

Multiplies two numbers.

Divides two numbers, returning an integer – without a remainder or decimal places.

Modulo operator, divides two numbers and returns only the remainder; the original numbers are rounded to integers before performing the operation.

Raises a number to the power of an exponent. The number is first, followed by the “^” and then the exponent.

String Operator
Used to join individual strings into one string.

Concatenates two strings. Non-string values are converted to strings and then concatenated.

Comparison Operators
Used to compare two expressions, these operators return True, False or Null [only when both expressions are Null]. The first expression precedes the operator, and the second follows it. These are frequently used with If Then [ElseIf] [Else] End If statements to provide alternate formula results based on values stored in other properties [or derived from other properties].

Greater than, True if the first expression is greater than the second.

<> or ><

Not equal, True if the first expression is either less than or greater than the second.

>= or =>
Greater than or equal, True if the first expression is greater than or equal to the second.

<= or =<
Less than or equal, True if the first expression is less than or equal to the second.

Equal, True if the first expression is equal to the second.
Comparison operators work on both numeric and string values. When mixing these in a single comparison, the comparison is done in accordance with the following:

  • If both the string and number are “literal” – actually written, not through a variable, then the string is converted to a number prior to comparison.
  • If one is literal and the other is a variable, the variable value is converted to the type of the literal value prior to conversion.
  • If both are variables, the number will be evaluated as smaller than the string.

Logical Operators
Used to evaluate one or more expressions, returning a logical value [True/False/Null]. For example, a logical operator can be used to have two conditions evaluated for a single If statement. These can also be used as bitwise operators. If you are doing this in a formula property, you really do not need to read this article.

Evaluates to True if both expressions are True, otherwise, False.

Evaluates to False when both expressions are False, otherwise, True.

Negates a single expression. If the expression is True, Not evaluates to False; if the expression is False, Not evaluates to True.

Evaluates to True if both expressions are True or both are False; evaluates to False if one is True and the other False.

Evaluates to True if one is True and the other False; evaluates to False if both expressions have the same evaluation.

There is an order of precedence for evaluating operators when more than one appears on the same line. I so strongly recommend using parentheses to make your desired order of operation explicit that I am going to omit describing the VBScript hierarchy.

No comments: