This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
+= Operator (Visual Basic)
- 11 contributors
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.
variableorproperty Required. Any numeric or String variable or property.
expression Required. Any numeric or String expression.
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.
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 .
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.
The value of num1 is now 13, and the value of str1 is now "103".
- Assignment Operators
- Arithmetic Operators
- Concatenation Operators
- Operator Precedence in Visual Basic
- Operators Listed by Functionality
Additional resources
Navigation Menu
Search code, repositories, users, issues, pull requests..., provide feedback.
We read every piece of feedback, and take your input very seriously.
Saved searches
Use saved searches to filter your results more quickly.
To see all available qualifiers, see our documentation .
- Notifications You must be signed in to change notification settings
VB.Net - Assignment Operators
There are following assignment operators supported by VB.Net −
Try the following example to understand all the assignment operators available in VB.Net −
When the above code is compiled and executed, it produces the following result −
© Copyright 2019. All Rights Reserved.
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more
VB .NET Language in a Nutshell by Steven Roman PhD, Ron Petrusha, Paul Lomax
Get full access to VB .NET Language in a Nutshell and 60K+ other titles, with a free 10-day trial of O'Reilly.
There are also live events, courses curated by job role, and more.
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:
where <operator> is one of the arithmetic or concatenation operators. This is equivalent to:
To illustrate, consider the addition assignment operator. The expression:
is equivalent to:
which simply adds 1 to x. Similarly, the expression:
which concatenates the string "end" to the end of the string s.
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:
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:
adds 1 to the value of lNumber and assigns the result to lNumber.
Subtraction assignment operator. For example:
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.
Don’t leave empty-handed
Get Mark Richards’s Software Architecture Patterns ebook to better understand how to design components—and how they should interact.
It’s yours, free.
Check it out now on O’Reilly
Dive in for free with a 10-day trial of the O’Reilly learning platform—then explore all the other resources our members count on to build skills and solve problems every day.
The Assignment Statement
Format: <variable name> = <expression>
The assignment statement causes the value of the expression on the right side of the equal sign to be stored in the variable specified on the left side of the equal sign.
An expression can be a constant, variable, or any valid combination of constants and/or variables connected with the operators such as +, -, *, and /.
The assignment statement has the form variable = constant
(i.e., the <expression> is a constant)
If the variable name on the left of the equal sign is a string variable, then the constant must be a string constant enclosed in quotes. The quotes are not stored. Examples follow:
Assume that the following variables are declared:
The statement
strCustName = "BOB SMITH"
would cause the characters BOB SMITH to be stored in the variable strCustName.
strCustAddr = "123 MAIN ST."
would cause the characters 123 MAIN ST. to be stored in the variable strCustAddr.
You can also declare and initialize a variable in one line:
I will recommend declaring and initializing the variables in different lines.
If the variable name on the left of the equal sign is a numeric variable (Integer, Long, Single, Double,Decimal), then the constant must be a valid numeric constant. Numeric constants must not be enclosed in quotes. They must consist only of the digits 0 through 9 and can optionally have a leading sign (- or +) and may have one decimal point. Examples:
The following statements would cause the specified quantities to be stored in these variables:
Date and time values are stored internally in a special format, but you don’t need to know the exact format. They are double precision numbers: the integer part represents date and the fraction part represents the time. Assume that the following variable is declared:
Dim dtmHireDate As Date
The following statement would cause the internal representation of November 29, 1999 to be stored in the variable dtmHireDate:
dtmHireDate = #11/29/99#
The statements given below are also the valid statements
dtmHireDate = #11/29/99 6:30:11 PM#
dtmHireDate = “November 2, 1999”
dtmHireDate = Now( )
Note: Whenever we assign a value to a variable name, the previous value stored in that variable is destroyed and replaced with the new value. This is called a "destructive replacement" or "destructive write". For example, if the previous value of the variable intI was 2, and then the statement intI = 6 was executed, the new value of intI would be 6 (the 6 would replace the 2).
The assignment statement has the form variable = variable
(i.e., the <expression> is a variable)
The contents of the variable on the right of the equal sign will be copied to the variable on the left of the equal sign, replacing the previous contents of the variable on the left. The contents of the variable on the right will remain unchanged.
The examples below assume that the following variables have been declared:
The assignment statement has the form variable = expression
(i.e., the <expression> is an arithmetic expression)
If an arithmetic expression is on the right of the equal sign, the expression is evaluated and the result of the expression is stored in the variable on the left of the equal sign.
For example, given the two statements:
In the second statement above, VB .NET will determine that the variable intI contains 2, add the constant 4 to it, and store the result (6) in intJ.
Given these two statements:
In mathematics, you could never have a statement like the second one above (intI = intI + 1), because in mathematics, the "=" indicates equality. But in VB .NET, the "=" does not indicate equality; rather, it means "is replaced by" - i.e., the expression on the right (intI + 1) is first evaluated and determined to be the value 3. The 3 is then stored in the variable on the left (which happens to be intI).
Other assignment statement notes:
No expression on left of equal sign
There can never be an expression on the left of the equal sign. For example,
A + B = C + D
is invalid, it means nothing in VB .NET. By definition, the function of the assignment statement is to store a value in the variable specified on the left of the equal sign.
Assignment statements with mixed data types
In previous versions of BASIC and VB, you could only assign a string constant or another string variable to a string variable, and you could only assign a numeric constant, numeric variable, or numeric expression to a numeric variable. Violation of this rule resulted in the generation of a Type Mismatch error. In later versions of VB, this rule has been relaxed (only if Option Strict is OFF in VB .NET) – basically, VB will convert one data type to another if it possibly can; if it can't perform a suitable conversion, the "Type Mismatch " error will still occur. Generally, "mixed-mode" assignment statements should be avoided when possible; they are inefficient and may sometimes produce unexpected results.
Regarding the last statement, if the decimal portion of a Single or Double is exactly .5, VB always rounds to the nearest even number when converting to an Integer or Long. This is sometimes referred to as "bank rounding".
In the "mixed mode" assignment statements shown above, VB would perform the necessary conversions in these cases wherever it could. Such conversions are called implicit conversions .
In VB, you can also use a set of functions that explicitly convert (or "cast") one type of data to another. The set of functions that enable you to do this all begin with the letter "C": CBool, CByte, CChar, CDate, CDbl, CDec, CInt, CLng, CSng, CStr, CObj, and CShort. There are also two older functions, Val and Str, which enable you to perform conversions as well. These functions will be covered in a later topic.
Assigning Data to Arrays
Given the following definitions:
Dim aintCount(9) As Integer
Dim asngSales(4,5) As Single
The following would be valid assignment statements:
aintCount(4) = 36
asngSales(2, 3) = 12543.22
You can also assign the arrays while declaring them:
Dim aintCount(3) As Integer = {0,1,2,3}
Assigning Data to Structures
Public Structure EmployeeName
FirstName As String
MidInit As String
LastName As String
End Structure
Public Structure EmployeeRecord
udtEmpName As EmployeeName
dtmHireDate As Date
sngHourlyRate As Single
dblQuarterlyEarnings(4) As Double
Dim udtEmpRec As EmployeeRecord
Dim audtEmpRec(10 As EmployeeRecord
udtEmpRec.sngHourlyRate = 28.75
audtEmpRec(3).dtmHireDate = #1/15/2001#
udtEmpRec.udtEmpName.MidInit = "B"
audtEmpRec(4).dblQuarterlyEarnings(3) = 14950.00
Using With/End With
You can use a With/End With block to "factor out" a qualifying reference in a group of statements. For example, the following sets of statements are equivalent:
With statement blocks can be nested. The following sets of statements are equivalent:
IMAGES
VIDEO
COMMENTS
Assignment Operators (Visual Basic) Article. 09/15/2021. 10 contributors. Feedback. The following are the assignment operators defined in Visual Basic. = Operator. ^= Operator. *= Operator.
There are following assignment operators supported by VB.Net −. Operator. Description. Example. =. Simple assignment operator, Assigns values from right side operands to left side operand. C = A + B will assign value of A + B into C. +=.
The = operator assigns the value on its right to the variable or property on its left. Note. The = operator is also used as a comparison operator. For details, see Comparison Operators.
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.
The equals sign (=) is used for two entirely different operators in VB.NET. It is used as the assignment operator as well as for the equality test operator. The operator, to which the character evaluates, depends on the context. So, for instance, in this example: Dim x As Integer = 1. Dim y As Integer = 2.
Assignment Operators (Visual Basic) The following are the assignment operators defined in Visual Basic. = Operator ^= Operator *= Operator /= Operator \= Operator += Operator-= Operator <<= Operator >>= Operator &= Operator. See also. Operator Precedence in Visual Basic; Operators Listed by Functionality; Statements
There are following assignment operators supported by VB.Net −. Example. Try the following example to understand all the assignment operators available in VB.Net −. Module assignment Sub Main () Dim a As Integer = 21 Dim pow As Integer = 2 Dim str1 As String = "Hello!
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.
The Assignment Statement. Format: <variable name> = <expression>. The assignment statement causes the value of the expression on the right side of the equal sign to be stored in the variable specified on the left side of the equal sign.
VB.Net - Assignment Operators. There are following assignment operators supported by VB.Net: Exponentiation and assignment operator. It raises the left operand to the power of the right operand and assigns the result to left operand.