VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Ron Petrusha, Paul Lomax

Get full access to VB.NET Language in a Nutshell, Second Edition 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 result ...

Get VB.NET Language in a Nutshell, Second Edition 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.

Cover of Software Architecture Patterns

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.

assignment operator example in vb.net

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

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. VB.Net is rich in built-in operators and provides following types of commonly used operators −

Arithmetic Operators

Comparison operators, logical/bitwise operators, bit shift operators, assignment operators, miscellaneous operators.

This tutorial will explain the most commonly used operators.

Following table shows all the arithmetic operators supported by VB.Net. Assume variable A holds 2 and variable B holds 7, then −

Show Examples

Operator Description Example
^ Raises one operand to the power of another B^A will give 49
+ Adds two operands A + B will give 9
- Subtracts second operand from the first A - B will give -5
* Multiplies both operands A * B will give 14
/ Divides one operand by another and returns a floating point result B / A will give 3.5
\ Divides one operand by another and returns an integer result B \ A will give 3
MOD Modulus Operator and remainder of after an integer division B MOD A will give 1

Following table shows all the comparison operators supported by VB.Net. Assume variable A holds 10 and variable B holds 20, then −

Operator Description Example
= Checks if the values of two operands are equal or not; if yes, then condition becomes true. (A = B) is not true.
<> Checks if the values of two operands are equal or not; if values are not equal, then condition becomes true. (A <> B) is true.
> Checks if the value of left operand is greater than the value of right operand; if yes, then condition becomes true. (A > B) is not true.
< Checks if the value of left operand is less than the value of right operand; if yes, then condition becomes true. (A < B) is true.
>= Checks if the value of left operand is greater than or equal to the value of right operand; if yes, then condition becomes true. (A >= B) is not true.
<= Checks if the value of left operand is less than or equal to the value of right operand; if yes, then condition becomes true. (A <= B) is true.

Apart from the above, VB.Net provides three more comparison operators, which we will be using in forthcoming chapters; however, we give a brief description here.

Is Operator − It compares two object reference variables and determines if two object references refer to the same object without performing value comparisons. If object1 and object2 both refer to the exact same object instance, result is True ; otherwise, result is False.

IsNot Operator − It also compares two object reference variables and determines if two object references refer to different objects. If object1 and object2 both refer to the exact same object instance, result is False ; otherwise, result is True.

Like Operator − It compares a string against a pattern.

Following table shows all the logical operators supported by VB.Net. Assume variable A holds Boolean value True and variable B holds Boolean value False, then −

Operator Description Example
And It is the logical as well as bitwise AND operator. If both the operands are true, then condition becomes true. This operator does not perform short-circuiting, i.e., it evaluates both the expressions. (A And B) is False.
Or It is the logical as well as bitwise OR operator. If any of the two operands is true, then condition becomes true. This operator does not perform short-circuiting, i.e., it evaluates both the expressions. (A Or B) is True.
Not It is the logical as well as bitwise NOT operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make false. Not(A And B) is True.
Xor It is the logical as well as bitwise Logical Exclusive OR operator. It returns True if both expressions are True or both expressions are False; otherwise it returns False. This operator does not perform short-circuiting, it always evaluates both expressions and there is no short-circuiting counterpart of this operator. A Xor B is True.
AndAlso It is the logical AND operator. It works only on Boolean data. It performs short-circuiting. (A AndAlso B) is False.
OrElse It is the logical OR operator. It works only on Boolean data. It performs short-circuiting. (A OrElse B) is True.
IsFalse It determines whether an expression is False.
IsTrue It determines whether an expression is True.

We have already discussed the bitwise operators. The bit shift operators perform the shift operations on binary values. Before coming into the bit shift operators, let us understand the bit operations.

Bitwise operators work on bits and perform bit-by-bit operations. The truth tables for &, |, and ^ are as follows −

p q p & q p | q p ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

Assume if A = 60; and B = 13; now in binary format they will be as follows −

A = 0011 1100

B = 0000 1101

-----------------

A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A  = 1100 0011

We have seen that the Bitwise operators supported by VB.Net are And, Or, Xor and Not. The Bit shift operators are >> and << for left shift and right shift, respectively.

Assume that the variable A holds 60 and variable B holds 13, then −

Operator Description Example
And Bitwise AND Operator copies a bit to the result if it exists in both operands. (A AND B) will give 12, which is 0000 1100
Or Binary OR Operator copies a bit if it exists in either operand. (A Or B) will give 61, which is 0011 1101
Xor Binary XOR Operator copies the bit if it is set in one operand but not both. (A Xor B) will give 49, which is 0011 0001
Not Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. (Not A ) will give -61, which is 1100 0011 in 2's complement form due to a signed binary number.
<< Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. A << 2 will give 240, which is 1111 0000
>> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A >> 2 will give 15, which is 0000 1111

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
+= Add AND assignment operator, It adds right operand to the left operand and assigns the result to left operand C += A is equivalent to C = C + A
-= Subtract AND assignment operator, It subtracts right operand from the left operand and assigns the result to left operand C -= A is equivalent to C = C - A
*= Multiply AND assignment operator, It multiplies right operand with the left operand and assigns the result to left operand C *= A is equivalent to C = C * A
/= Divide AND assignment operator, It divides left operand with the right operand and assigns the result to left operand (floating point division) C /= A is equivalent to C = C / A
\= Divide AND assignment operator, It divides left operand with the right operand and assigns the result to left operand (Integer division) C \= A is equivalent to C = C \A
^= Exponentiation and assignment operator. It raises the left operand to the power of the right operand and assigns the result to left operand. C^=A is equivalent to C = C ^ A
<<= Left shift AND assignment operator C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2
&= Concatenates a String expression to a String variable or property and assigns the result to the variable or property.

Str1 &= Str2 is same as

Str1 = Str1 & Str2

There are few other important operators supported by VB.Net.

Operator Description Example
AddressOf Returns the address of a procedure.
Await It is applied to an operand in an asynchronous method or lambda expression to suspend execution of the method until the awaited task completes.
GetType It returns a Type object for the specified type. The Type object provides information about the type such as its properties, methods, and events.
Function Expression It declares the parameters and code that define a function lambda expression.
If It uses short-circuit evaluation to conditionally return one of two values. The If operator can be called with three arguments or with two arguments.

Operators Precedence in VB.Net

Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator −

For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.

Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.

Operator Precedence
Await Highest
Exponentiation (^)
Unary identity and negation (+, -)
Multiplication and floating-point division (*, /)
Integer division (\)
Modulus arithmetic (Mod)
Addition and subtraction (+, -)
Arithmetic bit shift (<<, >>)
All comparison operators (=, <>, <, <=, >, >=, Is, IsNot, Like, TypeOf...Is)
Negation (Not)
Conjunction (And, AndAlso)
Inclusive disjunction (Or, OrElse)
Exclusive disjunction (Xor) Lowest

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Operator Statement

  • 12 contributors

Declares the operator symbol, operands, and code that define an operator procedure on a class or structure.

attrlist Optional. See Attribute List .

Public Required. Indicates that this operator procedure has Public access.

Overloads Optional. See Overloads .

Shared Required. Indicates that this operator procedure is a Shared procedure.

Shadows Optional. See Shadows .

Widening Required for a conversion operator unless you specify Narrowing . Indicates that this operator procedure defines a Widening conversion. See "Widening and Narrowing Conversions" on this Help page.

Narrowing Required for a conversion operator unless you specify Widening . Indicates that this operator procedure defines a Narrowing conversion. See "Widening and Narrowing Conversions" on this Help page.

operatorsymbol Required. The symbol or identifier of the operator that this operator procedure defines.

operand1 Required. The name and type of the single operand of a unary operator (including a conversion operator) or the left operand of a binary operator.

operand2 Required for binary operators. The name and type of the right operand of a binary operator.

operand1 and operand2 have the following syntax and parts:

[ ByVal ] operandname [ As operandtype ]

Part Description
Optional, but the passing mechanism must be .
Required. Name of the variable representing this operand. See .
Optional unless is . Data type of this operand.

type Optional unless Option Strict is On . Data type of the value the operator procedure returns.

statements Optional. Block of statements that the operator procedure runs.

returnvalue Required. The value that the operator procedure returns to the calling code.

End Operator Required. Terminates the definition of this operator procedure.

You can use Operator only in a class or structure. This means the declaration context for an operator cannot be a source file, namespace, module, interface, procedure, or block. For more information, see Declaration Contexts and Default Access Levels .

All operators must be Public Shared . You cannot specify ByRef , Optional , or ParamArray for either operand.

You cannot use the operator symbol or identifier to hold a return value. You must use the Return statement, and it must specify a value. Any number of Return statements can appear anywhere in the procedure.

Defining an operator in this way is called operator overloading , whether or not you use the Overloads keyword. The following table lists the operators you can define.

Type Operators
Unary , , , ,
Binary , , , , , , , , , , , , , , , , , , ,
Conversion (unary)

Note that the = operator in the binary list is the comparison operator, not the assignment operator.

When you define CType , you must specify either Widening or Narrowing .

Matched Pairs

You must define certain operators as matched pairs. If you define either operator of such a pair, you must define the other as well. The matched pairs are the following:

= and <>

> and <

>= and <=

IsTrue and IsFalse

Data Type Restrictions

Every operator you define must involve the class or structure on which you define it. This means that the class or structure must appear as the data type of the following:

The operand of a unary operator.

At least one of the operands of a binary operator.

Either the operand or the return type of a conversion operator.

Certain operators have additional data type restrictions, as follows:

If you define the IsTrue and IsFalse operators, they must both return the Boolean type.

If you define the << and >> operators, they must both specify the Integer type for the operandtype of operand2 .

The return type does not have to correspond to the type of either operand. For example, a comparison operator such as = or <> can return Boolean even if neither operand is Boolean .

Logical and Bitwise Operators

The And , Or , Not , and Xor operators can perform either logical or bitwise operations in Visual Basic. However, if you define one of these operators on a class or structure, you can define only its bitwise operation.

You cannot define the AndAlso operator directly with an Operator statement. However, you can use AndAlso if you have fulfilled the following conditions:

You have defined And on the same operand types you want to use for AndAlso .

Your definition of And returns the same type as the class or structure on which you have defined it.

You have defined the IsFalse operator on the class or structure on which you have defined And .

Similarly, you can use OrElse if you have defined Or on the same operands, with the return type of the class or structure, and you have defined IsTrue on the class or structure.

  • Widening and Narrowing Conversions

A widening conversion always succeeds at run time, while a narrowing conversion can fail at run time. For more information, see Widening and Narrowing Conversions .

If you declare a conversion procedure to be Widening , your procedure code must not generate any failures. This means the following:

It must always return a valid value of type type .

It must handle all possible exceptions and other error conditions.

It must handle any error returns from any procedures it calls.

If there is any possibility that a conversion procedure might not succeed, or that it might cause an unhandled exception, you must declare it to be Narrowing .

The following code example uses the Operator statement to define the outline of a structure that includes operator procedures for the And , Or , IsFalse , and IsTrue operators. And and Or each take two operands of type abc and return type abc . IsFalse and IsTrue each take a single operand of type abc and return Boolean . These definitions allow the calling code to use And , AndAlso , Or , and OrElse with operands of type abc .

  • IsFalse Operator
  • IsTrue Operator
  • Operator Procedures
  • How to: Define an Operator
  • How to: Define a Conversion Operator
  • How to: Call an Operator Procedure
  • How to: Use a Class that Defines Operators

Additional resources

IMAGES

  1. VB.NET operators

    assignment operator example in vb.net

  2. VB.NET Operators

    assignment operator example in vb.net

  3. VB.NET Operators

    assignment operator example in vb.net

  4. VB.NET Tutorial Part 4: Assignment Operators

    assignment operator example in vb.net

  5. සිංහලෙන්

    assignment operator example in vb.net

  6. Assignment operator

    assignment operator example in vb.net

VIDEO

  1. Vb.net tooltip control|properties,methods and events

  2. Arithmetic Operators in Visual Basic .net

  3. C++ Assignment Operators Practice coding

  4. Selenium

  5. Data types and Operators in C#

  6. Assignment Operators in C Programming

COMMENTS

  1. Assignment Operators

    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

  2. VB.Net

    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. +=. Add AND assignment operator, It adds right operand to the left operand and assigns the result to left operand.

  3. = Operator

    The element on the left side of the equal sign (=) can be a simple scalar variable, a property, or an element of an array. The variable or property cannot be ReadOnly. The = operator assigns the value on its right to the variable or property on its left. The = operator is also used as a comparison operator. For details, see Comparison Operators.

  4. .net

    2. First of all - you can't override assignment operator = (Why aren't assignment operators overloadable in VB.NET?). If you assign structure to new structure, you copy just value fields, reference fields will stay the same (thus your new MarbleCollection.marbles will point to same object as original MarbleCollection.marbles).

  5. &= Operator

    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 concatenates the String expression on its right to the String variable or property on its left, and assigns the result to the variable or property on its left.

  6. Assignment Operators

    3.10 Assignment Operators. Visual Basic .NET provides several assignment operators for abbreviating assignment statements. For example, the statement. value = value + 3. can be abbreviated with the addition assignment operator (+=) as. value += 3. The += operator adds the value of the right operand to the value of the left operand and stores ...

  7. Types of VB.Net Operators: Arithmetic, Comparison & Logical

    Here is an example of VB.Net arithmetic operator: Step 1) Create a new console application. To know this, visit our previous tutorial on Data Types and Variables. Step 2) Add the following code: Module Module1. Sub Main() Dim var_w As Integer = 11. Dim var_x As Integer = 5. Dim var_q As Integer = 2.

  8. PDF VB.Net

    An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. VB.Net is rich in built-in operators and provides following types of commonly used operators: Arithmetic Operators. Comparison Operators. Logical/Bitwise Operators. Bit Shift Operators. Assignment Operators.

  9. Assignment Operators

    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.

  10. VB.NET Operators

    Arithmetic Operators in VB.NET are used to perform numerical calculations. Below are all the Arithmetic operations supported by VB.NET. Sum of two numbers. Difference of two numbers. Product of two numbers. Division of two numbers, returns float value. Division of two numbers, returns integer value. (Decimal part removed)

  11. VB.Net

    VB.Net - Operators

  12. += Operator

    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.

  13. VB.NET Operators

    In VB.NET, operator is a special symbol that tells the compiler to perform the specific logical or mathematical operation on the data values. The data value itself (which can be either a variable or a constant) is called an operand, and the Operator performs various operations on the operand. The symbol + and - are the Operators, and the 3, 2 ...

  14. -= Operator

    The - Operator (Visual Basic) 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.

  15. VB.net Operators

    Assignment Operators in VB.net Description Example = It is a simple assignment Operator used to assign a right-side operand or value to a left side operand. X = 5, X assign a value 5 X = P + Q, (P + Q) variables or value assign to X. += An Add AND assignment Operator is used to add the value of the right operand to the left operand.

  16. VB.NET Operators

    These are some of the types of VB.NET Operators: For example: x = 2 + 3. Here, = and + are the operators and x, 2, 3 are the operands. The operator is working on some things, those things are known as an operand. ... Assignment operators are used for assigning values to variables in VB.NET.

  17. Can I define an assignment operator in vb.net Structure?

    End Operator. End Structure. UPDATE: For your proposed Int4 type I strongly suggest that you make it a Narrowing operator instead. This forces the user of your code to cast explicitly, which is a visual hint that the assignment might fail at runtime, e.g. Dim x As Int4 = CType(&HF, Int4) ' should succeed. Dim y As Int4 = CType(&HFF, Int4 ...

  18. Operator Statement

    The following code example uses the Operator statement to define the outline of a structure that includes operator procedures for the And, Or, IsFalse, and IsTrue operators. And and Or each take two operands of type abc and return type abc. IsFalse and IsTrue each take a single operand of type abc and return Boolean.

  19. VB.NET: Combining assignments (operator syntax)

    The context doesn't change anything in VB.NET terms. The C# code assigns to a value and returns the value on the same line and that is not done in VB.NET.And unless you're very ingrained in the C-is concepts, you may not know that the code fragment (Region.allByLanguage[key] = GeoController.RegionsByCountryAndLanguage(country, language)) actually returns a value.