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

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:

VB.Net Programming Tutorial

  • VB.Net Basic Tutorial
  • VB.Net - Home
  • VB.Net - Overview
  • VB.Net - Environment Setup
  • VB.Net - Program Structure
  • VB.Net - Basic Syntax
  • VB.Net - Data Types
  • VB.Net - Variables
  • VB.Net - Constants
  • VB.Net - Modifiers
  • VB.Net - Statements
  • VB.Net - Directives
  • VB.Net - Operators
  • VB.Net - Decision Making
  • VB.Net - Loops
  • VB.Net - Strings
  • VB.Net - Date & Time
  • VB.Net - Arrays
  • VB.Net - Collections
  • VB.Net - Functions
  • VB.Net - Subs
  • VB.Net - Classes & Objects
  • VB.Net - Exception Handling
  • VB.Net - File Handling
  • VB.Net - Basic Controls
  • VB.Net - Dialog Boxes
  • VB.Net - Advanced Forms
  • VB.Net - Event Handling
  • VB.Net Advanced Tutorial
  • VB.Net - Regular Expressions
  • VB.Net - Database Access
  • VB.Net - Excel Sheet
  • VB.Net - Send Email
  • VB.Net - XML Processing
  • VB.Net - Web Programming
  • VB.Net Useful Resources
  • VB.Net - Quick Guide
  • VB.Net - Useful Resources
  • VB.Net - Discussion
  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary

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 −

IMAGES

  1. Operators in Visual Basic 6 0 Tutorial (Part

    assignment operator in visual basic 6 0

  2. Operators in Visual Basic 6.0

    assignment operator in visual basic 6 0

  3. Assignment Operator in VBA

    assignment operator in visual basic 6 0

  4. VB 6.0 WITH SUMAN: INTRODUCTION TO VISUAL BASIC

    assignment operator in visual basic 6 0

  5. Visual basic 6.0 programming environment

    assignment operator in visual basic 6 0

  6. Visual Basic 6.0 Example Programs and Sample Code

    assignment operator in visual basic 6 0

VIDEO

  1. Decision making statement part 2 || Visual basic 6.0

  2. How to use option button in visual basic 6.0

  3. C#

  4. Programming example: Copy assignment operator

  5. Label and Command Button Control in Visual Basic 6.0

  6. C#