The following will be discussed in this chapter Compose event procedures that perform more sophisticated tasks, focus specifically on data items, continue work with Visual Basic .NET project structure, determine the meaning of the term scope,...
CHAPTER THREE Representing Data: Constants and Variables Chapter Introduction 3- • Compose event procedures that perform more sophisticated tasks • Focus specifically on data items • Continue work with Visual Basic NET project structure • Determine the meaning of the term scope McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved Objectives 3- • Differentiate between numeric and string data • Determine whether a data item should be a constant or variable • Code constants and variables in event procedures • Describe the characteristics and uses of standard data types • Create projects that consist of several forms • Explain scope and describe the domain of variables in procedures and forms McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 3.1 Data Categorization 3- • Two broad categories of data are numeric and string – Numeric data must contain only numbers – String data can contain any symbol – Numeric data is used in arithmetic calculations – String data cannot be used in calculations McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 3.2 Constants 3- • Data item whose value is assigned at design time and remains the same at run time • A literal constant is just a value • A symbolic constant is a descriptive name substituted for a literal constant McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 3.2 Constants (cont.) 3- • Four different kinds of constants: – Numeric literal – String literal – Numeric symbolic – String symbolic McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved Literal Constants 3- • Writing Numeric Literal Constants – Ex 3.45 +231 9.4E+7 • Writing String Literal Constants – Ex “Hello Jean” • Symbolic Constants – INTERESTRATE represents 0.045 • Creating/Choosing Symbolic Constant Names – Names are chosen by the developer – Naming rules must be adhered to McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved Literal Constants (cont.) 3- • The Constant Definition Statement – Ex Const INTERESTRATE = 0.045 • Run Time: How the Computer Uses Symbolic Constants – Stored in a reference table for later use McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved Literal Constants (cont.) 3- • Advantages of Using Symbolic Constants – Make program easier to understand – Reduce the chance of program inconsistencies McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved Literal Constants (cont.) • Literal versus Symbolic Constants – The null string (“”) and numeric data used in formulas should be the only literal constants • Typical Uses of Symbolic Constants – Prime interest rate – Overtime rate – Number of print lines for a printed page McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 310 Forms and Disk Storage • Forms are the main way VB organizes the storage of a project on disk • One file is saved for each form • A form file contains information about the form McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 329 Forms and Code Windows • Each form has its own Code window • Code window will show Name property of related form • Code window will show only the event procedures on the related form McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 330 Form as a Class • You create a class template by creating a form • Buttons and text boxes are added to the class definition • Events are methods added to the form class • Many properties and methods are inherited from the form’s superclass McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 331 3.6 Variable Scope • The domain within which a variable can be accessed • Set of all the code that can refer to a variable • Determined by where and how the variable is declared • There are four levels: block, procedure (local), module, and global McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 332 Block- and Procedure-Level Scope • Any variable declared inside a procedure has procedure-level scope • Variable can only be accessed by statements in that procedure • Scope can be narrowed to a single block of code within the procedure McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 333 Module-Level Variables • The scope is the set of all procedures associated with the form • Any statement in any procedure belonging to the form can access a module-level variable McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 334 Global Variables • Variables that can be shared across all forms have global scope – The Public Statement • Used to create a global variable – Modules • Repository for data that need to be shared by forms • Repository for global variables McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 335 Global Variables (cont.) – Hiding Global Variables • A procedure-level variable in a procedure “hides” a global variable with the same name • A module-level variable will “hides” a global variable with the same name McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 336 Global Variables (cont.) – Procedure-Level, Module-Level, and Global Scope • A procedure-level variable is declared in a procedure using a Dim statement • A module-level variable is declared in the declarations section of a form using a Dim statement • A global variable is declared in the declarations section of a module using the Public statement McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 337 Global Variables (cont.) • Project Structure – Project • Forms – General Declarations Section – Controls » Properties » Event Procedures » Methods • Code Modules – General Declarations Section McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 338 3.7 Variable Lifetime • Period of time a variable exists – Static Variables • Maintain the lifetime of a procedure-level variable beyond the termination of its procedure McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 339 3.8 Constant Scope • Symbolic constants have the same levels of scope as variables – Use the Public Const statement to create a global constant • Ex Public Const DAYSINWEEK = McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 340 Chapter Summary • The two basic kinds of data items are numeric and string • Data items are constants or variables • Constants cannot change during program execution • Constants are literal or symbolic • Variables are symbolic names for memory locations • Variables can or change during program execution McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 341 Chapter Summary (cont.) • Variables must be declared • Expressions are combinations of variables, constants, and operators that produce a value • An assignment statement is used to store values into a variable or control property • A project can have multiple forms, but Visual Basic NET allows only one active form at a time • The Show method activates a form McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 342 Chapter Summary (cont.) • The variable’s scope is the domain within which the variable can be accessed • The three levels of scope are procedure-level, module-level, and global • Variable lifetime refers to how long a variable exists McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 343 ... rights reserved 3. 1 Data Categorization 3- • Two broad categories of data are numeric and string – Numeric data must contain only numbers – String data can contain any symbol – Numeric data is used... rights reserved Literal Constants 3- • Writing Numeric Literal Constants – Ex 3. 45 + 231 9.4E+7 • Writing String Literal Constants – Ex “Hello Jean” • Symbolic Constants – INTERESTRATE represents... Constants – Visual Basic NET contains a large set – Contained in classes, such as the Math and Color classes McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 31 1 3. 3 Variables