Let Statement

Description

Assigns the value of an expression to a variable or property.

Syntax

[Let] varname = expression

The Let statement syntax has these parts:

Part

Description

varname

Name of the variable or property; follows standard variable naming conventions.

expression

Value assigned to the variable.


Remarks

In order to simplify Basic code, the optional Let keyword is most often omitted.

A value expression can be assigned to a variable only if it is of a data type that is compatible with the variable. You can't assign string expressions to numeric variables, and you can't assign numeric expressions to string variables. If you do, an error occurs at compile time.

Variant variables can be assigned either string or numeric expressions. However, the reverse is not always true. Any Variant except a Null can be assigned to a string variable, but only a Variant whose value can be interpreted as a number can be assigned to a numeric variable. Use the IsNumeric function to determine if the Variant can be converted to a number.

Caution

Assigning an expression of one numeric data type to a variable of a different numeric data type coerces the value of the expression into the data type of the resulting variable.

Let statements can be used to assign one record variable to another only when both variables are of the same user-defined type. Use the LSet statement to assign record variables of different user-defined types. Use the Set statement to assign object references to variables.

See Also

Const Statement, Data Type Summary, IsNumeric Function, LSet Statement, Set Statement, Variant Data Type.

Example

This example uses statements with and without the Let statement to assign the value of an expression to a variable.


' The following variable assignments use the Let statement.
Let MyStr = "Hello World"
Let MyInt = 5
' The following are the same assignments without the Let statement.
MyStr = "Hello World"
MyInt = 5