Clearly Visual Basic: Programming with Visual Basic 2008

32
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 7 Where Can I Store This?

description

Clearly Visual Basic: Programming with Visual Basic 2008. Chapter 7 Where Can I Store This?. Design the following Interface, and program using methods learned in Chapter 6. Objectives. Declare variables and named constants Convert text to a numeric data type using the TryParse method - PowerPoint PPT Presentation

Transcript of Clearly Visual Basic: Programming with Visual Basic 2008

Page 1: Clearly Visual Basic: Programming with Visual Basic 2008

Clearly Visual Basic: Programming with Visual Basic 2008

Chapter 7 Where Can I Store This?

Page 2: Clearly Visual Basic: Programming with Visual Basic 2008

Clearly Visual Basic: Programming with Visual Basic 2008 2

Design the following Interface, and program using methods learned in Chapter 6

Page 3: Clearly Visual Basic: Programming with Visual Basic 2008

Objectives

• Declare variables and named constants

• Convert text to a numeric data type using the TryParse method

• Understand the scope and lifetime of variables

• Desk-check a program

• Format a program’s numeric output

Clearly Visual Basic: Programming with Visual Basic 2008 3

Page 4: Clearly Visual Basic: Programming with Visual Basic 2008

Using Storage Bins

• Internal memory – Composed of memory locations

• Memory location– Can hold only one item of data at a time

• To reserve a memory location:– Use a Visual Basic instruction that assigns both a

name and data type to the memory Location

• Memory locations are called variables

Clearly Visual Basic: Programming with Visual Basic 2008 4

Page 5: Clearly Visual Basic: Programming with Visual Basic 2008

So, What’s Your Type?

• Variables – Come in different types and sizes

• Item that a variable will accept for storage – Determined by the variable’s data type

• Decimal variables – Take twice as much room in memory as Double

variables

Clearly Visual Basic: Programming with Visual Basic 2008 5

Page 6: Clearly Visual Basic: Programming with Visual Basic 2008

Clearly Visual Basic: Programming with Visual Basic 2008 6

Page 7: Clearly Visual Basic: Programming with Visual Basic 2008

Let’s Play the Name Game

• Variable name– Should be descriptive– Should be meaningful right after you finish a

program and years later– No punctuation marks or spaces allowed– Cannot be a reserved word, such as Val

Clearly Visual Basic: Programming with Visual Basic 2008 7

Page 8: Clearly Visual Basic: Programming with Visual Basic 2008

Clearly Visual Basic: Programming with Visual Basic 2008 8

Page 9: Clearly Visual Basic: Programming with Visual Basic 2008

You’ll Need a Reservation

• Reserving a variable – Often referred to as declaring a variable

• Dim statement– Declares a variable in an event procedure

Clearly Visual Basic: Programming with Visual Basic 2008 9

Page 10: Clearly Visual Basic: Programming with Visual Basic 2008

Clearly Visual Basic: Programming with Visual Basic 2008 10

Page 11: Clearly Visual Basic: Programming with Visual Basic 2008

11

Mini QuizWhich of the 3 data types are appropriate for storing the number of disks purchased by a customer: a. Integer b. Decimal C. Double

Which of the following is not a valid name for a variable? A. decRate B. dblRate C. decRate_Of_Pay D. dblPay.rate

Write a Dim statement to declare a Double variable named dblHoursWorkedDim dblHoursWorked as Double

Page 12: Clearly Visual Basic: Programming with Visual Basic 2008

How Many Variables Should I Use?

• Circle Area problem’s solution (Figure 7-5)– Utilizes two different variables

• One to store the output item and the other to store the input item

• Before coding the Circle Area application– View the Ch07-Circle Area video

Clearly Visual Basic: Programming with Visual Basic 2008 12

Page 13: Clearly Visual Basic: Programming with Visual Basic 2008

Clearly Visual Basic: Programming with Visual Basic 2008 13

Page 14: Clearly Visual Basic: Programming with Visual Basic 2008

Clearly Visual Basic: Programming with Visual Basic 2008

The TryParse Method

• Every numeric data type in Visual Basic has:– A TryParse method that can be used to convert text

to that numeric data type

• Syntax of the TryParse method – dataType.TryParse(text, variable)

14

Page 15: Clearly Visual Basic: Programming with Visual Basic 2008

15

Page 16: Clearly Visual Basic: Programming with Visual Basic 2008

Clearly Visual Basic: Programming with Visual Basic 2008 16

Page 17: Clearly Visual Basic: Programming with Visual Basic 2008

Dim Area as a DoubleDim PI as a Constant- Double

Dim Radius as a double

Using TryParse-change the Radius to a numeric variableDouble.TryParse(txtRadius.Text, dblRadius)

Page 18: Clearly Visual Basic: Programming with Visual Basic 2008

Check, Please … I’m Ready to Go

• Desk-check table for a program – Will contain one column for each variable

• Desk-check the Circle Area program – Use radius values of 6.5 and 10

Clearly Visual Basic: Programming with Visual Basic 2008 18

Page 19: Clearly Visual Basic: Programming with Visual Basic 2008

Clearly Visual Basic: Programming with Visual Basic 2008 19

Page 20: Clearly Visual Basic: Programming with Visual Basic 2008

Using Constants to Keep Things … Well, the Same

• Named constant– Memory location whose value cannot change while

the application is running– Used to give names to constant values– Makes code more self-documenting and easier to

modify– Created using the Const statement

Clearly Visual Basic: Programming with Visual Basic 2008 20

Page 21: Clearly Visual Basic: Programming with Visual Basic 2008

Dressing Up the Output

• Formatting– Specifying number of decimal places and the special

characters to display in a number

• variable.ToString(formatString)– Formats a number

• string – Text enclosed in double quotation marks

Clearly Visual Basic: Programming with Visual Basic 2008 21

Page 22: Clearly Visual Basic: Programming with Visual Basic 2008

Try entering 6.5 for the RadiusThis is what your results should look like:

Clearly Visual Basic: Programming with Visual Basic 2008 22

Page 23: Clearly Visual Basic: Programming with Visual Basic 2008

23

Page 24: Clearly Visual Basic: Programming with Visual Basic 2008

Private Sub btnCalc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalc.Click ' calculates and displays the area of a circle

' declare named constant Const dblPI As Double = 3.141593

' declare variables Dim dblArea As Double Dim dblRadius As Double

' assign radius to a variable Double.TryParse(txtRadius.Text, dblRadius)

' calculate area dblArea = dblRadius * dblRadius * dblPI

' display area lblArea.Text = dblArea.ToString("N2")

End Sub

Page 25: Clearly Visual Basic: Programming with Visual Basic 2008

25

1. Which of the following declares a variable named intNumSold?A. Dim intNumSold As Integer B. Dim As Integer intNumSoldC. Const intNumSold As Integer D. None of the Above

2. If you enter A34 in a txtPrice textbox, what does the TryParse control assign to it? Decimal.TryParse(txtPrice.Text, decPrice)A. A B. A34 C. 0 D. 34

3. Which adds intScore and intScore2 and then multiplies the sum by 2, assigning the results to intTotal?A. intScore1 +intScore2 *2 = intTotal B. (intScore1 +intScore2)*2= intTotalC. intTotal = IntScore1 + intScore2 * 2 D. None of these

4. Which of the following statements declares the decRate as a constant set to .15? A. Con decRate as Decimal = .15B. Const decRate as Decimal = .15 C. Constant decRate as Decimal = .15 D. None of these

5. Which formats dblDue with dollar sign and 2 decimal places?A. lblDue.text = dblDue.ToString(“C2”)B. lblDue.Text = ToSting(dblDue, “C2”)

Page 26: Clearly Visual Basic: Programming with Visual Basic 2008

Clearly Visual Basic: Programming with Visual Basic 2008 26

Jackson College charges $1800 for room and board plus $100 per semester hour. The Cashier will enter the number of hours the student enrolls in. The output is Total Cost Assign the variable txtHours for the textBox using the TryParse command. Format the output as currency

Page 27: Clearly Visual Basic: Programming with Visual Basic 2008

Design and program the following Concert Revenue Calculator

Page 28: Clearly Visual Basic: Programming with Visual Basic 2008
Page 29: Clearly Visual Basic: Programming with Visual Basic 2008

Finding the area, and number of pizza slices from a given Radius

Area = Pi * R^2

Page 30: Clearly Visual Basic: Programming with Visual Basic 2008

Open SUN Project and Dimension decHours, decPayRate, decGross, decFwt, decFica, decState, decNet As Decimal

Page 31: Clearly Visual Basic: Programming with Visual Basic 2008

Clearly Visual Basic: Programming with Visual Basic 2008

Summary

• Internal memory of a computer– Composed of memory locations

• Memory location – Can store only one value at any one time

• Value in a variable – Can change as the application is running

• Integer data type– Stores integers

31

Page 32: Clearly Visual Basic: Programming with Visual Basic 2008

Clearly Visual Basic: Programming with Visual Basic 2008

Summary (continued)

• Dim statement – Reserves a procedure-level variable

• TryParse method– Converts text to numbers

• Const statement – Declares named constants

• ToString method– Converts a number to formatted text

32