1 / 21

Clearly Visual Basic: Programming with Visual Basic 2010 2 nd Edition

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

meir
Télécharger la présentation

Clearly Visual Basic: Programming with Visual Basic 2010 2 nd Edition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Clearly Visual Basic: Programming with Visual Basic 2010 2nd Edition Chapter 6 Where Can I Store This? (Variables and Constants)

  2. 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

  3. 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

  4. 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

  5. Figure 6-2 Most commonly used numeric data types Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. Figure 6-8 Basic syntax and examples of the TryParse method Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. Figure 6-13 Syntax and examples of formatting numeric output Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition

  20. 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

  21. 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

More Related