Section 5.4. Assignment Operators


5.4. Assignment Operators

Along with the standard assignment operator (=), many other operators can be turned into assignment operators by simply appending an equals sign to the right of the operator. These converted operators all have the same form:

     expression1 <operator>= expression2 

where <operator> is the operator being promoted to an assignment operator. This form is equivalent to:

     expression1 = expression1 <operator> expression2 

To illustrate, consider the addition assignment operator. The expression:

     x += 1 

is equivalent to:

     x = x + 1 

which simply adds 1 to the value of x. Similarly, the expression:

     s &= "end" 

is equivalent to:

     s = s & "end" 

which concatenates the string "end" to the end of the string s.

All of these "shortcut" assignment operators were introduced with Visual Basic .NET 2002.


= (Assignment)

The assignment operator assigns the value or reference of the expression on the right of the assignment operator to the variable on the left. For example, the following assigns y plus an additional value of 5 to x.

     x = y + 5 

The assignment operator alone is used to assign both values and references; in previous versions of VB, the Set statement had to be used along with the assignment operator to assign an object reference. The Set keyword is no longer used in this context. Also, the previously optional Let keyword is no longer part of the Visual Basic language.


+=

The addition assignment operator. As an example:

     totalValue += 1 

adds 1 to the value of totalValue and assigns the result to totalValue.


-=

The subtraction assignment operator. As an example:

     totalValue -= 1 

subtracts 1 from the value of totalValue and assigns the result to totalValue.


*=

The multiplication assignment operator. As an example:

     totalValue *= 3 

multiplies the value of totalValue by 3 and assigns the result to totalValue.


/=

The division assignment operator. As an example:

     totalValue /= 2 

divides the value of totalValue by 2 and assigns the result to totalValue. If the value to the right of the division assignment operator equates to 0, an error occurs.


\=

The integer division assignment operator. As an example:

     totalValue \= 2 

divides the value of totalValue by 2, discards any fractional part, and assigns the result to totalValue. If the value to the right of the integer division assignment operator equates to 0, an error occurs.


^=

The exponentiation assignment operator. As an example:

     totalValue ^= 2 

squares the value of totalValue and assigns the result to totalValue.


&=

The concatenation assignment operator. As an example:

     storyText &= "The End" 

appends a literal text string to the end of storyText's existing content and assigns this new concatenated string to storyText.


<<=

New in 2003. The shift left assignment operator. As an example:

     dataMask <<= 2 

shifts the bits of dataMask left two positions and assigns the new value back to dataMask.


>>=

New in 2003. The shift right assignment operator. As an example:

     dataMask >>= 2 

shifts the bits of dataMask right two positions and assigns the new value back to dataMask.

Unlike the comparison operators , in which the order of symbols is reversible (that is, >= is the same as =>), the order of the "shortcut" assignment operator symbols is not reversible. For example, while:

     x -= 1 

decrements x by 1, the expression:

     x =- 1 

assigns a value of 1 to the variable x. That is, it really looks like this:

     x = -1 





Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net