Skip to content

Latest commit

 

History

History
68 lines (47 loc) · 3.99 KB

addition-assignment-operator.md

File metadata and controls

68 lines (47 loc) · 3.99 KB
description title ms.date f1_keywords helpviewer_keywords ms.assetid
Learn more about: += Operator (Visual Basic)
+= Operator
07/20/2015
vb.+=
+= operator [Visual Basic]
assignment statements [Visual Basic], compound
statements [Visual Basic], compound assignment
+= operator [Visual Basic], appending strings
compound assignment statements [Visual Basic]
d3e959f4-85d4-4e47-87c4-77b62335a5b3

+= Operator (Visual Basic)

Adds the value of a numeric expression to the value of a numeric variable or property and assigns the result to the variable or property. Can also be used to concatenate a String expression to a String variable or property and assign the result to the variable or property.

Syntax

variableorproperty += expression  

Parts

variableorproperty
Required. Any numeric or String variable or property.

expression
Required. Any numeric or String expression.

Remarks

The element on the left side of the += operator can be a simple scalar variable, a property, or an element of an array. The variable or property cannot be ReadOnly.

The += operator adds the value on its right to the variable or property on its left, and assigns the result to the variable or property on its left. The += operator can also be used to concatenate the String expression on its right to the String variable or property on its left, and assign the result to the variable or property on its left.

Note

When you use the += operator, you might not be able to determine whether addition or string concatenation will occur. Use the &= operator for concatenation to eliminate ambiguity and to provide self-documenting code.

This assignment operator implicitly performs widening but not narrowing conversions if the compilation environment enforces strict semantics. For more information on these conversions, see Widening and Narrowing Conversions. For more information on strict and permissive semantics, see Option Strict Statement.

If permissive semantics are allowed, the += operator implicitly performs a variety of string and numeric conversions identical to those performed by the + operator. For details on these conversions, see + Operator.

Overloading

The + operator can be overloaded, which means that a class or structure can redefine its behavior when an operand has the type of that class or structure. Overloading the + operator affects the behavior of the += operator. If your code uses += on a class or structure that overloads +, be sure you understand its redefined behavior. For more information, see Operator Procedures.

Example

The following example uses the += operator to combine the value of one variable with another. The first part uses += with numeric variables to add one value to another. The second part uses += with String variables to concatenate one value with another. In both cases, the result is assigned to the first variable.

[!code-vbVbVbalrOperators#7]

[!code-vbVbVbalrOperators#8]

The value of num1 is now 13, and the value of str1 is now "103".

See also