210 likes | 393 Views
Clearly Visual Basic: Programming with Visual Basic 2010 2 nd Edition. Chapter 6 Where Can I Store This? (Variables and Constants). Objectives. After studying Chapter 6, you should be able to: Declare variables and named constants
E N D
Clearly Visual Basic: Programming with Visual Basic 2010 2nd Edition Chapter 6 Where Can I Store This? (Variables and Constants)
Objectives After studying Chapter 6, you should be able to: • 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 2010, 2nd Edition
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 2010, 2nd Edition
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 2010, 2nd Edition
Figure 6-2 Most commonly used numeric data types Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
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 2010, 2nd Edition
Let’s Play the Name Game (cont’d.) Figure 6-3 Data type IDs and examples of variable names Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
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 2010, 2nd Edition
You’ll Need a Reservation (cont’d.) Figure 6-4 Syntax and examples of the Dim statement Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
How Many Variables Should I Use? • Circle Area problem’s solution (Figure 6-5) • Utilizes two different variables • One to store the output item • Another to store the input item • Before coding the Circle Area application • View the Ch06-Circle Area video Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
How Many Variables Should I Use? (cont’d.) Figure 6-5 Circle Area problem’s solution Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
The TryParse Method • The TryParse Method • Every numeric data type in Visual Basic has one • 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 2010, 2nd Edition
Figure 6-8 Basic syntax and examples of the TryParse method Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
Check, Please…I’m Ready to Go • Next step is to desk-check the program • Desk-check table for a program • Contains 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 2010, 2nd Edition
Figure 6-10 Desk-check table for the Circle Area program Figure 6-11 Circle’s area shown in the interface Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
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 • Syntax shown in Figure 6-12 Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
Using Constants to Keep Things … Well, the Same (cont’d.) Figure 6-12 Syntax and examples of the Const statement Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
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 2010, 2nd Edition
Figure 6-13 Syntax and examples of formatting numeric output Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
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 2010, 2nd Edition
Summary (cont’d.) • 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 2010, 2nd Edition