Assignment Statements Overview

The basic value assignment statements have the following form:

numeric_variable = arithmetic_expression       ' Comment
or
string_variable = string_expression ' Comment

where the arithmetic_expression can be arbitrarily complex and can consist of variable values and functions inter-related by the arithmetic operations described in the previous section, and string_expression can be a string variable, string function, string valued property, string constant or concatenated string value.

For all arithmetic assignment statements, the result of the statement is always converted to the data type of the variable being assigned the new value.  For example:

Dim a, b As Single, c As Integer
a = 2.25 ' Assigned floating point value
b = 3.5 ' Assigned floating point value
c = a * b ' Result of 7.875 rounded and stored as 8

In addition to the standard assignment statements (e.g. x=2), assignment operators are provided that perform an operation on a variable value and store the result back into the variable value.  For example:

 x *= 3              is equivalent to x = x * 3

Table 19-10 contains the list of assignment operators and their equivalents.

Table 19-10: Operators and Equivalents

Assignment operator Sample Use Equivalent Code

^= Operator

x ^= y

x = x ^ y

*= Operator

x *= y

x = x * y

/= Operator

x /= y

x = x / y

\= Operator

x \= y

x = x \ y

+= Operator

x += y

x = x + y

-= Operator

x -= y

x = x – y

<<= Operator

x <<= y

x = x << y

>>= Operator

x >>= y

x = x >> y

&= Operator

x &= y

x = x & y

 

In addition to simplifying and clarifying complex program statements, the use of the assignment operators can be more efficient especially when the variable or property that is being assigned the new value has a complex array index or other specification or long string values are being concatenated.  This is due to the fact that the full variable or property specification only has to be evaluated once to obtain the memory address of its value.