Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations

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:


2 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 t u r e L o c a t e y o u r l o g o f i l e C l i c k O K T o r e s i z e t h e l o g o C l i c k a n y w h e r e i n s i d e t h e l o g o. T h e b o x e s t h a t a p p e a r o u t s i d e t h e l o g o a r e k n o w n a s r e s i z e h a n d l e s. U s e t h e s e t o r e s i z e t h e o b j e c t. I f y o u h o l d d o w n t h e s h i f t k e y b e f o r e u s i n g t h e r e s i z e h a n d l e s, y o u w i l l m a i n t a i n t h e p r o p o r t i o n s o f t h e o b j e c t y o u w i s h t o r e s i z e. ©Copyright 1997-2006 by Ronald P. Kessler, Ph.D. Introduction to VB.Net 2003

3 Why Use Variables? They allow us to use math computations They make our programs generic to many solutions

4 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

5 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.

6 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.

7 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

8 Math Operators and Computations Price = 23.90 Tax =.0775 Total = Price * Tax lblTotal.Text=Total

9 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

10 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

11 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.

12 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

13 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

14 Defining Variables the Correct Way...

15 Why Define Variables Ourselves? This makes much better use of memory (RAM) It makes our programs run faster It will catch our spelling errors

16 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.

17 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...

18 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

19 Using Constants...

20 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.

21 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

22 All Done….for now at least.

Download ppt "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."

Similar presentations

Ads by Google