Chapter 03 Variables, Constants, and Calculations
Chapter 03 Variables, Constants, and Calculations
Variables, Constants,
and Calculations
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-2
Objectives (2 of 2)
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-3
Data — Variables and Constants (1
of 2)
Variable
• Memory locations that hold data that can be
changed during project execution
• Example: customer’s name
• Named Constant
• Memory locations that hold data that cannot
be changed during project execution
• Example: sales tax rate
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-4
Data — Variables and Constants (2
of 2)
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-6
Data Types
Data Type Prefix
Boolean bln
Byte byt
Character chr
Date dtm
Decimal dec
Single sng
Double dbl
Short sht
Integer int
Long lng
String str
Object obj
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-7
Naming Variables and Constants
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-8
Constants
• Named
• User assigned name, data type, and value
• Use CONST keyword to declare.
Const COMPANY_ADDRESS_String As String = "101 S. Main Street"
Const SALES_TAX_RATE_Decimal As Decimal = .08D
• Intrinsic
• System defined within Visual Studio
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-9
Assigning Values to Constants
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-10
Declaring Variables
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-11
Declaration Statement Examples
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-12
Scope and Lifetime of Variables (1 of
2)
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-13
Module Level Variable Declaration
Example
Code module-level declarations in the Declaration section
at the top of the code.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-14
Guidelines for User Input
• Each Input object (textbox, etc) and Output object (label,
textbox, etc.) will have its own variable associated with it
• All calculations (formulas) will be done ONLY with
variables, constants and functions; the results of all
calculations will be placed in a variable
• All variables will be formatted into an output object (label,
textbox, etc.) in order to make them visible and appear in
user friendly format
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-15
15
Chapter 4: Variables and
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-16
16
Chapter 4: Variables and
Calculations
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-17
Converting Strings to a
Numeric Data Type
• Use Parse methods to convert the Text property to
its numeric form before it’s used in a calculation.
• Each numeric data type class has a Parse method.
• Parse method returns a value that can be used in
calculations.
• Parse method fails if user enters nonnumeric data
or leaves data blank.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-18
Converting to String
Examples:
ResultTextBox.Text = ResultDecimal.ToString()
CountTextBox.Text = CountInteger.ToString()
IDString = IDInteger.ToString()
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-19
Conversion Methods
Method Convert
To
Integer.Parse Integer
Decimal.Parse Decimal
.ToString String
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-20
Conversion Examples
QuantityInteger
=Integer.Parse(quantityTextBox.Text)
PriceDecimal =Decimal.Parse(priceTextBox.Text)
WholeNumberInteger =Integer.Parse(digitString)
ResultTextBox.Text =ResultDecimal.ToString( )
CountTextBox.Text =CountInteger.ToString( )
IDString =IDInteger.ToString( )
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-21
Arithmetic Operations
Operator Operation
+ Addition
– Subtraction
* Multiplication
/ Division
\ Integer Division
Mod Modulus – Remainder of
division
^ Exponentiation
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-22
Order of Operations
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-23
Evaluation of Expression
1. All operations within parentheses. Multiple operations within
the parentheses are performed according to the rules of
precedence.
2. All exponentiation. Multiple exponentiation operations are
performed from left to right.
3. All multiplication and division. Multiple operations are
performed from left to right.
4. All integer division. Multiple operations are performed from left
to right.
5. Mod operations. Multiple operations are performed from left to
right.
6. All addition and subtraction are performed from left to right.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-24
Mathematical Examples
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-25
Using Calculations in Code
‘Accumulate a total.
TotalSalesDecimal += salesDecimal
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-26
Option Explicit and Option Strict
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-27
Converting Between Numeric Data Types
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-28
Performing Calculations with
Unlike Data Types
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-30
Formatting Data for Display
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-31
Using Format Specifier Codes
• "C" code
• Currency — String formatted with dollar sign,
commas separating each group of 3 digits and 2
digits to the right of decimal point
• "N" code
• Number — String formatted with commas
separating each group of 3 digits and 2 digits to the
right of decimal point
• Can specify number of decimal positions
• Example: "C0" zero digits
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-32
Format Specifier Codes
C or c Currency
F or f Fixed-point
N or n Number
D or d Digits
P or p Percent
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-33
Format Specifier Code Examples
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-34
Date Specifier Code
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-35
Handling Exceptions
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-37
Try Block — General Form
Try
‘statements that may cause an error
Catch [VariableName As ExceptionType]
‘statements for action when an exception occurs
[Finally
‘statements that always execute before exit of the Try
block]
End Try
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-38
Try Block — Example
Catches Any Exception
Try
QuantityInteger = Integer.Parse(QuantityTextBox.Text)
QuantityTextBox.Text = QuantityInteger.ToString( )
Catch
MessageLabel.Text = "Error in input data."
End Try
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-39
Try Block — Example
Catches Specific Exception
• This Catch statement catches bad input
data that cannot be converted to numeric.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-40
Common Exception Classes
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-41
Try Block — Example
Handling Multiple Exceptions
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-42
MessageBox Object (1 of 2)
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-43
MessageBox Object (2 of 2)
• TextMessage string
• String literal or variable that displays message
• Title Bar text
• String that appears in title bar of message box
• MessageBox Buttons
• OK, OKCancel, RetryCancel, YesNo, YesNoCancel,
AbortRetryIgnore
• MessageBox Icons
• Asterisk, Error, Exclamation, Hand, Information, None,
Question, Stop, Warning
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-44
Using Overloaded Methods
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-45
Testing Multiple Fields
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-46
Counting and Accumulating Sums
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. 3-47