Sams Teach Yourself Visual Basic 6 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- Acknowledgments
- Introduction
- Who Should Read This Book
- What This Book Will Do for You
- Can This Book Really Teach Visual Basic in 24 Hours?
- What You Need
- Files on the Visual Basic Distribution CD-ROM
- Conventions Used in This Book
- Enough! Time Is Ticking!
- Part I: Introducing Visual Basic
- Hour 1. Visual Basic at Work
- Hour 2.Analyzing Visual Basic Programs
- Hour 3.Controls and Properties
- Hour 4.Examining Labels, Buttons, and Text Boxes
- Part II: Coding the Details
- Hour 5.Putting Code into Visual Basic
- Coding Basics
- Data Basics
- Expressions and Math Operators
- Summary
- Q&A
- Workshop
- Hour 6.Message and Input Boxes
- Hour 7.Making Decisions
- Hour 8.Visual Basic Looping
- Part III:Putting Code to Work
- Hour 9.Combining Code and Controls
- Hour 10.List Boxes and Data Lists
- Hour 11.Additional Controls
- Hour 12.Dialog Box Basics
- Part IV:Programming with Data
- Hour 13.Modular Programming
- Hour 14.Built-In Functions Save Time
- Hour 15.Visual Basic Database Basics
- Hour 16.Printing with Visual Basic
- Part V:Sprucing Up Programs
- Hour 17.Menus and Visual Basic
- Hour 18.The Graphic Image Controls
- Hour 19.Toolbars and More Graphics
- Hour 20.Writing Correct Applications
- Part VI:Advancing Visual Basic Applications
- Hour 21.Visual Basic and ActiveX
- Hour 22.Object Basics
- Hour 23.Distributing Your Applications
- Hour 24.Online Visual Basic
- Part VII:Appendixes
- Appendix A.Operator Precedence
- Appendix B.Answers
- Appendix C.Using the CD-ROM
Expressions and Math Operators
You should learn Visual Basic's math operators so you can calculate and assign expression results to variables when you code assignment statements that contain expressions. An operator is a symbol or word that does math and data manipulation.
Table 5.4 describes Visual Basic's primary math operators. Other operators exist, but the ones in Table 5.4 suffice for most of the programs that you will write. Look over the operators. You are already familiar with most of them because they look and act just like their real-world counterparts.
Table 5.4. The primary math operators.
| Operator | Example | Description |
| + | Net + Disc | Adds two values |
| - | Price - 4.00 | Subtracts one value from another value |
| * | Total * Fact | Multiplies two values |
| / | Tax / Adjust | Divides one value by another value |
| ^ | Adjust ^ 3 | Raises a value to a power |
| & (or +) | Name1 & Name2 | Concatenates two strings |
Suppose that you wanted to store the difference between the annual sales (stored in a variable named curAnnualSales) and cost of sales (stored in a variable named curCostOfSales) in a variable named curNetSales. Assuming that all three variables have been defined and initialized, the following assignment statement computes the correct value for curNetSales:
curNetSales = curAnnualSales - curCostOfSales
This assignment tells Visual Basic to compute the value of the expression and to store the result in the variable named curNetSales. Of course, you can store the results of this expression in a control's Caption or Text properties, too.
If you want to raise a value by a power—which means to multiply the value by itself a certain number of times—you can do so. The following code assigns 10000 to lngValue because 10 raised to the fourth power (that is, 10 times 10 times 10 times 10) is 10,000:
lngYears = 4 lngValue = 10 ^ intYears
No matter how complex the expression is, Visual Basic computes the entire result before it stores that result in the variable at the left of the equal sign. The following assignment statement, for example, is rather lengthy, but Visual Basic computes the result and stores the value in the variable named sngAns:
sngAns = 8 * sngFactor - sngPi + 12 * sngMonthlyAmts
Combining expressions often produces unintended results because Visual Basic computes mathematical results in a predetermined order. Visual Basic always calculates exponents first if one or more ^ operators appear in the expression. Visual Basic then computes all multiplication and division—working from left to right—before any addition and subtraction.
Visual Basic assigns 13 to intResult in the following assignment:
intResult = 3 + 5 * 2
At first, you might think that Visual Basic would assign 16 to intResult because 3 + 5 is 8 and 8 * 2 is 16. However, the rules state that Visual Basic always computes multiplication—and division if division exists in the expression—before addition. Therefore, Visual Basic first computes the value of 5 * 2, or 10, and next adds 3 to 10 to get 13. Only then does it assign the 13 to Result.
If both multiplication and division appear in the same expression, Visual Basic calculates the intermediate results from left to right. For example, Visual Basic assigns 20 to the following expression:
intResult = 8 / 2 + 4 + 3 * 4
Visual Basic computes the division first because the division appears to the left of the multiplication. If the multiplication appeared to the left of the division, Visual Basic would have multiplied first. After Visual Basic calculates the intermediate answers for the division and the multiplication, it performs the addition and stores the final answer of 20 in intResult.
It is possible to override the operator precedence by using parentheses. Visual Basic always computes the values inside any pair of parentheses before anything else in the expression, even if it means ignoring operator precedence. The following assignment statement stores 16 in intResult because the parentheses force Visual Basic to compute the addition before the multiplication:
intResult = (3 + 5) * 2
The following expression stores the fifth root of 125 in the variable named sngRoot5:
sngRoot5 = 125 ^ (1/5)
As you can see from this expression, Visual Basic supports fractional exponents.
To concatenate means to merge two strings together.
One of Visual Basic's primary operators has nothing to do with math. The concatenation operator joins one string to the end of another. Suppose that the user entered his first name in a Label control named lblFirst and his last name in a Label control named lblLast. The following concatenation expression stores the full name in the String variable named strFullName:
strFullName = lblFirst & lblLast
There is a problem here, though, that might not be readily apparent—there is no space between the two names. The & operator doesn't automatically insert a space because you don't always want spaces inserted when you concatenate two strings. Therefore, you might have to concatenate a third string between the other two, as in
strFullName = lblFirst & " " & lblLast
Visual Basic actually supports a synonym operator, the plus sign (+), for concatenation. In other words, the following assignment statement is identical to the previous one (although the ampersand [&] keeps ambiguity down because of the plus sign's double usage with numbers and strings):
strFullName = lblFirst + " " + lblLast
Use the ampersand for string concatenation even though the plus sign works also. The ampersand is less ambiguous and makes for better programs.
Summary | Next Section

Account Sign In
View your cart