6 Visual Basic 2010  –  A Quick Look At  Operators

In a previous chapter we programmed the Simple Calculator Form with this code:

Dim dNumber1 As Decimal

Dim dNumber2 As Decimal

Dim dNumberResult As Decimal

dNumber1 = CDec(TextBox1.Text)

dNumber2 = CDec(TextBox2.Text)

If RadioButton1.Checked = True Then dNumberResult = dNumber1 + dNumber2

If RadioButton2.Checked = True Then dNumberResult = dNumber1 – dNumber2

If RadioButton3.Checked = True Then dNumberResult = dNumber1 * dNumber2

If RadioButton4.Checked = True Then dNumberResult = dNumber1 / dNumber2

TextBox3.Text = CStr(dNumberResult)

In this chapter we are going to continue to explain what we did.  This little piece of code declared three variables and assigned them values using some If…Then logic statements and operators along the way.  In the last chapter we looked at variables, and using conversion functions.  In this installment were going out take a quick look at operators.

## Operators

Numerous symbols are used in Visual Basic 2010 to perform operations like adding and subtracting numbers, or concatenating strings, and so forth.  In our programming example we use the plus, minus, times, division operators which are +, -, *, and /.

The following chart both lists the operators and their order of precedence.    Order of precedence is the important concept because the operating system does not just read left to right in performing operations in statements. Rather it follows this predefined order of precedence that does things like calculate anything in a set of parentheses first, and so forth.   I use the example of parentheses because parentheses are used as a tool to isolate items that need to be calculated before other things in an expression are calculated. For example look at the following expression:

X = 2 + 4 / 5

If you just read left to right, you would get a value of 1.2.  If you follow the order of precedence, though, you would know that the division happens before the addition, and you would get a value of 2.8. The way to control this so that the addition happens first is to put the 2+4 in parentheses as in the following example.

X = (2 + 4) / 5

 Operator Function ^ Exponentiation +, – Unary Identity and Negation – Negation *, / Multiplication and Division \ Integer Division Mod Modulus (remainder) +, – Addition and Subtraction & String Concatenation

We use the “+” operator in this line of code:

IF RadioButton1.Checked = True Then dNumberResult = dNumber1 + dNumber2

Logical Operators

 And Both sides must be true in order for the expression to value to true. Not Returns the opposite value of the expression. For example, if the expression is true, it returns false. Or If either side is true then the expression evaluates to true, otherwise it is false. (If both sides are true then it is still true.) Xor If only one side is true then the expression evaluates to true. If both sides are either true or false then the expression is false.

In the previous lesson we “nested” two if statements to avoid dividing by zero.  We are going to change that to one using the “And” operator.  Here is what it looks like:

If (RadioButton4.Checked = True) And (dNumber2 = 0) Then

MessageBox.Show(“You are attempting to divide by zero.  Try another number.”, “Error”, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

Exit Sub

Else

dNumberResult = dNumber1 / dNumber2

End If

Save all files, build the program, and run it. Try dividing by zero. You should get the error message you entered, just like you did on the previous lesson.

The MSDN section on operators starts at http://msdn.microsoft.com/en-us/library/a1w3te48%28v=VS.100%29.aspx