Unary Operators Arithmetic Operators

46 b.MyPublicMember Test assignment to reference type. c = New SomeClass c.MyPublicMember = To be copied to d d = c c.MyPublicMember = New value for c Console.WriteLineThe value of d.MyPublicMember is _ d.MyPublicMember End Sub End Class The output of the TestValueAndReferenceAssignment method in Ex am ple 2- 3 is: The value of b.MyPublicMember is To be copied to b The value of d.MyPublicMember is New value for c In Ex am ple 2- 3 , the SomeStructure structure and the SomeClass class have identical definitions, except that one is a structure and the other is a class. This leads to very different behavior during assignment. When a value type is copied, the actual value is copied. When a reference type is copied, only the reference is copied, resulting in two references to the same value. If the value is subsequently changed through one of the references, the new value is also seen through the other reference. This difference is shown in the output from Ex am ple 2- 3 . The value type in variable a is copied to variable b . The value of a.MyPublicMember is then modified. Subsequently, the call to Console.WriteLine shows that this modification does not affect b.MyPublicMember . In contrast, the assignment of c to d copies only a reference, which means that after the assignment, both c and d reference the same object. The value of c.MyPublicMember is then modified. The subsequent call to Console.WriteLine shows that this modification did affect d.MyPublicMember . Indeed, d.MyPublicMember refers to the same memory as c.MyPublicMember .

2.12 Operators and Expressions

Operators are symbols characters or keywords that specify operations to be performed on one or two operands or arguments. Operators that take one operand are called unary operators. Operators that take two operands are called binary operators. Unary operators use prefix notation, meaning that the operator precedes the operand e.g., -5 . Binary operators except for one case use infix notation, meaning that the operator is between the operands e.g., 1 + 2 . The TypeOf...Is operator is a binary operator that uses a special form that is neither prefix nor infix notation.

2.12.1 Unary Operators

Visual Basic supports the following unary operators: + unary plus The unary plus operator takes any numeric operand. Its not of much practical use because the value of the operation is equal to the value of the operand. - unary minus The unary minus operator takes any numeric operand except as noted later. The value of the operation is the negative of the value of the operand. In other words, the result is calculated by subtracting the operand from zero. If the operand type is Short, Integer, or Long, and the value 47 of the operand is the maximum negative value for that type, then applying the unary minus operator will cause a System.OverflowException error, as in the following code fragment: Dim sh As Short = -32768 Dim i As Integer = -sh Not logical negation The logical negation operator takes a Boolean operand. The result is the logical negation of the operand. That is, if the operand is False , the result of the operation is True , and vice versa. AddressOf The AddressOf operator returns a reference to a method. Two different kinds of references can be obtained, depending on the context in which the operator is used: • When the AddressOf operator is used within the argument list of a call to a method, which is made available via the Declare statement, it returns a function pointer that is suitable for such calls. • When the AddressOf operator is used in any other context, a delegate object is returned. See Sect ion 2.19 later in this chapter for information.

2.12.2 Arithmetic Operators

The arithmetic operators perform the standard arithmetic operations on numeric values. The arithmetic operators supported by Visual Basic .NET are: multiplication The multiplication operator is defined for all numeric operands. The result is the product of the operands. regular division The regular division operator is defined for all numeric operands. The result is the value of the first operand divided by the second operand. \ integer division The integer division operator is defined for integer operands Byte, Short, Integer, and Long. The result is the value of the first operand divided by the second operand, then rounded to the integer nearest to zero. Mod modulo The modulo operator is defined for integer operands Byte, Short, Integer, and Long. The result is the remainder after the integer division of the operands. exponentiation The exponentiation operator is defined for operands of type Double. Operands of other numeric types are converted to type Double before the result is calculated. The result is the value of the first operand raised to the power of the second operand. + addition 48 The addition operator is defined for all numeric operands and operands of an enumerated type. The result is the sum of the operands. For enumerated types, the sum is calculated on the underlying type, but the return type is the enumerated type. See the discussion of enumerated types in the Enumerations section later in this chapter for more information on the types that can underlie an enumerated type. See also Sect ion 2.12.4 later in this section. - subtraction The subtraction operator is defined for all numeric operands and operands of an enumerated type. The result is the value of the first operand minus the second operand. For enumerated types, the subtraction is calculated on the underlying type, but the return type is the enumerated type. See the discussion of enumerated types in Sect ion 2.17 later in this chapter for more information on the types that can underlie an enumerated type.

2.12.3 Relational Operators