Understanding Operators
Operators assist you in manipulating data by assigning variable values, retrieving variable values, or comparing variable values. An operator falls into one of the following categories: arithmetic, assignment, bitwise, comparison, concatenation, or logical.
Each time you declare a variable, you're assigning the variable to a value, so you're using an assignment operator. When you compare the value of one variable to the value of another variable, you're using a comparison operator. When you add numbers together, you're using an arithmetic operator. You used operators often last week, and most operators are self-explanatory in what purpose they actually have. If you're coming from a Visual Basic 6 background, you'll be pleasantly surprised by some of the new operators available to you in Visual Basic .NET, so make sure that you read through the following section.
Arithmetic Operators
In grade school, we all learned how to add, subtract, multiply, and divide. These are basic life functions, just like watching Star Trek and eating pizza. Visual Basic .NET offers the arithmetic operators listed in Table 8.2 that handle the dirty work of doing math.
Table 8.2. Arithmetic/Multiplicative Operators in Visual Basic .NET and C#Visual Basic .NET | C# | Description |
---|
+ | + | Addition |
- | - | Subtraction |
* | * | Multiplication |
/ | / | Division |
| N/A | Integer division |
Mod | % | Modulo |
^ | N/A | Exponentiation |
N/A | ++ | Unary addition |
N/A | -- | Unary subtraction |
Each of the operators for performing mathematical functions is straightforward. The one difference between the languages is the way Visual Basic .NET handles division. The integer division operator doesn't return a remainder if one exists when dividing two numbers; the regular / division operator does. Listing 8.3 uses each of the arithmetic operators and returns values from the test variables in the procedure.
Listing 8.3. Using Arithmetic Operators to Test Values
Private Sub testOperators()
Dim X As Integer = 50
Dim Y As Integer = 10
' Addition - returns 60
MessageBox.Show((X + Y).ToString())
' Subtraction - returns 40
MessageBox.Show((X - Y).ToString())
' Multiplication - returns 500
MessageBox.Show((X * Y).ToString())
' Division - returns 5
MessageBox.Show((X / Y).ToString())
' Modulo - returns 0
MessageBox.Show((X Mod Y).ToString())
' Modulo - returns 10
MessageBox.Show((Y Mod X).ToString())
' Exponent - returns 9.76562E+16
MessageBox.Show((X ^ Y).ToString())
End Sub
private void testOperators()
{
int X = 50;
int Y = 10;
// Addition - returns 60
MessageBox.Show((X + Y).ToString());
// Subtraction - returns 40
MessageBox.Show((X - Y).ToString());
// Multiplication - returns 500
MessageBox.Show((X * Y).ToString());
// Division - returns 5
MessageBox.Show((X / Y).ToString());
// Modulo - returns 0
MessageBox.Show((X % Y).ToString());
}
|
Assignment Operators
Assignment operators take a value from the right side of the operator and assign it to the value on the left side of the operator. In previous Visual Basic versions, the = sign was the assignment operator, but the language has been enhanced to include some pretty cool new assignment operators. Table 8.3 lists the assignment operators in Visual Basic .NET and C# and their descriptions.
Table 8.3. Assignment Operators in Visual Basic .NET and C#Visual Basic .NET | C# | Description |
---|
= | = | Equals assignment |
+= | += | Addition/concatenation assignment |
-= | -= | Subtraction assignment |
*= | *= | Multiplication assignment |
/= | /= | Division assignment |
= | /= | Integer division |
^= | N/A | Exponentiation assignment |
&= | += | String concatenation assignment |
N/A | %= | Modulus assignment |
N/A | <<= | Left shift assignment |
N/A | >>= | Right shift assignment |
N/A | &= | Bitwise AND assignment |
N/A | ^= | Bitwise exclusive OR assignment |
N/A | |= | Bitwise inclusive OR assignment |
To see some of the assignment operators in action, Listing 8.4 demonstrates the more commonly used assignment operators. The usage of the Visual Basic .NET assignment operators should be pretty exciting if you are coming from a Visual Basic 6 background.
Listing 8.4. Using Assignment Operators to Test Values
Private Sub testOperators()
Dim X As Integer = 50
Dim Y As Integer = 10
Dim z As Integer
' Addition - returns 60
X += Y
MessageBox.Show(X)
' Subtraction - returns 50
X -= Y
MessageBox.Show(X)
' Multiplication - returns 500
X *= Y
MessageBox.Show(X)
' Division - returns 50
X /= Y
MessageBox.Show(X)
' Assignment - returns FALSE
MessageBox.Show((X = Y).ToString())
End Sub
private void testOperators()
{
int X = 50;
int Y = 10;
// Addition - returns 60
MessageBox.Show((X += Y).ToString());
// Subtraction - returns 50
MessageBox.Show((X -= Y).ToString());
// Multiplication - returns 500
MessageBox.Show((X *= Y).ToString());
// Division - returns 50
MessageBox.Show((X /= Y).ToString());
// Assignment - returns 10
MessageBox.Show((X = Y).ToString());
}
|
You'll notice that the Visual Basic .NET code is slightly different from the C# code in the way the operator is handled. In Visual Basic .NET, you must explicitly use the right side of the operator in an expression, so placing all the code in the single MessageBox statement doesn't work.
Comparison Operators
Comparison operators evaluate the expression on the right side of the equal sign and return a Boolean true or false based on the comparison of the expressions. Comparison operators also can be grouped in the relational and equality operators group. In all cases, a Boolean true or false is returned to the expression you're attempting to evaluate. Table 8.4 lists the comparison operators in Visual Basic .NET and C# and their descriptions.
Table 8.4. Comparison Operators in Visual Basic .NET and C#Visual Basic .NET | C# | Description |
---|
< | < | Less than |
<= | <= | Less than or equal to |
> | > | Greater than |
>= | >= | Greater than or equal to |
= | == | Equal to |
<> | != | Not equal to |
Is | == | Compare two objects |
TypeOf | is | Compare object reference types |
= | == | Compare two strings |
& | + | Concatenate Strings |
AndAlso | && | Short-circuited Boolean AND |
OrElse | || | Short-circuited Boolean OR |
And | && | Logical AND |
Or | || | Logical OR |
Not | ! | Logical NOT |
To see a few of the operators in action, read Listing 8.5 and notice the inline comments that explain why the MessageBox prompts will or will not display.
Listing 8.5. Using Comparison Operators to Test Variable Values
Private Sub testOperators()
Dim X As Integer = 50
Dim Y As Integer = 10
If X < Y Then
' This never shows, X is NOT less then Y
MessageBox.Show("X greater than Y")
End If
Dim s1 As String = "Bob"
Dim s2 As String = "bob"
If s1 = s2 Then
' This never shows, Bob does not equal bob
MessageBox.Show("s1 = s2")
End If
If s1 = "Bob" OrElse s2 = "Bob" Then
' This shows, s1 or s2 = Bob
MessageBox.Show("s1 or s2 = Bob")
End If
End Sub
private void testOperators()
{
int X = 50;
int Y = 10;
if (X < Y)
{
// This never shows, X is NOT less then Y
MessageBox.Show("X greater than Y");
}
string s1 = "Bob";
string s2 = "bob";
if (s1 == s2)
{
// This never shows, Bob does not equal bob
MessageBox.Show("s1 = s2");
}
if (s1 == "Bob" || s2 == "Bob")
{
// This shows, s1 or s2 = Bob
MessageBox.Show("s1 or s2 = Bob");
}
}
|
You can see that when dealing with strings, checking the equality in C# uses the == operator, whereas Visual Basic .NET uses the = operator.