066ffd5fe8e169f5e754a9f55ff40560.ppt
- Количество слайдов: 21
©Copyright 1997 -2006 by Ronald P. Kessler, Ph. D.
• They allow us to use math computations • They make our programs generic to many solutions
• Just as in algebra, variables in VB are used to represent values of things that can change. • I can use a variable to compute sales tax: Tax. Rate=. 0775 Sales. Tax= Price * Tax. Rate Sales. Tax, Price, and Tax. Rate are variables
• When we have been entering items & prices in our data entry screens without specifically telling VB what is text and what should be a number. • Items that we type in (text) are Strings • Prices are numbers and should be treated like Decimal.
• Let’s say I buy 3 dozen mouse traps at 16. 95 per dozen. • I can tell VB to compute the total: • My Total will be ( $16. 95 * 3) or $50. 85 In code it is: Total = 16. 95 * 3 I have defined the variable Total simply by typing it in my formula.
Price = 23. 90 Tax =. 0775 Total = Price * Tax lbl. Total. Text=Total
Use the +, -, * , and “/” for the basic math operations. Integer Division uses a “” (backslash) so that: 52 = 2 Use variables in your computations: Dim Sales as Decimal Dim Sales. Tax as Decimal Dim Total. Sale as Decimal Sales= 765. 90 Sales. Tax=. 0775 Total. Sale= Sales * Sales. Tax lbl. Total. Text = Total. Sale
How to solve/setup formulas: 64 / 8 * 3 / ( 6 + 6 ) - 2 = 64 /8 * 3 / 12 -2 = 24 / 12 -2 = 2 -2=0 1. Always do stuff in () first 2. Exponentiation (raise things to powers) 3. * and / 4. + and - Always go left to right and handle * and / or + and - as they come
In the formula: Income= Hours. Worked * Pay. Rate we are computing a math operation. In the formula: btn. Print. Text = “Print this junk” we are assigning data from the right side to the left. When we say : Num. Customers = Num. Customers + 1 we are assigning, not doing math. In mathematics, this statement cannot exist. In VB, the equal sign (=) doubles as an operator and an assignment character Assignments take the stuff on the right and assign it to the variable on the left and stores that value in memory.
‘----assign data from data entry screen and use conversion functions ‘ Procedure= txt. Proc. Text Charge= CDec(txt. Chrg. Text) ‘Convert text to decimal type Paid= CDec(txt. Paid. Text) Adjustment= CDec(txt. Adj. Text) Current. Bal= Charge - Paid – Discount lbl. Result. Text = Format. Number(Current. Bal, 2) ‘ 2 decimals OR…. lbl. Result. Text = Format. Currency(Current. Bal, 2) Format. Number & Val are built-in functions in VB ‘shows $
Defining Variables the Correct Way. . .
Why Define Variables Ourselves? • This makes much better use of memory (RAM) • It makes our programs run faster • It will catch our spelling errors
Where Do I Tell VB to Force Me to Define Variables? In the GENERAL section of the main form of your program use the term: Option Explicit On You can force VB to make you declare variables from every project automatically if you want. Or you can type in Option Explicit On each time. I want you to start using this from now on! Use Tools|Options|Projects to set it in Visual Studio.
How to Define Variables Ourselves • We use the command Dim (dimension) to define the type of data we are wanting to use. • If I want the total cost of an item, then I tell VB this before I start using the variable: • You usually Dimension variables in the subroutines (event procedures) where you are going to use them. . .
In the section of the form, just after “Windows Form designer generated code”, use this to define a constant : Const SALESTAX As Decimal = 0. 08 D The “D” makes it a decimal…I know it looks redundant! Get in the habit of typing constants all in capitals. Now we can use the value assigned to SALESTAXRATE any where in our program. This value never changes as long as the program is running.
066ffd5fe8e169f5e754a9f55ff40560.ppt