CHAPTER THREE Representing Data: Constants and Variables.

Slides:



Advertisements
Similar presentations
Working with Intrinsic Controls and ActiveX Controls
Advertisements

Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Chapter 3: Using Variables and Constants Programming with Microsoft Visual Basic 2005, Third Edition.
Lecture Set 4 Data Types and Variables Part B – Variables, Constants, Expressions Conversion Rules Options Strict, Option Explicit Scope of Definition.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
The Web Warrior Guide to Web Design Technologies
1.
VBA Modules, Functions, Variables, and Constants
Visual Basic.NET Fundamentals. Objectives Use primitive data types, naming conventions, and style rules Understand and apply the control structures supported.
Using Objects and Properties
IS 1181 IS 118 Introduction to Development Tools VB Chapter 04.
Data Types and Operations Programming Fundamentals (Writing Code)Programming Fundamentals (Writing Code)
1 Pertemuan 03 Representing Data Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
McGraw-Hill/Irwin The Interactive Computing Series © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft Excel 2002 Exploring Formulas.
Visual Basic Chapter 1 Mr. Wangler.
Variables and Constants
Chapter 3: Using Variables and Constants
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Object Variables Visual Basic for Applications 3.
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 6 Multiple Forms.
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
© 2006 Lawrenceville Press Slide 1 Chapter 3 Visual Basic Interface.
CHAPTER THREE Representing Data: Constants and Variables.
Numeric Variables Visual Basic for Applications 6.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
 Application – another name for a program.  Interface – is what appears on the screen when the application is running.  Program Code – is instructions.
Representing Data: Constants and Variables CHAPTER THREE Matakuliah: T0063 – Pemrograman Visual Tahun: 2009.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
Introduction to Programming with RAPTOR
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Chapter 3 w Variables, constants, and calculations DIM statements - declaration temporary memory locations identifier, data type, scope data types - values.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
Variables and Functions ROBOTC Software © 2012 Project Lead The Way, Inc.Principles of Engineering.
Programming with Microsoft Visual Basic th Edition
Tutorial 3: Using Variables and Constants1 Tutorial 3 Using Variables and Constants.
1 Writing Software Kashef Mughal. 2 Algorithms  The term algorithm (pronounced AL-go-rith-um) is a procedure or formula for solving a problem.  An Algorithm.
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
Controlling Program Flow with Decision Structures.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
CHAPTER FOUR Performing Calculations and Manipulating Data: Expressions.
Making Interactive Programs with Visual Basic .NET
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
Programming with Microsoft Visual Basic 2012 Chapter 3: Using Variables and Constants.
Representing Data: Constants and Variables
Microsoft Visual Basic 2010 CHAPTER FOUR Variables and Arithmetic Operations.
Visual Basic.NET Windows Programming
Egyptian Language School Computer Department
A variable is a name for a value stored in memory.
Chapter 6 JavaScript: Introduction to Scripting
Variables and Arithmetic Operations
Chapter 3: Using Variables and Constants
Variables and Arithmetic Operations
Microsoft Visual Basic 2005 BASICS
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
CIS16 Application Development Programming with Visual Basic
CHAPTER FOUR VARIABLES AND CONSTANTS
Chapter 2: Introduction to C++.
Presentation transcript:

CHAPTER THREE Representing Data: Constants and Variables

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Chapter Introduction 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 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 Data Categorization 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 Constants 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 Constants (cont.) 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 Writing Numeric Literal Constants –Ex E+7 Writing String Literal Constants –Ex. “Hello Jean” Symbolic Constants –INTERESTRATE represents 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.) The Constant Definition Statement –Ex. Const INTERESTRATE = 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.) 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 Literal Constants (cont.) Predefined Symbolic 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 Variables FleetSize WageRate AverageAge MaximumCapacity NumberOfSeminar Participants EmployeeName NumBidUnits YtdEarnings EmployeeNumber ExtendedPrice Depreciation X

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Standard Data Types Number of Bytes –Main memory occupied by a variable. Range –Largest and smallest values that can be stored in a numeric variable of a given type.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Standard Data Types (cont.) Precision –Indicates how close together two numeric values can be. Speed of Arithmetic Calculation –Differs for the different data types.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Choosing the Best Data Type for a Variable Use decision rules –Ex. Boolean is the best type for a variable that may be true or false. –Ex. Decimal is the best type for a dollar amount.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Declaring Variables: The Dim Statement A variable declaration statement. –Examples: Dim StreetAddress As String Dim GrossWeight As Integer Dim HomePhone As String Dim NetIncome As Decimal Dim CurrentStudent As Boolean

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Using Variables: The Assignment Statement Syntax of the Assignment Statement –variablename = expression Ex. CourseCode = “CISB119” Run Time: The Effect of the Assignment Statement –Evaluates expression on right side of equal sign. –Stores value of expression in variable on left side of equal sign.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Using Variables: The Assignment Statement (cont.) Run Time: How the Computer Evaluates Expressions –Computer determines the identity of each component of the expression. –Computer performs the indicated operations to calculate a value.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Using Variables: The Assignment Statement (cont.) Changing Variable Values during Execution –Storing a value in a variable will overwrite any existing value. Assignment Statements with Strings –To store the result of string manipulations in string variables.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Using Variables: The Assignment Statement (cont.) The Type Mismatch Error –Trying to store string data in a numeric variable. The Try/Catch Block –Used to detect and handle errors that are encountered during run time.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Using Variables: The Assignment Statement (cont.) Control Properties in Assignment Statements –Ex. lblHomePrice.Text = Why Use Variables –Ideal to store results of intermediate calculations. –Values stored in variables may be retrieved and formatted.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Option Explicit Removes the requirement to declare all variables. Highly recommended that this option is ON. Helpful in reducing typographical errors.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved The Windows Form Control Background of our user interface. Organizes a project.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Appearance and Use Multiple forms may be used for large projects. –Each form should represent an objective. –Each form should be clear and attractive. Each form is a user interface window during run time. All forms have the same basic components.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Properties of the Form Control (Name) AcceptButton BackColor CancelButton ControlBox Font MaximizeBox MinimizeBox Text

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Events Most common for a form are the Activate and Load events. Only one form can be activate at any given time. The user interacts with the active form. An Activate event occurs when the user switches forms. A Load event occurs each time a form is loaded.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Forms and Main Memory In large projects the developer must manage forms to conserve RAM at run time. Forms must be Loaded and Unloaded.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Run Time Speed Transferring data or code between storage and RAM slows down processing speed. Loading and unloading of forms should be minimized.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved 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 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 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 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 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 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 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 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 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 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 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 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 = 7

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved 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 do change during program execution.

McGraw-Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved 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 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.