Presentation on theme: "Variables, Constants &Computation T o i n s e r t y o u r c o m p a n y l o g o o n t h i s s l i d e F r o m t h e I n s e r t M n u e l e c t P i c."— Presentation transcript:
Why Use Variables? They allow us to use math computations They make our programs generic to many solutions
What Is a Variable? 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: TaxRate=.0775 SalesTax= Price * TaxRate SalesTax, Price, and TaxRate are variables
How Does VB Know What Each Variable Is? 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.
PROBLEM: I want to compute the total cost of items purchased. Lets 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.
Data Types for.Net (See Page 99) Short (Integer)+ 32,768 2 bytes(i or int) Integer + 2,147,483,648 4 bytes(i or int) Long Integer+ 9,223,372,036,854,775,808 8 bytes(lng) Single Precision 6 digits of accuracy 4 bytes(sng) Double Precision 14 digits of accuracy 8 bytes(dbl) Decimal More $$ than we can count 16 bytes(dec) StringSize of Str.(s or str) BooleanTrue/False 2 bytes (bln) In VB 3,4,5,& 6 we used Currency instead of decimal & it was 8 bytes
Math Operators and Computations Price = 23.90 Tax =.0775 Total = Price * Tax lblTotal.Text=Total
Math Operators Use the +, -, *, and / for the basic math operations. Integer Division uses a \ (backslash) so that: 5\2 = 2 Use variables in your computations: Dim Sales as Decimal Dim SalesTax as Decimal Dim TotalSale as Decimal Sales= 765.90 SalesTax=.0775 TotalSale= Sales * SalesTax lblTotal.Text = TotalSale
Operation Hierarchy How to solve/setup formulas: 64 / 8 * 3 / ( 6 + 6 ) - 2 = 64 /8 * 3 / 12 -2 = 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
Difference Between Assignment and Math In the formula: Income= HoursWorked * PayRate we are computing a math operation. In the formula: btnPrint.Text = Print this junk we are assigning data from the right side to the left. When we say : NumCustomers = NumCustomers + 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.
Working with Data Entry... ----assign data from data entry screen and use conversion functions Procedure= txtProc.Text Charge= CDec(txtChrg.Text) Convert text to decimal type Paid= CDec(txtPaid.Text) Adjustment= CDec(txtAdj.Text) CurrentBal= Charge - Paid – Discount lblResult.Text = FormatNumber(CurrentBal, 2) 2 decimals OR…. lblResult.Text = FormatCurrency(CurrentBal, 2) shows $ FormatNumber & Val are built-in functions in VB
Formatting Your Numbers... To format Currency (Decimal data type) we can say: lblTax.Text = CDec(txtPrice.Text) * 0.08 'Make VB treat text as a number using the CDec (Convert to Decimal) function and we decide how to format it. lblTax.Text =FormatNumber(CDec(txtPrice.Text * 0.08), 2) lblBalDue.Text = Format(CDec(txtPrice.Text) + CDec(lblTax.Text), "###.00") If you want a $ to appear before the amount, use: lblBalDue.Text = FormatCurrency(CDec(txtPrice.Text) + CDec(lblTax.Text), 2") VB also has FormatPercent & FormatdateTime
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...
Define Variables In Subs... Private Sub btnPrint_Click () ---Declare variables here Dim strItem as String Dim decPrice as Decimal Dim decTotal as Decimal Dim intQuantity as Integer strItem= txtItem.Text decPrice= CDec(txtPrice.Text) intQuantity= CInt(txtQuantity.Text) use CInt or CShort decTotal = decPrice * intQuantity lblReceipt.Text = "Total===>" & Format(CDec(txtPrice.Text) + CDec(lblTax.Text), "###.00") End Sub
In the section of the form, just after Windows Form designer generated code, use this to define a constant : Const SALESTAX As Decimal = 0.08D 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.
Using Your Own Constants... ---Inside the form code Const SALESTAX As Decimal = 0.08D _________________________________________ Sub btnPrint_Click () Dim Tax as Decimal Dim Price as Decimal Price= CDec(txtPrice.Text) Tax= Price * SALESTAXRATE End Sub