ASSIGNMENT OPERATORS

Visual Basic has always had the simple assignment operator =. Visual Basic .NET added several new assignment operators to handle some common statements where a value was set equal to itself combined with some other value. For example, the following two statements both add the value 10 to the variable iterations:

iterations = iterations + 10 ' Original syntax.
iterations += 10             ' New syntax.
 

All the other assignment operators work similarly by adding an equals sign to an arithmetic operator. For example, the statement A ^= B is equivalent to A = A ^ B.

You can still use the original syntax if you like. However, the new syntax sometimes gives you better performance. If the left-hand side of the assignment is not a simple variable, Visual Basic may be able to save time by evaluating it only once. For example, the following code adds 0.1 to a customer order’s discount value. By using +=, the code allows Visual Basic to find the location of this value only once.

Customers(cust_num).Orders(order_num).Discount += 0.1
 
PERFORMANCE ANXIETY
In most applications, performance is usually adequate whether you use += or the older syntax. Usually, you are best off if you use whichever version seems most natural and easiest to understand and only worry about performance when you are sure you have a problem.

The complete list of assignment operators is: =, ^=, *=, /=, \=, +=, -=, &=, <<=, and >>=.

If you have Option Strict set to On, the variables must have the appropriate ...

Get Visual Basic 2012 Programmer's Reference 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.