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.

image

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.

image

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.

image

image

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 in the table, the program evaluates them from left to right as they appear in the expression.

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 LONG 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 examples demonstrating 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:

OPERATOR SYMBOLS
+
-
*
/

^
&
<<
>>
=
<>
<
>
<=
>=
Mod
Not
And
Or
Xor
Like
IsTrue
IsFalse
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 +(c1 As ComplexNumber, 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.224.54.13