1 CS 106 Computing Fundamentals II Chapter 28 “Scope” Herbert G. Mayer, PSU CS Status 7/14/2013 Initial content copied verbatim from CS 106 material developed.

Slides:



Advertisements
Similar presentations
VBA for MS Excel Hamze Msheik. Open the Visual Basic Editor in Excel 2007 Click on the Microsoft Office button in the top left of the Excel window and.
Advertisements

Arrays. What is an Array? An array is a way to structure multiple pieces of data of the same type and have them readily available for multiple operations.
1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
VBA Modules, Functions, Variables, and Constants
Sub Programs To Solve a Problem, First Make It Simpler.
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.
VB – Core III Functions Sub-routines Parameter passing Modules Scope Lifetime.
Adding Controls to User Forms. Adding Controls A user form isn’t much use without some controls We’re going to add controls and write code for them Note.
Introduction to VBA. This is not Introduction to Excel We’re going to assume you have a basic level of familiarity with Excel If you don’t, or you need.
1 CS 106 Computing Fundamentals II Chapter 16 “Naming In Excel” Herbert G. Mayer, PSU CS Status 7/6/2013 Initial content copied verbatim from CS 106 material.
ECE 103 Engineering Programming Chapter 10 Variables, AKA Objects Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103.
Ch 10: More on Variables and Subroutines CP212 Winter 2012.
1 CS 106 Computing Fundamentals II Chapter 17 “Introduction To VBA” Herbert G. Mayer, PSU CS status 6/30/2013 Initial content copied verbatim from CS 106.
Tutorial 11 Using and Writing Visual Basic for Applications Code
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
® Microsoft Access 2010 Tutorial 11 Using and Writing Visual Basic for Applications Code.
1 CS 106 Computing Fundamentals II Chapter 25 “Variables, Assignment Statement” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim.
1 CS 106 Computing Fundamentals II Chapter 29 “Creating A User Form” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim from CS 106.
1 CS 106 Computing Fundamentals II Chapter 7 “Showing Developer Tab” Herbert G. Mayer, PSU CS status 6/17/2013 Initial content copied verbatim from CS.
1 CS 106 Computing Fundamentals II Chapter 4 “Excel Basics for Mac” Herbert G. Mayer, PSU CS status 6/27/2013 Initial content copied verbatim from CS 106.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
1 CS 106 Computing Fundamentals II Chapter 34 “Conditionals In Excel” Herbert G. Mayer, PSU CS Status 7/17/2013 Initial content copied verbatim from CS.
1 CS 106 Computing Fundamentals II Chapter 61 “Loops” Herbert G. Mayer, PSU CS Status 7/29/2013 Initial content copied verbatim from CS 106 material developed.
1 CS 106 Computing Fundamentals II Chapter 32 “Boolean Expressions” Herbert G. Mayer, PSU CS Status 7/14/2013 Initial content copied verbatim from CS 106.
1 CS 106 Computing Fundamentals II Chapter 84 “Array Formulae” Herbert G. Mayer, PSU CS status 6/14/2013 Initial content copied verbatim from CS 106 material.
1 CS 106 Computing Fundamentals II Chapter 67 “Working With Files” Herbert G. Mayer, PSU CS status 6/24/2013 Initial content copied verbatim from CS 106.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
1 CS 106 Computing Fundamentals II Chapter 75 “Arrays” Herbert G. Mayer, PSU CS Status 7/31/2013 Initial content copied verbatim from CS 106 material developed.
ME 142 Engineering Computation I Using Subroutines Effectively.
1 CS 106 Computing Fundamentals II Chapter 23 “Controls And Events” Herbert G. Mayer, PSU CS Status 7/5/2013 Initial content copied verbatim from CS 106.
Controls and Events. The Next Step In the first module, we discussed general problem solving In this module, we’ll apply what we learned, from specification.
PSU CS 106 Computing Fundamentals II VB Declarations HM 5/4/2008.
Chapter 3 w Variables, constants, and calculations DIM statements - declaration temporary memory locations identifier, data type, scope data types - values.
A lesson approach © 2011 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft® Excel 2013.
ME 142 Engineering Computation I Using Subroutines Effectively.
# 1# 1 What is a variable that you create? What is a constant that you create? What is an intrinsic (built-in) constant? What variables are built in?
1 CS 106 Computing Fundamentals II Chapter 211 “Coding And Debugging” Herbert G. Mayer, PSU CS status 6/29/2013 Initial content copied verbatim from CS.
Programming with Microsoft Visual Basic th Edition
1 CS 106 Computing Fundamentals II Chapter 210 “Adding Controls to User Forms” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
1 CS 106 Computing Fundamentals II Chapter 6 “How To Zip & Unzip Files” Herbert G. Mayer, PSU CS status 6/17/2013 Initial content copied verbatim from.
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
1 CS 106 Computing Fundamentals II Chapter 42 “Sub Procedures And Functions” Herbert G. Mayer, PSU CS Status 8/5/2013 Initial content copied verbatim from.
Scope. Scope of Names In a small program written by one person, it is easy to be sure that each variable has a unique name In a large program, or one.
1 CS 106 Computing Fundamentals II Chapter 85 “Excel Tables” Herbert G. Mayer, PSU CS status 6/14/2013 Initial content copied verbatim from CS 106 material.
Lecture 7 Methods (functions and subroutines) Parameter Passing
CS 106 Computing Fundamentals II Chapter 5 “Excel Basics for Windows”
Spreadsheet-Based Decision Support Systems
CS 106 Computing Fundamentals II Chapter 35 “Controls For Choices”
CS 106 Computing Fundamentals II Chapter 77 “Algorithm”
CS 106 Computing Fundamentals II Chapter 33 “Conditional Statements”
Microsoft Access Illustrated
Microsoft Office Illustrated
Learning Excel Session 9 and 10 Dr. Chaitali Basu Mukherji.
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
CS 106 Computing Fundamentals II Chapter 71 “Indexing”
CS 106 Computing Fundamentals II Chapter 73 “Ranges”
CS 106 Computing Fundamentals II Chapter 66 “Working With Strings”
CS285 Introduction - Visual Basic
Herbert G. Mayer, PSU CS Status 8/2/2013
Tonga Institute of Higher Education
CS 106 Computing Fundamentals II Chapter 64 “For Loop Variations”
ECE 103 Engineering Programming Chapter 12 More C Statements
CS 106 Computing Fundamentals II Chapter 69 “Event Loop”
Chapter 8 - Functions and Functionality
Introduction to VB programming
3.2 Working with Data Scope of variables 29/07/2019.
10.2 Procedures Passing Parameters 30/08/2019.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Presentation transcript:

1 CS 106 Computing Fundamentals II Chapter 28 “Scope” Herbert G. Mayer, PSU CS Status 7/14/2013 Initial content copied verbatim from CS 106 material developed by CS professors: Cynthia Brown & Robert Martin

2 Syllabus Scope Scope Example Example Global vs. Local Global vs. Local Advice on Globals Advice on Globals

3 Scope of Names In a small program written by one person, it is easy to enforce each variable has a unique nameIn a small program written by one person, it is easy to enforce each variable has a unique name In a large program, one written by many people, it is not so easyIn a large program, one written by many people, it is not so easy In fact, we may want to be able to use the same name in different parts of a large program, without conflictsIn fact, we may want to be able to use the same name in different parts of a large program, without conflicts

4 Scope Scope is the domain of program text, over which a names is knownScope is the domain of program text, over which a names is known There are two different domains in VBAThere are two different domains in VBA One is named the global scope; that is the range of text of the whole module. This includes any number of subroutines and of functions The other scope is named local; that is just the range of one sub or one function, including formal parameters A global name is visible by default in any SUBA global name is visible by default in any SUB A global name x can be hidden in a SUB if it is re- declared locally by the name xA global name x can be hidden in a SUB if it is re- declared locally by the name x

5 Scope and Lifetime of Variables A global variable is visible in al functions and SUBs of a module, unless re-declared locally. A global lives as long as the program is runningA global variable is visible in al functions and SUBs of a module, unless re-declared locally. A global lives as long as the program is running A local variable exists only as long as the SUB or function is runningA local variable exists only as long as the SUB or function is running 5

6 Global vs Local: The Code Option Explicit '************************************************************************* ' Illustrate scope '************************************************************************* Dim varA As Double 'create a global variable Sub WorkBook_Open() varA = 0 varA = 0 End Sub Sub ChangeValues() Dim varB As Double ‘varB is local Dim varB As Double ‘varB is local varA = varA + 1 varA = varA + 1 varB = varB + 1 varB = varB + 1 Cells(1, 1).Value = varA Cells(1, 1).Value = varA Cells(1, 2).Value = varB Cells(1, 2).Value = varB End Sub 6 Each time you click the button, varA and varB are incremented This procedure sets varA to 0 when the workbook opens

7 What happens… varA exists as long as the workbook is open. Each time we add 1, its value increasesvarA exists as long as the workbook is open. Each time we add 1, its value increases varB is created anew each time we press the button, when procedure ChangeValues is called. VBA sets a new variable to 0, so each time the value we see, after adding 1, is 1varB is created anew each time we press the button, when procedure ChangeValues is called. VBA sets a new variable to 0, so each time the value we see, after adding 1, is 1 This illustrates the difference in lifetime between local and global variablesThis illustrates the difference in lifetime between local and global variables

8 Advice on Global Variables Use global variables very sparingly. A program with all variables global easily becomes confusing and error-proneUse global variables very sparingly. A program with all variables global easily becomes confusing and error-prone Use them for quantities that have to be used by different proceduresUse them for quantities that have to be used by different procedures If a quantity is only needed within a single procedure, use a local variable for itIf a quantity is only needed within a single procedure, use a local variable for it Give global variables names that are unique; don’t reuse the same name for a local variable!Give global variables names that are unique; don’t reuse the same name for a local variable! Global constants are fine; just give them unique namesGlobal constants are fine; just give them unique names 8