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.
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 ReadOnly (Visual Basic), 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.
In addition to numeric values, the assignment operator can also assign String values, as the following example illustrates.
You can also assign Boolean values, using either a Boolean literal or a Boolean expression, as the following example illustrates.
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 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 Assignment Operators.
The concatenation assignment operator (&=) is useful for adding a string to the end of already existing strings, as the following example illustrates.
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 Type Conversions in Visual Basic. 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 Widening and Narrowing Conversions.
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 Implicit and Explicit Conversions.