Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
(Publisher: The Coriolis Group)
Author(s): Peter G. Aitken
ISBN: 1576102815
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Now, pgaInProgress = 2 and pgaShipped = 3. You can assign non-sequential values also:

Public Enum OrderStatus
   PgaBackordered = -1
   PgaInProgress = 15
   PgaShipped = 123
End Enum

Why use an enumeration instead of named constants, as shown here?

Const pgaBackordered = 0
Const pgaInProgress = 1
Const pgaShipped = 2

In terms of using the constant names, there is no advantage. The advantage to using an enumeration lies in the fact that it becomes a type you can use for variables and for the parameters and return values of procedures. Thus, you could declare a variable as follows:

Dim StateOfOrder As OrderStatus

Or, declare procedures to take the enumeration type as a parameter:

Public Sub SetStatus (X As OrderStatus)
...
End Property

Or as the return value of a function:

Public Function GetOrderStatus(OrderNumber As Integer) As OrderStatus
...
End Function

Enumerations can be viewed in the Visual Basic Object Browser, and the AutoList feature will display a list of enumeration members while you are writing code.


TIP:  Naming Enumeration Members

To avoid conflicts with other variable or constant names, it is standard practice to create enumeration member names that all start with the same series of lowercase letters. In the examples, I used my initials pga, but it is more convenient to use something that relates the enumeration members to the class they are defined in. Within a module that deals with order processing, for example, you might prefix member names with ordr.


I’m The Manipulative Type

Now that you know how to store data in your program, you’d probably like to know what you can do with the data. The answer is, “Lots.”

Some of the most important tasks involve Visual Basic’s operators. An operator is a symbol or word that instructs Visual Basic to manipulate data in a certain way. You’ve already been introduced to the assignment operator (=), which tells Visual Basic to make the variable or object property on the left of the operator equal to the expression on the right side of the operator.


TIP:  What’s An Expression?

You’ll see the term expression used frequently, and you’re probably wondering just what exactly it refers to. It is quite simple, actually. An expression is anything that evaluates to a number, a string, or a logical (True/False) value. Thus, the literal constant 5 is an expression, as is 5+2.


Arithmetic Operators

The arithmetic operators perform mathematical manipulations. There are seven of them. The first four, listed in Table 4.3, are the common operations that I’m sure you are famil-iar with.

The last three arithmetic operators may be more obscure. Integer division, represented by the \ symbol, divides two numbers and returns an integer result, discarding any fractional part of the answer. Thus, 7 \ 2 evaluates to 3, as does 6 \ 2. No rounding occurs; any fractional part of the answer is simply discarded. Thus, both 21 \ 10 and 29 \ 10 evaluate to 2.

The exponentiation operator raises a number to a power. The symbol for this operation is ^. In Basic, therefore, X ^ Y means the same as the more common notation XY. If X is negative, then Y must be an integer; otherwise, both X and Y can be floating point values.

The modulus operator, represented by the keyword Mod, divides two numbers and returns only the remainder. The expression 7 Mod 2 evaluates to 1, 23 Mod 4 evaluates to 3, and 25 Mod 5 evaluates to 0. Any fractional part of the answer is truncated, so 23.5 Mod 4 evaluates to 3, not 3.5.

String Manipulation

The only operator that works with string data is called the concatenation operator, represented by the symbol &. Concatenation simply means to tack one string onto the end of another. For example, if MyString is a string variable, then executing the statement

MyString = “Visual ” & “Basic”

results in the string “Visual Basic” being stored in the variable. You can also use the + symbol for string concatenation. It is provided for compatibility with old Basic programs, but it’s best to stick with & for new programs.

Table 4.3 Arithmetic operators.

Operation Symbol Example Result
Addition + 2 + 5 7
Subtraction - 18 - 10 8
Multiplication * 2 * 5 10
Division / 10 / 2 5


TIP:  Is That All?

Is concatenation all that Visual Basic can do with strings? Not by a long shot. It performs other string manipulations, not with operators, but with the built-in string procedures. These will be covered in Chapter 11.


Operator Precedence

What happens if an expression contains more than one operator? What difference does it make? An example will illustrate. Consider this expression:

5 + 3 * 2

What does it evaluate to? If we perform the addition first, it evaluates to 16 (5 + 3 = 8, 8 * 2 = 16); but if we perform the multiplication first, the result is 11 (3 * 2 = 6, 5 + 6 = 11). Which is correct? Because of such potentially ambiguous expressions, Visual Basic includes strict rules of operator precedence. This is just a fancy way of determining which operations are performed first. The precedence of Visual Basic’s operators is given in Table 4.4. Operators with low precedence numbers are performed first.

Returning to the original example, we can see that the expression 5 + 3 * 2 will evaluate to 11, because multiplication has a higher precedence than addition and thus will be performed first. For operators that have the same precedence level, such as multiplication and division, the order of execution is always left to right.

What if the order of execution specified by the operator precedence rules isn’t what you want? Let’s say you would like to add variables A and B, then multiply the sum by variable C. Can this be done? Yes. Parentheses come to the rescue. By including parentheses in an


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.