Appendix C. Operators

The Visual Basic operators fall into five main categories: arithmetic, concatenation, comparison, logical, and bitwise. The following sections explain these categories and the operators they contain. The end of this appendix describes special Date and TimeSpan operators, as well as operator overloading.

ARITHMETIC OPERATORS

The following table lists the arithmetic operators provided by Visual Basic.

OPERATOR

PURPOSE

EXAMPLE

RESULT

^

Exponentiation

2 ^ 3

(2 to the power 3) = 2 * 2 * 2 = 8.

Negation

−2

−2

*

Multiplication

2 * 3

6

/

Division

3 / 2

1.5

Integer division

175

3

Mod

Modulus

17 Mod 5

2

+

Addition

2 + 3

5

Subtraction

3 − 2

1

<<

Bit left shift

&H57 << 1

&HAE

>>

Bit right shift

&H57 >> 1

&H2B

The bit shift operators deserve a little extra discussion. These operators shift the binary representation of a number by a given number of bits either left or right. Unfortunately, Visual Basic doesn't understand binary so you must manually translate between binary and decimal, octal, or hexadecimal.

For example, the hexadecimal value &H57 is 01010111 in binary. If you shift this one bit to the left, you get 10101110, which is &HAE in hexadecimal. If you shift the original value one bit to the right, you get 00101011, which is &H2B in hexadecimal.

When working with binary values, many developers prefer to work in hexadecimal because each hexadecimal digit corresponds to four binary bits so you can work with each group of four bits separately.

CONCATENATION OPERATORS

Visual Basic provides two concatenation operators: + and &. Both join two strings together. Because the + symbol also represents an arithmetic operator, your code will be easier to read if you use the & symbol for concatenation.

COMPARISON OPERATORS

The following table lists the comparison operators provided by Visual Basic.

OPERATOR

PURPOSE

EXAMPLE

RESULT

=

Equals

A = B

True if A equals B

<>

Not equals

A <> B

True if A does not equal B

<

Less than

A < B

True if A is less than B

<=

Less than or equal to

A <= B

True if A is less than or equal to B

>

Greater than

A > B

True if A is greater than B

>=

Greater than or equal to

A >= B

True if A is greater than or equal to B

Is

Equality of two objects

emp Is mgr

True if emp and mgr refer to the same object

IsNot

Inequality of two objects

emp IsNot mgr

True if emp and mgr refer to different objects

TypeOf ... Is ...

Object is of a certain type

TypeOf obj Is Manager

True if obj points to a Manager object

Like

Matches a text pattern

value Like "###-####"

True if value contains three digits, a dash, and four digits

The following table lists characters that have special meanings to the Like operator.

CHARACTER(S)

MEANING

?

Matches any single character

*

Matches any zero or more characters

#

Matches any single digit

[characters]

Matches any of the characters between the brackets

[!characters]

Matches any character not between the brackets

A-Z

When inside brackets, matches any character in the range A to Z

The following table lists some useful Like patterns.

PATTERN

MEANING

[2-9]##-####

Seven-digit U.S. phone number

[2-9]##-[2-9]##-####

Ten-digit U.S. phone number including area code

1-[2-9]##-[2-9]##-####

Eleven-digit U.S. phone number beginning with 1 and area code

#####

Five-digit U.S. ZIP code

#####-####

Nine-digit U.S. ZIP+4 code

?*@?*.?*

e-mail address

[A-Z][0-9][A-Z][0-9][A-Z][0-9]

Canadian postal code

LOGICAL OPERATORS

The following table summarizes the Visual Basic logical operators.

OPERATOR

PURPOSE

EXAMPLE

RESULT

Not

Logical or bitwise negation

Not A

True if A is false

And

Logical or bitwise And

A And B

True if A and B are both true

Or

Logical or bitwise Or

A Or B

True if A or B or both are true

Xor

Logical or bitwise exclusive Or

A Xor B

True if A or B but not both is true

AndAlso

Logical or bitwise And with short-circuit evaluation

A AndAlso B

True if A and B are both true

OrElse

Logical or bitwise Or with short-circuit evaluation

A OrElse B

True if A or B or both are true

BITWISE OPERATORS

Bitwise operators work much as logical operators do, except that they compare values one bit at a time. Visual Basic provides bitwise versions of Not, And, Or, and Xor but not bitwise versions of AndAlso or OrElse.

OPERATOR PRECEDENCE

The following table lists the operators in order of precedence. When evaluating an expression, the program evaluates an operator before it evaluates those lower than it in the list. When operators are on the same line, the program evaluates them from left to right.

OPERATOR

DESCRIPTION

^

Exponentiation

-

Negation

*, /

Multiplication and division

Integer division

Mod

Modulus

+, -, +

Addition, subtraction, and concatenation

&

Concatenation

<<, >>

Bit shift

=, <>, <, <=, >, >=, Like, Is, IsNot, TypeOf ... Is ...

All comparisons

Not

Logical and bitwise negation

And, AndAlso

Logical and bitwise And with and without short-circuit evaluation

Xor, Or, OrElse

Logical and bitwise Xor, and Or with and without short-circuit evaluation

Use parentheses to change the order of evaluation and to make expressions easier to read.

ASSIGNMENT OPERATORS

The following table summarizes the Visual Basic assignment operators.

OPERATOR

EXAMPLE

ORIGINAL SYNTAX EQUIVALENT

=

A = B

A = B

^=

A ^= B

A = A ^ B

*=

A *= B

A = A * B

/=

A /= B

A = A / B

=

A = B

A = A B

+=

A += B

A = A + B

-=

A -= B

A = A - B

&=

A &= B

A = A & B

<<=

A <<= B

A = A << B

>>=

A >> B

A = A >> B

There are no assignment operators corresponding to Mod or the Boolean operators.

CHOOSE, IF, AND IIF

The Choose, If, and IIf statements return values that you can assign to a variable. These statements are not really assignment operators (you need to use = to assign their results to a variable) and they perform decisions so they are described in Appendix E, "Control Statements."

DATE AND TIMESPAN OPERATORS

The Date and TimeSpan data types are related through their operators. The following list shows the relationships between these two data types:

  • Date - Date = TimeSpan

  • Date + TimeSpan = Date

  • TimeSpan + TimeSpan = TimeSpan

  • TimeSpan - TimeSpan = TimeSpan

The following table lists convenient methods provided by the Date data type.

SYNTAX

MEANING

result_date = date1.Add(timespan1)

Returns date1 plus timespan1

result_date = date1 .AddYears(num_years)

Returns the date plus the indicated number of years

result_date = date1 .AddMonths(num_months)

Returns the date plus the indicated number of months

result_date = date1.AddDays(num_days)

Returns the date plus the indicated number of days

result_date = date1 .AddHours(num_hours)

Returns the date plus the indicated number of hours

result_date = date1 .AddMinutes(num_minutes)

Returns the date plus the indicated number of minutes

result_date = date1 .AddSeconds(num_seconds)

Returns the date plus the indicated number of seconds

result_date = date1.AddMilliseconds (num_milliseconds)

Returns the date plus the indicated number of milliseconds

result_date = date1 .AddTicks(num_ticks)

Returns the date plus the indicated number of ticks (100 nanosecond units)

result_timespan = date1 .Subtract(date2)

Returns the time span between date2 and date1

result_integer = date1 .CompareTo(date2)

Returns a value indicating whether date1 is greater than, less than, or equal to date2

result_boolean = date1.Equals(date2)

Returns True if date1 equals date2

OPERATOR OVERLOADING

The syntax for defining an operator for a class is as follows:

[ <attributes> ] Public [ Overloads ] Shared [ Shadows ] _
[ Widening | Narrowing ] Operator symbol ( operands ) As type
    ...
End Operator

The operator's symbol can be +, -, *, /, , ^, &, <<, >>, =, <>, <, <, <=, >=, Mod, Not, And, Or, Xor, Like, IsTrue, IsFalse, or CType.

For example, the following code defines the + operator for the ComplexNumber class. This class has two public properties, Re and Im, that give the number's real and imaginary parts.

Public Shared Operator +(
 ByVal c1 As ComplexNumber,
 ByVal c2 As ComplexNumber) As ComplexNumber
    Return New ComplexNumber With {
        .Re = c1.Re + c2.Re,
        .Im = c1.Im + c2.Im}
End Operator

Some operands come in pairs, and if you define one, you must define the other. The pairs are = and <>, < and >, <= and >=, and IsTrue and IsFalse.

If you define And and IsFalse, Visual Basic uses them to define the AndAlso operator. Similarly, if you define Or and IsTrue, Visual Basic automatically provides the OrElse operator.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.218.37