Scope of Variables and Constants A Variable or Constant may exist and be Visible for an entire project, for only one form, or for only one procedure Therefore,

Slides:



Advertisements
Similar presentations
Chapter 6 Multiform Projects Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Advertisements

Visual Basic Project Files:.VBP file: Project File: a small text file that contains the names of other files in the project, as well as some information.
Writing General Procedures Often you will encounter programming situations in which multiple procedures perform the same operation This condition can occur.
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 7 Multiple Forms, Modules,
VBA Modules, Functions, Variables, and Constants
Using Visual Basic 6.0 to Create Web-Based Database Applications
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1.
CVEV 118/698 Visual Basic Lecture 1 Prof. Mounir Mabsout Expert 1: Elsa Sulukdjian Expert 2: Walid El Asmar.
Creating Object Oriented Programs Object oriented (OO) terminology Class vs. object Instantiate an object in a project Understand Class_Initialize / Terminate.
Slide 1 Variables, Constants and Data Types. Slide 2 Variables v Three components define a variable: –Name (memory location) –Type –Information (value)
Scope of Variables and Constants A Variable or Constant may exist and be Visible for an entire project, for only one form, or for only one procedure Therefore,
Data Types and Operations Programming Fundamentals (Writing Code)Programming Fundamentals (Writing Code)
Lec2 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 2 Back to Index v Basic Data Types v Arithmetic.
VB Code Statements 3 types of VB statement The Remark statement, known as comments, are used for project documentation only Begin with an apostrophe Not.
Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.
To type the VB code behind the command button (named cmdPush), Double-Click on the Push Me (caption) command button As a result the Visual Basic Code Window.
McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 3 Variables, Constants,
Copyright © 2014 Pearson Education, Inc. Chapter 7 Multiple Forms, Modules, and Menus.
5.05 Apply Looping Structures
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #4: Working with Variables and User Interfaces IE 212: Computational Methods for Industrial Engineering.
McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 6 Multiple Forms.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Multiple Forms, Standard Modules, And Menus
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
Multiple Forms and Standard Modules
Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes.
Using Arrays and File Handling
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
Microsoft Access Using Visual Basic Routines. Visual Basic Datatypes Boolean Byte Currency Date Double Integer Long Object Single String Variant Hyperlink.
Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops.
 Application – another name for a program.  Interface – is what appears on the screen when the application is running.  Program Code – is instructions.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to accompany prepared by James T. Perry University of San.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 6 Multiform Projects.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Input Validation Check the values entered into a text box before beginning any calculations Validation is a form of ‘self-protection’, rejecting bad data.
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.
Programming with Microsoft Visual Basic th Edition
Tutorial 3: Using Variables and Constants1 Tutorial 3 Using Variables and Constants.
6-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
Slide 1 Controls v Control naming convention –Label: lblName –Command Button: cmdName –Text Box: txtName.
Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to accompany prepared by James T. Perry University of San.
More on Variables and Subroutines. Introduction Discussion so far has dealt with self- contained subs. Subs can call other subs or functions. A module.
CSC 162 Visual Basic I Programming. Storage Classes Determines the “lifetime” of an identifier Types: –Automatic Default Memory is allocated for the variable.
Creating New Forms Projects can appear more professional when using different windows for different types of information. Select Add Windows Form from.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 6 Looping and Multiple Forms.
Chapter 7 Multiple Forms, Modules, and Menus. Section 7.2 MODULES A module contains code—declarations and procedures—that are used by other files in a.
Multiple forms - SDI & MDI Please use speaker notes for additional information!
Chapter 8 Multiple Forms, Modules, and Menus. Introduction This chapter demonstrates how to: – Add multiple forms to a project – Create a module to hold.
Multiple Forms and Menus
PictureBox, MessageBox, Multiple Forms, Splash Screens and Timers
A variable is a name for a value stored in memory.
Working with Forms in Visual Basic
Using Procedures and Exception Handling
Variables and Arithmetic Operations
Chapter 6 Multiform Projects
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
Multi-Form Applications Things You Need to Know
Tonga Institute of Higher Education
3.2 Working with Data Scope of variables 29/07/2019.
Presentation transcript:

Scope of Variables and Constants A Variable or Constant may exist and be Visible for an entire project, for only one form, or for only one procedure Therefore, the visibility of a variable or constant is referred to as its Scope Visibility means that “can this variable be seen” in this location The scope of a variable or constant is said to be Global, Module (Form) Level, or Local

Module Level variables or constants are accessible from all procedures of a Form A Local variable or constant may be used only within the procedure in which it is declared The Scope of a Variable declared with a DIM statement is determined by where the declaration statement is made The Lifetime of a Variable is the period of time that the variable exists The Lifetime of a Local variable is normally one execution of the procedure The Lifetime of a Module Level variable is the entire time the Form is Loaded (generally the lifetime of the entire project)

Local Declarations Any variable you declare inside a procedure The lifetime of the variable is one execution of the procedure Each time you execute (e.g. click on a command button) a procedure, the local DIM statements are executed. Each variable is created as a “fresh” new one with an initial value of 0 for numeric datatypes or “” for string When the procedure finishes, its variables disappear, their memory locations are released

Local Declarations

Module-Level Declarations When you need to use a variable or constant in more than one procedure of a form Declare at module-level [General] [Declarations]

Module-Level Declarations

Global Variables Though you can declare global variables anywhere, convention dictates that public variables be placed only in the standard code module. Examples of global and module level declarations in a SCM: –Public gcurTotalSalary as Currency 'global variable –Public Const gcurTAX_RATE as Single =.082 'global constant –Dim mcurMySalary as Currecy'module-only variable If a procedure were to contain a variable declared locally whose name is the same as a global variable (a violation of unwritten convention for variable naming), then the local variable takes precedence. A local variable, when in scope, always preempts use of a global variable. The Private statement is rarely used, because that is the default (for “Dim”)

Static Variables Static variables are local to a procedure but retain their value for the life of a project. They are often used inside a procedure that counts things and must maintain that count. Static variables are initialized once only. Thereafter they are not initialized during the project: –Private Sub Something() – Static intCount as Integer 'initialized to 0 once – intCount = intCount + 1 'remembered across invocations – ··· –End Sub

Standard Code Modules Public procedures are“visible” to all forms Public variables are visible to all forms SCM has the extension.BAS Create SCM: Project, Add Module DIM variables in the code module are visible to all procedures in the module, but not to procedures in the form modules. SCM modules do not have any event procedures because SCM has no events to which it can react. The SCM only has code and procedures.

Identifiers When you use variables and constants, it is important that you know their scope. Good programming practice to include scope information in naming conventions Use ‘m’ as a prefix to identify module-level declarations Use ‘g’ to identify variables with global scope

Multi-Form Projects Working with more than one form in a project The first form that VB displays is called the STARTUP FORM To add another form; Project Menu/Add Form or select the icon from the shortcut toolbar This form will be added to the project, and cam be viewed from the Project Explorer Window

Each form is a separate entity, and the code exists and is related only to the specific form To move between each form: FORMNAME.SHOW FORMNAME.HIDE Form style can be 1 (modal) or the default value 0 (nonmodal)

Variables & Constants in Multi- form Projects Scope of variables: –Local: available inside a procedure –Static: inside procedure, but remembered –Module level: available anywhere in a form –Global: available across forms--anywhere Global variables declared with Public Variable prefix naming conventions: m for module, g for global Scope a variable as narrowly as possible

Referring to Other Forms’ Objects You can refer to txtName in another form called frmSummary this way: frmSummary!txtName = … or frmSummary!txtName.Font.Name =... This implies that control names are unique within a form but need not be unique across forms.

An About Box Acts like a Windows Help|About box Often displays information about the programmers, designers, and so on An about box is simply a modal form with an OK button and label boxes displaying information You can use VB’s About Dialog template

A Splash Screen Splash screen displays while product loads Create: Project, Add Form, then select Splash Screen Splash screen loads first instead of main form Place splash screen load statement in Sub Main procedure in Standard Code Module