Assignment Operators

Along with the equal operator, there is one assignment operator that corresponds to each arithmetic and concatenation operator. Its symbol is obtained by appending an equal sign to the arithmetic or concatenation symbol.

The arithmetic and concatenation operators work as follows. They all take the form:

expression1 <operator>= expression2

where <operator> is one of the arithmetic or concatenation operators. This is equivalent to:

expression1 = expression1 <operator> expression2

To illustrate, consider the addition assignment operator. The expression:

x += 1

is equivalent to:

x = x + 1

which simply adds 1 to x. Similarly, the expression:

s &= "end"

is equivalent to:

s = s & "end"

which concatenates the string "end" to the end of the string s.

Tip

All of the “shortcut” assignment operators—such as the addition assignment operator or the concatenation assignment operator—are new to VB .NET.

The assignment operators are:

=

The equal operator, which is both an assignment operator and a comparison operator. For example:

oVar1 = oVar2

Note that in VB .NET, the equal operator alone is used to assign all data types; in previous versions of VB, the Set statement had to be used along with the equal operator to assign an object reference.

+=

Addition assignment operator. For example:

lNumber += 1

adds 1 to the value of lNumber and assigns the result to lNumber.

-=

Subtraction assignment operator. For example:

lNumber -= 1

subtracts 1 from the value of lNumber and assigns the ...

Get VB .NET Language in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.