Title: Working with Variables, Constants, Data Types, and Expressions
1Chapter 4
- Working with Variables, Constants, Data Types,
and Expressions
2Objectives
- Learn to use the RadioButton and GroupBox
controls, and Layout toolbar - Learn to use the Pmt function, and the Format
function - Lock controls on a form
- Visual Basic .NET data types
- Declare and use variables and constants within
code - Convert between data types
- Use the Option Strict statement
- Use arithmetic expressions
- Describe the order of operator precedence in code
3Problem Car Loan Calculator
- Develop a window application for Automobile Loan
Calculator. It will calculate a customers
monthly automobile loan payment based on a loan
amount, interest rate, and the term, or length,
of the loan. - MonthlyPayment (InterestRate
(1InterestRate) NumberofPayments) / ((1
InterestRate) NumberofPayments-1) LoanAmount
4Program Development
5Problem Analysis
Question What are the inputs, outputs?
6Interface Design
- The increment of Loan amount is 1000. The
maximum value allowed is 25000
- The Current interest rate is incremented or
decremented by 0.05 with minimum 5.0 and
maximum 15.00. - The format of Monthly payment is currency
7Program Design
- Two tasks
- Calculate monthly payment
- Reset the input and output values
8Validate Design
- Validate the design by stepping through the
requirements and make the design addresses each
requirement - For example,
- The formula in the Algorithms section of the
requirements document is handled in the design by
the Computer Payment Click event - The items in the first note in the Notes section
are shown as requested on the storyboard
9Starting a Project
- Start VB .NET and click the New Project button on
the Start Page - Click Create directory for Solution, type
Automobile Loan Calculator in the Name box - Browse data disk for Chapter4, and then click
Open - Click the OK button
10Setting Form Properties
- Set the properties of Form1 using the table as a
guide
11Adding Controls
- Add three Label, two NumericUpDown, one TextBox,
and two Button controls - Add a GroupBox control
12Adding RadioButton Controlsto a Group Box
- Drag the RadioButton button to GroupBox1 control.
Be sure to release the mouse button while the
mouse pointer is within the borders of the
GroupBox1 - Repeat this step for two more RadioButton controls
13Change Properties of Controls
14Change GroupBox Properties
- The GroupBox is used to group functionally
similar controls together
15Change GroupBox Properties
Checked property value is true
16Using Layout Toolbar
- The layout toolbar contains tools that allow you
to adjust the alignment, spacing, and size of
controls - Select the Label2 control, the Current interest
rate () - Resize the control so that the text displays on a
single line. Press and hold down the CTRL key
and then click the other two Label controls on
the form - Click the Align Rights button on the Layout
toolbar and then click the Make Vertical Spacing
Equal button on the Layout toolbar.
- When use Layout toolbar to size and align
multiple controls, the last control you select is
used as the basis for aligning the controls
17Sizing and Aligning Controls
- Select the nudLoanAmount, nudRate, and
txtMonthlyPayment controls. Click the Make Same
Size button and then Align Lefts button on the
toolbar.
18Setting a Default Button
- The default button is the Compute Payment button.
Pressing the ENTER key is equivalent to clicking
the button - Select the AcceptButton property in the
properties window to btnComputePayment
19Locking Controls Positions
- Disallows the ability to move controls or modify
control sizes on a form during design time - Click Format on the menu bar, and select Lock
Controls
20Constants and Variables
- A value is a number or string that programmers
use in the code - A variable represents a location in computer
memory that can change values as the code
executes - A constant represents a location in computer
memory that cannot be changed during execution - By the name of a constant, we can easily tell the
use or the purpose of the constant
21Data Types
- Computer uses binary numbers to represent
everything - 1310000011012 c 9910 011000112
- The data type of a variable or a constant
determines what kind of data the variable or
constants can store - Try to use the data type that takes up the
smallest amount of memory - Try to use integral data type if decimal number
is not necessary
22Data Types
23Declaring Constants
24Declaring Constants
- Double-click the Form1 form in an area that does
not contain a control. - When the code window displays, enter the seven
lines of code below
25Coding Form Load Event Procedure
- The form Load Event Procedure executes when the
form first displays on screen. Execution happens
before any other events. - Click the Form1_Load event procedure and enter
the two lines of code below.
26Coding the btnReset_Click Event Procedure
- Click the Form1.vbDesign tab and then
double-click the btnReset control. Enter the 7
lines of code below.
27Option Strict Statement
- To force you to ensure that all assignment
statements use the same data type on both sides
of the assignment - In the code window on line 1, type Option Strict
On
28Declaring Variables
29Scope of a Variable
- Scope refers to a variables accessibility. It is
defined by the placement of the variable in the
code. - MaximumLoanAmount declared in the general area of
the form is a global constant - Variable declared inside a procedure is a local
variable - Local variable is not accessible outside that
procedure
30Global Variable
- Append a letter g in front of a variable name to
indicate a global variable - Place global variable after the comment header
31Event Procedures of RadioButton
- The CheckChanged event procedure executes when
user clicks a RadioButton - Double-click the radTwoYears control on the
Form1.vbDesign, enter the code - Repeat for radFiveYears and radSixYears buttons
32Declaring Local Variables
- Click the Form1.vbDesign tab and then
double-click the btnComputePayment control.
Enter the code below in the code window.
33Converting Data Types
- To ensure that all data being used in the
calculation has the same data type, we may use
Convert.ToDouble() method.
34Arithmetic Operators
- Arithmetic operator is used to manipulate two or
more numeric values - The Mod operator divides two numbers and return
the remainder of division operation - The \ operator truncates any decimal numbers to
integers, divides, and returns an integral
quotient
34 mod 6 is 4 6.6 \ 2.5 is 3 6.6 / 2.5 is 2.64
35Precedence of Operators
- Precedence of operators determine the sequence an
expression is evaluated by a computer - An expression is evaluated from left to right
- (), , , /, \, Mod, , -
(2 3 4 / 5) 2 5 / (4 3 2 3) (2
3 4 / 5) 2 5 / (4 3 8) (2 2.4) 2
5 / 4 (-0.4) 2 5 / 4 0.16 5 / 4 0.16
1.25 1.41
36In-Class Exercise
- Evaluate each of the following expressions
- 4 5 3 / 6 6 2 / 12
- 7 5 / 2 9 Mod 3 3
- If necessary, insert parentheses so that each
numeric expression results in the value indicated
on the right side of the arrow - 8 / 2 2 12 ? 14
- 7 3 4 2 3 / 13 ? 22
37Construction of Error-Free Numeric Expressions
- Do not attempt to divide by 0
- Do not attempt to take square root of a negative
number - Do not attempt to compute a value larger than the
maximum a data type may represent
38Calculating a Monthly Interest Rate
- Enter the lines of code below and do not press
the ENTER key
39Intrinsic Functions
- Intrinsic functions are VB .NET built-in
functions - The pmt() function is in the financial category
of the run-time library functions Pmt( rate,
nper, pv, fv, due) - The function Pmt() requires a negative value for
the present value (pv) argument - The function returns a value of certain data type
40The Format Function
- The Format function is another intrinsic
function - strPayment Format(dblPayment, Currency)
- Currency is one of many predefined format name
41Finish btnComputePayment_Click
42Summary
- Use the RadioButton and GroupBox controls, and
Layout toolbar - Set a default button on a form, and lock controls
on a form - Declare and use variables and constants
- Describe data types and Convert between data
types - Use arithmetic expressions
- Code a form Load event procedure
- Describe the order of operator precedence in code
- Use the intrinsic function like Pmt, and Format
functions