Examining Visual Basic s Operators


Examining Visual Basic's Operators

Simply having a variable with some value in it is not very interesting. Typically, we'd like to be able to perform some sort of operation on that variable, or on multiple variables in tandem. For example, in mathematics there are numbers and operators. Numbers are things like 4, 17.5, pi, and so on, whereas operators are things like negate, add, subtract, divide, and so on.

Many of the traditional mathematical operators are operators in Visual Basic. For example, to add together two numeric variables in Visual Basic, you use the + operator. To multiply two numeric variables, you use the * operator.

There are different classes of operators, the most important being arithmetic operators, comparison operators, the concatenation operator, and assignment operators. We'll examine these four classes of operators in the following four sections.

Arithmetic Operators

The four most-used arithmetic operators in Visual Basic are +, -, *, and /, which perform addition, subtraction, multiplication, and division, respectively. These operators are referred to as binary operators because they operate on two variables.

For example, to add together two integer variables and store the result in an integer variable, you could use the following code:

Dim a, b, c as Integer b = 15 c = 20 a = b + c 


Here, a will be assigned a value equal to the value of b plus the value of c, which is 35.

The - operator can be used both as a binary operator and as a unary operator. A unary operator is an operator that operates on just one variable. When the - operator is used as a unary operator, it performs the negation of a number. That is, if we had the code

Dim a, b, c as Integer b = 15 c = 20 a = -(b + c) 


a would be assigned the value 35. Here, the - operator is used as a unary operator on the expression b + c, thereby negating the value returned by b + c.

By the Way

Note that parentheses can be used to determine the order of operations. If, in the preceding code snippet, instead of

a = -(b + c) 


we had used

a = -b + c 


a would have been assigned the value 5 because negation has precedence over addition. That is, when the expression -b + c is evaluated, b is negated first, and then its negated value is added to c. With -(b + c), first b and c are summed, and then the resulting sum is negated.

The arithmetic precedence rules in Visual Basic mirror the standard precedence rules of mathematics. If you ever need to alter the order of operations, simply use parentheses to group those expressions that should be evaluated first.


The / operator always returns a nonintegral numeric value, even if the resulting quotient does not have a remainder. That is, the value returned by 4 / 2 is the nonintegral numeric value 2.0, not the integer value 2. Of course the / operator can also have a quotient with a decimal remainder; for example, the value returned by 3 / 4 is 0.75.

Exploring the Comparison Operators

Comparison operators are binary operators that compare the value of two variables. The six comparison operators are listed in Table 5.1. Comparison operators always return a Boolean valuetrue or Falsedepending on the value of the two variables being compared.

Table 5.1. Visual Basic .NET's Comparison Operators

Operator

Description

<

Less than

<=

Less than or equal

>

Greater than

>=

Greater than or equal

=

Equal

<>

Not equal


The following statements evaluate to true:

4 < 8 3.14159 >= 2 "Bob" <> "Sue" (10/2) = (20/4) 4 <= 4 


The following statements evaluate to False:

7 > 100 "Bob" = "Frank" (10/2) = 7.5 4 < 4 


By the Way

Usually, comparison operators are used in control structures, which we'll look at in detail in the next hour.


Understanding the Concatenation Operator

The concatenation operator concatenates two string variables. Concatenating two strings produces a string that consists of the content of the first string with the content of the second string appended. The string concatenation operator in Visual Basic is the ampersand, &.

Let's look at a quick code snippet to see how the concatenation operator works. Consider the following code:

Dim firstWord as String = "ASP.NET" Dim secondWord as String = "is" Dim thirdWord as String = "neat" Dim sentence as String sentence = firstWord & " " & secondWord & " " & thirdWord & "." 


The variable sentence will end up with the value "ASP.NET is neat." Note that first three string variablesfirstWord, secondWord, and thirdWordare created, where each variable holds a word in a sentence. Next, the string variable sentence is declared. We want to assign to sentence the value of each of the three words concatenated together, with a space between each word and a period at the end.

To accomplish this, we use the & operator to join together six strings. First, the string firstWord and " " are concatenated, resulting in the temporary string "ASP.NET". I use the word temporary here because this string is immediately concatenated with secondWord, resulting in "ASP.NET is", which is then concatenated with " ", resulting in "ASP.NET is ". Next, this is concatenated with thirdWord, resulting in "ASP.NET is neat", and finally this is concatenated with ".", resulting in "ASP.NET is neat.", which is then assigned to the variable sentence.

Inserting the Value of a Variable into a String

In many situations, we may want to insert the value of a string variable into another string. For example, imagine that we have a variable called firstName that contains the user's first name, and we want to display a message on the web page that reads "Hello FirstName", where FirstName is the value of the variable firstName. That is, if the value of firstName is Scott, we want the message "Hello Scott" to appear.

First, we might want to create a string variable named output that would contain the final string that we want to display. To accomplish this, we would use code like

Dim output as String output = "Hello, " & firstName 


It is important to realize that after these two lines of code execute, the variable output will contain the value "Hello, FirstName", where FirstName is the value of firstName. Note that we did not use

Dim output as String output = "Hello, firstName" 


Had we used this syntax, the value of output would be precisely as we indicated: "Hello, firstName". To insert the value of firstName into the string output, we need to concatenate the string "Hello, " with the string value of firstName. This is done using the concatenation operator, not by simply inserting the variable name into the string.


Visual Basic's Assignment Operators

The most common assignment operator is the = operator, which takes the form

variableName = value 


For example, to assign the value 5 to an integer variable, we can use the following code:

Dim age as Integer age = 5 


The value assigned to a variable can be things more complex than simple values like 5. The value can be an expression involving other operators. For example, we might want to add two numbers and store this sum in a variable. To accomplish this, we could use code like

'Create three integer variables Dim sum, number1, number2 as Integer number1 = 15 number2 = 20 'Assign the sum of number1 and number2 to sum sum = number1 + number2 


Shorthand Versions for Common Assignments

In many situations we might have a variable that is routinely updated in some fashion. We'll see some concrete examples of this in the next hour, but for now take my word for it that commonly we will be interested in incrementing an integer variable by one.

To accomplish this, we could use the following code:

Dim someIntegerVariable as Integer = 0 ... someIntegerVariable = someIntegerVariable + 1 ... 


Initially, someIntegerVariable is declared with an initial value of 0. Later, we want to increment the value of someIntegerVariable. To do this, we add one to the current value of someIntegerVariable and then store the new value back into someIntegerVariable. So, if someIntegerVariable equals 0,

someIntegerVariable = someIntegerVariable + 1 


will take the value of someIntegerVariable (0), add 1 to it (yielding 1), and store 1 into someIntegerVariable. The next time the line

someIntegerVariable = someIntegerVariable + 1 


is encountered, someIntegerVariable will equal 1. Hence, this line of code will first evaluate someIntegerVariable + 1, which is the value of someIntegerVariable (1) plus 1. It will then assign this value (2) back into someIntegerVariable. As you can see, this line of code increments the value of someIntegerVariable by 1 regardless of what the current value of someIntegerVariable is.

Because this is a very common line of code, Visual Basic provides an alternate assignment operator to reduce the amount of code we need to write. This shorthand operator, +=, has the form

variableName += value 


and has the effect of adding value to the current value of variableName and then storing the resulting value of this addition back into variableName. The following two lines have exactly the same meaning and produce exactly the same resultsincrementing someIntegerVariable by 1:

someIntegerVariable = someIntegerVariable + 1 


and

someIntegerVariable += 1 


In addition to +=, there are a number of other shorthand assignment operators, as shown in Table 5.2. Along with the shorthand arithmetic operators, you'll notice the shorthand concatenation operator, &=. This and the += operator are the two shorthand assignment operators we'll use most often.

Table 5.2. The Shorthand Assignment Operators

Operator

Description

+=

variable += value adds value to the value of variable and then stores this resulting value back into variable.

-=

variable -= value subtracts value from the value of variable and then stores this resulting value back into variable.

*=

variable *= value multiplies value to the value of variable and then stores this resulting value back into variable.

/=

variable /= value divides value into the value of variable and then stores this resulting value back into variable. Recall that the / operator returns a nonintegral value.

&=

variable &= value concatenates value to the value of variable and then stores this resulting value back into variable.





Sams Teach Yourself ASP. NET 2.0 in 24 Hours, Complete Starter Kit
Sams Teach Yourself ASP.NET 2.0 in 24 Hours, Complete Starter Kit
ISBN: 0672327384
EAN: 2147483647
Year: 2004
Pages: 233

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net