Section C.2. Assignment Operators

   

C.2 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:

   expression1 <operator>= expression2   

where <operator> is one of the arithmetic or concatenation operators. This 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 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 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 

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:

 lNumber += 1 

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

-=

Subtraction assignment operator. For example:

 lNumber -= 1 

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

^=

Exponential assignment operator. For example:

 lNumber ^= 2 

squares lNumber and assigns the result to lNumber.

*=

Multiplication assignment operator. For example:

 lNumber *= 3 

triples lNumber and assigns the result to lNumber.

/=

Division assignment operator. For example:

 lNumber /= 2 

halves lNumber and assigns the result to lNumber.

\=

Integer division assignment operator. For example:

 dblNumber \= 2 

divides dblNumber by 2, discards any fractional part, and assigns the result to dblNumber.

&=

Concatenation assignment operator. For example:

 strVal &= "." 

appends a period to the end of strVal.

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

 x += 1 

increments x by 1, the expression:

 x =+ 1 

simply assigns 1 to the variable x.

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

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