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

21
Variables, Constants Variables, Constants & & Computation Computation ©Copyright 1997-2006 by Ronald P. Kessler, Ph.D. Introduction to VB.Net 2003 Introduction to VB.Net 2003

Transcript of 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...

Page 1: 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.

Variables, ConstantsVariables, Constants &&

ComputationComputation

©Copyright 1997-2006 by Ronald P. Kessler, Ph.D.

Introduction to VB.Net 2003Introduction to VB.Net 2003

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

Why Use Variables?Why Use Variables?

•They allow us to use math computations•They make our programs generic to many solutions

Page 3: 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.

What Is a Variable?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= .0775SalesTax= Price * TaxRate

SalesTax, Price, and TaxRate are variables

Page 4: 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.

How Does VB Know What Each Variable Is?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.

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

PROBLEM:PROBLEM:I want to compute the total cost of items purchasedI want to compute the total cost of items purchased..

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

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

Data Types for .NetData Types for .Net(See Page 99)(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)

String Size of Str. (s or str)

Boolean True/False 2 bytes (bln)

In VB 3,4,5,& 6 we used Currency instead of decimal & it was 8 bytes

Page 7: 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.

Math Operators and Math Operators and ComputationsComputations

Price = 23.90

Tax = .0775

Total = Price * Tax

lblTotal.Text=Total

Page 8: 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.

Math OperatorsMath OperatorsUse 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

Page 9: 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.

Operation HierarchyOperation HierarchyHow 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

Page 10: 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.

Difference Between Assignment and MathDifference 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.

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

Working with Data Entry...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

Page 12: 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.

Formatting Your Numbers...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

Page 13: 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.

Defining VariablesDefining Variablesthethe

Correct Way . . .Correct Way . . .

Page 14: 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.

Why Define Variables Why Define Variables Ourselves?Ourselves?

•This makes much better use of memory (RAM)

•It makes our programs run faster

•It will catch our spelling errors

Page 15: 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.

Where Do I Tell VB to Force Me to Define Where Do I Tell VB to Force Me to Define Variables?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|Projectsto set it in Visual Studio.

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

How to Define Variables How to Define Variables OurselvesOurselves

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

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

Define Variables In Subs. . .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

Page 18: 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.

Using Constants...Using Constants...

Page 19: 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.

Using Constants...Using Constants...

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.

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

Using Your Own Constants...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

Page 21: 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.

All Done….for now at least.All Done….for now at least.