Download presentation
Presentation is loading. Please wait.
Published byJuniper Wade Modified over 8 years ago
1
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 7 Where Can I Store This?
2
Clearly Visual Basic: Programming with Visual Basic 20082 Design the following Interface, and program using methods learned in Chapter 6
3
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 20083
4
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 20084
5
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 20085
6
6
7
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 20087
8
8
9
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 20089
10
10
11
11 Mini Quiz Which 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 dblHoursWorked Dim dblHoursWorked as Double
12
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 200812
13
Clearly Visual Basic: Programming with Visual Basic 200813
14
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
15
15
16
Clearly Visual Basic: Programming with Visual Basic 200816
17
Dim Area as a Double Dim PI as a Constant- Double Dim Radius as a double Using TryParse-change the Radius to a numeric variable Double.TryParse(txtRadius.Text, dblRadius)
18
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 200818
19
Clearly Visual Basic: Programming with Visual Basic 200819
20
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 200820
21
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 200821
22
Try entering 6.5 for the Radius This is what your results should look like: Clearly Visual Basic: Programming with Visual Basic 200822
23
23
24
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
25
25 1.Which of the following declares a variable named intNumSold? A. Dim intNumSold As Integer B. Dim As Integer intNumSold C. 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= intTotal C. 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 =.15 B. 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”)
26
Clearly Visual Basic: Programming with Visual Basic 200826 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
27
Design and program the following Concert Revenue Calculator
29
Finding the area, and number of pizza slices from a given Radius Area = Pi * R^2
30
Open SUN Project and Dimension decHours, decPayRate, decGross, decFwt, decFica, decState, decNet As Decimal
31
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
32
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.