330 likes | 584 Views
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
E N D
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 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
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
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
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
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
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
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
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) Clearly Visual Basic: Programming with Visual Basic 2008
Dim Area as a DoubleDim PI as a Constant- DoubleDim Radius as a double Using TryParse-change the Radius to a numeric variableDouble.TryParse(txtRadius.Text, dblRadius)
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
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
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
Try entering 6.5 for the RadiusThis is what your results should look like: 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
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 • 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 • 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 • 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 • Which formats dblDue with dollar sign and 2 decimal places?A. lblDue.text = dblDue.ToString(“C2”)B. lblDue.Text = ToSting(dblDue, “C2”)
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 Clearly Visual Basic: Programming with Visual Basic 2008
Finding the area, and number of pizza slices from a given Radius Area = Pi * R^2
Open SUN Project and Dimension decHours, decPayRate, decGross, decFwt, decFica, decState, decNet As Decimal
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 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 Clearly Visual Basic: Programming with Visual Basic 2008