![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
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.
Im The Manipulative TypeNow that you know how to store data in your program, youd probably like to know what you can do with the data. The answer is, Lots. Some of the most important tasks involve Visual Basics operators. An operator is a symbol or word that instructs Visual Basic to manipulate data in a certain way. Youve 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.
Arithmetic OperatorsThe arithmetic operators perform mathematical manipulations. There are seven of them. The first four, listed in Table 4.3, are the common operations that Im 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 ManipulationThe 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 its best to stick with & for new programs.
Operator PrecedenceWhat 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 Basics 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 isnt what you want? Lets 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
|
![]() |
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. |