Operators


Visual Basic includes a variety of operators that let you manipulate the values of your variables. You've already seen the assignment operator (=), which lets you assign a value directly to a variable. Most of the other operators let you build up expressions that combine multiple original values in formulaic ways for eventual assignment to a variable. Consider the following statement:

squareArea = length * width 


This statement includes two operators: assignment and multiplication. The multiplication operator combines two values (length and width) using multiplication, and stores the product in the squareArea variable. Without operators, you would be hard pressed to calculate an area or any complex formula.

There are two types of non-assignment operators: unary and binary. Unary operators work with only a single value, or operand. Binary operators require two operands, but result in a single processed value. Operands include literals, constants, variables, and function return values. Table 6-7 lists the different operators with usage details.

Table 6-7. Visual Basic Non-Assignment Operators

Operator

Description

+

Addition. Adds two operands together, producing a sum. Some programmers also use this operator to perform string concatenation, but it's better to join strings using another operator (&) specifically designed for that purpose.

Syntax: operand1 + operand2

Example: 2 + 3

+

Unary plus. Ensures that an operand retains its current sign, either positive or negative. Because all operands automatically retain their sign, this operator is redundant, and removed from code automatically by Visual Studio. Normally this operator is meaningless in your code, but it may come in handy when we discuss "operator overloading" in Chapter 12, "Operating Overloading."

Syntax: +operand

Example: +5

-

Subtraction. Subtracts one operand (the second) from another (the first), and returns the difference.

Syntax: operand1 operand2

Example: 10 4

-

Unary negation. Reverses the sign of its operand. When used with a literal number, it results in a negative value. When used with a variable that contains a negative value, it produces a positive result.

Syntax: operand2

Example: 34

*

Multiplication. Multiplies two operands together, and returns the product.

Syntax: operand1 * operand2

Example: 8 * 3

/

Division. Divides one operand (the first) by another (the second), and returns the quotient. If the second operand contains zero, a divide-by-zero error occurs.

Syntax: operand1 / operand2

Example: 9 / 3

\

Integer division. Divides one operand (the first) by another (the second), and returns the quotient, first truncating any decimal portion from that result. If the second operand contains zero, a divide-by-zero error occurs.

Syntax: operand1 \ operand2

Example: 9 \ 4

Mod

Modulo. Divides one operand (the first) by another (the second), and returns the remainder as an integer value. If the second operand contains zero, a divide-by-zero error occurs.

Syntax: operand1 Mod operand2

Example: 10 Mod 3

^

Exponentiation. Raises one operand (the first) to the power of another (the second).

Syntax: operand1 ^ operand2

Example: 2 ^ 8

&

String concatenation. Joins two operands together, and returns a combined string result. Both operands are converted to their String equivalent before being joined together.

Syntax: operand1 & operand2

Example: "O" & "K"

And

Conjunction. Performs a logical or bitwise conjunction on two operands, and returns the result. For logical (Boolean) operations, the result will be True only if both operands evaluate to True. For bitwise (integer) operations, each specific bit in the result will be set to 1 only if the corresponding bits in both operands are 1.

Syntax: operand1 And operand2

Example: this And that

Or

Disjunction. Performs a logical or bitwise disjunction on two operands, and returns the result. For logical (Boolean) operations, the result will be True if either operand evaluates to True. For bitwise (integer) operations, each specific bit in the result will be set to 1 if the corresponding bit in either operand is 1.

Syntax: operand1 Or operand2

Example: this Or that

AndAlso

Short-circuited conjunction. This operator is equivalent to the logical version of the And operator, but if the first operand evaluates to False, the second operand will not be evaluated at all. This operator does not support bitwise operations.

Syntax: operand1 AndAlso operand2

Example: this AndAlso that

OrElse

Short-circuited disjunction. This operator is equivalent to the logical version of the Or operator, but if the first operand evaluates to True, the second operand will not be evaluated at all. This operator does not support bitwise operations.

Syntax: operand1 OrElse operand2

Example: this OrElse that

Not

Negation. Performs a logical or bitwise negation on a single operand. For logical (Boolean) operations, the result will be True if the operand evaluates to False, and False if the operand evaluates to True. For bitwise (integer) operations, each specific bit in the result will be set to 1 if the corresponding operand bit is 0, and set to 0 if the operand bit is 1.

Syntax: Not operand1

Example: Not this

Xor

Exclusion. Performs a logical or bitwise "exclusive or" on two operands, and returns the result. For logical (Boolean) operations, the result will be True only if the operands have different logical values (True or False). For bitwise (integer) operations, each specific bit in the result will be set to 1 only if the corresponding bits in the operands are different.

Syntax: operand1 Xor operand2

Example: this Xor that

<<

Shift left. The Shift Left operator shifts the bits of the first operand to the left by the number of positions specified in the second operand, and returns the result. Bits pushed off the left end of the result are lost; bits added to the right end are always 0. This operator works best if the first operand is an unsigned integer value.

Syntax: operand1 << operand2

Example: &H25 << 3

>>

Shift right. The Shift Right operator shifts the bits of the first operand to the right by the number of positions specified in the second operand, and returns the result. Bits pushed off the right end of the result are lost; bits added to the right end are always the same as the bit originally in the left-most position. This operator works best if the first operand is an unsigned integer value.

Syntax: operand1 >> operand2

Example: &H25 >> 2

=

Equals (comparison). Compares two operands and returns True if they are equal in value.

Syntax: operand1 = operand2

Example: one = two

<>

Not equals. Compares two operands and returns True if they are not equal in value.

Syntax: operand1 <> operand2

Example: one <> two

<

Less than. Compares two operands and returns True if the first is less in value than the second. When comparing string values, the return is True if the first operand appears first when sorting the two strings.

Syntax: operand1 < operand2

Example: one < two

>

Greater than. Compares two operands and returns True if the first is greater in value than the second. When comparing string values, the return is True if the first operand appears last when sorting the two strings.

Syntax: operand1 > operand2

Example: one > two

<=

Less than or equal to. Compares two operands and returns True if the first is less than or equal to the value of the second.

Syntax: operand1 <= operand2

Example: one <= two

>=

Greater than or equal to. Compares two operands and returns True if the first is greater than or equal to the value of the second.

Syntax: operand1 >= operand2

Example: one >= two

Like

Pattern comparison. Compares the first operand to the pattern specified in the second operand, and returns True if there is a match. The pattern operand supports some basic wildcard and selection options, and is fully described in the documentation supplied with Visual Studio. .NET also includes a feature called Regular Expressions that provides a much more comprehensive pattern matching solution.

Syntax: operand1 Like operand2

Example: one Like pattern

Is

Type comparison. Compares the first operand to another object, a data type, or Nothing, and returns True if there is a match. I will document this operator in more detail later in the text, and in Chapter 8.

Syntax: operand1 Is operand2

Example: one Is two

IsNot

Negated type comparison. This operator is a shortcut for using the Is and Not operators together. The following two expressions are equivalent:

this IsNot that Not (this Is that) 


Syntax: operand1 IsNot operand2

Example: one IsNot two

TypeOf

Instance comparison. Returns the data type of a value or variable. The type of every class or data type in .NET is implemented as an object, based on System.Type. The TypeOf operator can be used only with the Is operator:

Syntax: TypeOf operand1 Is typeOperand

Example: TypeOf one Is Integer

AddressOf

Delegate retrieval. Returns a delegate (described in Chapter 8) that represents a specific instance of a procedure or method.

Syntax: AddressOf method1

Example: AddressOf one.SomeMethod

GetType

Type retrieval. Returns the data type of a value or variable, just like the TypeOf operator. However, GetType works like a function, and does not need to be used with the Is operator.

Syntax: GetType(operand1)

Example: GetType(one)


Non-assignment operators use their operands to produce a result, but they do not cause the operands themselves to be altered in any way. The assignment operator does update the operand that appears on its left side. In addition to the standard assignment operator, Visual Basic includes several operators that combine the assignment operator with some of the binary operators. Table 6-8 lists these assignment operators.

Table 6-8. Visual Basic Assignment Operators

Operator

Based On

=

Standard assignment operator

+=

+ (Addition)

-=

- (Subtraction)

*=

* (Multiplication)

/=

/ (Division)

\=

\ (Integer Division)

^=

^ (Exponentiation)

&=

& (Concatenation)

<<=

<< (Shift Left)

>>=

>> (Shift Right)


These assignment operators are just shortcuts for the full-bodied operators. For instance, to add one to a numeric variable, you can use either of these two statements.

' ----- Increment totalSoFar by 1. totalSoFar = totalSoFar + 1 ' ----- Another way to increment totalSoFar by 1. totalSoFar += 1 





Start-to-Finish Visual Basic 2005. Learn Visual Basic 2005 as You Design and Develop a Complete Application
Start-to-Finish Visual Basic 2005: Learn Visual Basic 2005 as You Design and Develop a Complete Application
ISBN: 0321398009
EAN: 2147483647
Year: 2006
Pages: 247
Authors: Tim Patrick

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