Assignment statements carry out assignment operations, which consist of taking the value on the right side of the assignment operator (=) and storing it in the element on the left, as in the following example.
In the preceding example, the assignment statement stores the literal value 42 in the variable v.
Eligible Programming Elements
The programming element on the left side of the assignment operator must be able to accept and store a value. This means it must be a variable or property that is not, or it must be an array element. In the context of an assignment statement, such an element is sometimes called an lvalue, for "left value."
The value on the right side of the assignment operator is generated by an expression, which can consist of any combination of literals, constants, variables, properties, array elements, other expressions, or function calls. The following example illustrates this.
The preceding example adds the value held in variable y to the value held in variable z, and then adds the value returned by the call to function findResult. The total value of this expression is then stored in variable x.
Data Types in Assignment Statements
In addition to numeric values, the assignment operator can also assign String values, as the following example illustrates.
Dim a, b As String a = "String variable assignment" b = "Con" & "cat" & "enation" ' The preceding statement assigns the value "Concatenation" to b.
You can also assign Boolean values, using either a Boolean literal or a Boolean expression, as the following example illustrates.
Dim r, s, t As Boolean r = True s = 45 > 1003 t = 45 > 1003 Or 45 > 17 ' The preceding statements assign False to s and True to t.
Similarly, you can assign appropriate values to programming elements of the Char, Date, or Object data type. You can also assign an object instance to an element declared to be of the class from which that instance is created.
Compound Assignment Statements
Compound assignment statements first perform an operation on an expression before assigning it to a programming element. The following example illustrates one of these operators, +=, which increments the value of the variable on the left side of the operator by the value of the expression on the right.
The preceding example adds 1 to the value of n, and then stores that new value in n. It is a shorthand equivalent of the following statement:
A variety of compound assignment operations can be performed using operators of this type. For a list of these operators and more information about them, see.
The concatenation assignment operator (&=) is useful for adding a string to the end of already existing strings, as the following example illustrates.
Type Conversions in Assignment Statements
The value you assign to a variable, property, or array element must be of a data type appropriate to that destination element. In general, you should try to generate a value of the same data type as that of the destination element. However, some types can be converted to other types during assignment.
For information on converting between data types, see. In brief, Visual Basic automatically converts a value of a given type to any other type to which it widens. A widening conversion is one in that always succeeds at run time and does not lose any data. For example, Visual Basic converts an Integer value to Double when appropriate, because Integer widens to Double. For more information, see .
Narrowing conversions (those that are not widening) carry a risk of failure at run time, or of data loss. You can perform a narrowing conversion explicitly by using a type conversion function, or you can direct the compiler to perform all conversions implicitly by setting Option Strict Off. For more information, see.