# 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?

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.
Lecture Set 4 Data Types and Variables Part B – Variables, Constants, Expressions Conversion Rules Options Strict, Option Explicit Scope of Definition.
AE6382 VBA - Excel l VBA is Visual Basic for Applications l The goal is to demonstrate how VBA can be used to leverage the power of Excel u VBA syntax.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
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.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
VBA Modules, Functions, Variables, and Constants
CVEV 118/698 Visual Basic Lecture 1 Prof. Mounir Mabsout Expert 1: Elsa Sulukdjian Expert 2: Walid El Asmar.
CSI 101 Elements of Computing Spring 2009 Lecture #6: Data Types and Variables in Visual Basic Wednesday, Februrary 11th, 2009.
Slide 1 Variables, Constants and Data Types. Slide 2 Variables v Three components define a variable: –Name (memory location) –Type –Information (value)
IS437: Fall 2004 Instructor: Dr. Boris Jukic Data Types, Constants, Variables, Scope, Conversion.
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:
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.
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,
5.05 Apply Looping Structures
110-D1 Variables, Constants and Calculations(1) Chapter 3: we are familiar with VB IDE (building a form…) to make our applications more powerful, we need.
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.
Variables, Constants, Methods, and Calculations Chapter 3 - Review.
Variables and Constants
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
Tutorial 11 Using and Writing Visual Basic for Applications Code
CHAPTER THREE Representing Data: Constants and Variables.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
National Diploma Unit 4 Introduction to Software Development Data types, variables and constants.
Access VBA Programming for Beginners - Class 2 - by Patrick Lasu
Chapter 2: Using Data.
CPS120: Introduction to Computer Science
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures.
Chapter 3 P. 1 Data - Variables and Constants Definition A variable is a value which can change during program execution. A constant is a value which can.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
1 CS105 Discussion 5 – Variables and If Announcements MP 1 due on Monday Midterm 1 on Tuesday If you need a conflict, request it NOW!!
INT213-Week-2 Working with Variables. What is variable
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.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
Programming with Microsoft Visual Basic th Edition
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
Variables in VB.NET. Variables  A storage location in memory (RAM)  Holds data/information while the program is running  These storage locations can.
CPS120: Introduction to Computer Science Variables and Constants.
Slide 1 Controls v Control naming convention –Label: lblName –Command Button: cmdName –Text Box: txtName.
CHAPTER THREE Representing Data: Constants and Variables.
Controlling Program Flow with Decision Structures.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
CIS 338: VB Variables Dr. Ralph D. Westfall April, 2011.
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.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
Objects Allow us to represent and control the computer technology Use methods to make the object do something Use properties to find out and change the.
Representing Data: Constants and Variables
Egyptian Language School Computer Department
A variable is a name for a value stored in memory.
VBA - Excel VBA is Visual Basic for Applications
Data Types, Arithmetic Operations
2. Understanding VB Variables
Variables and Arithmetic Operations
Chapter 6 Variables What is VBScript?
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 1/15/2019.
CHAPTER FOUR VARIABLES AND CONSTANTS
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 7/9/2019.
Presentation transcript:

# 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? What is a data type? Variables and Constants CS 105 Spring 2010

# 2# 2 Conventions—Use them! You need to learn to use our conventions so your code will be easy to read and evaluate. NOTE: Important material on VBA is at the start of the VBA lecture slide section, so read it before you do your MP!! And now, onto variables…

# 3# 3 CS 105 Spring 2010 Data -- Variables Variables are memory locations that hold data that can change during the execution of the program. The properties of an object are variables that can change during a procedure, – except the Name property. For example, Enabled = True can be changed to Enabled = False

# 4# 4 CS 105 Spring 2010 The computer loads the workbook into memory (RAM), and a place in memory is assigned to each property of each object. What if cmdRun = enabled? The computer takes the value on the right side of the property box and stores it in the place in the computer’s memory named on the left side property box for cmdRun. Where are the properties stored in memory?

# 5# 5 CS 105 Spring 2010 Variables Dim intNum as Integer intNum = 99 Range("B1").Value = intNum RAM intNum

# 6# 6 CS 105 Spring 2010 Why do we use variables? What if we have a problem: We want to exchange the values in cells A1 and C1. If we do the following, it won’t work: Range("A1").Value = Range("C1").Value Range("C1").Value = Range("A1").Value

# 7# 7 CS 105 Spring 2010 Why we use variables We can introduce a user-defined variable called vntTemp and solve the following by vntTemp = Range("A1").Value Range("A1").Value = Range("C1").Value Range("C1").Value = vntTemp

# 8# 8 CS 105 Spring 2010 Creating a Variable You can name and create your own variables. Variant means the data can be used as text or as a number. Integer means it is a whole number. We will cover this in more depth later. Dim vntName as Variant Dim intNum as Integer

# 9# 9 CS 105 Spring 2010 What’s in a name?

# 10 CS 105 Spring 2010 Rules for Forming Names for Constants and Variables Names MUST have no spaces or periods –not conflict with reserved words (such as print, name, and value) Naming conventions – names should –be meaningful –prefix that indicates the type (and other things) –Constants: Please use UPPERCASE in CS105

# 11 CS 105 Spring 2010 Proper Naming is a MUST Programmers make mistakes, therefore we require students to use Option Explicit and correct naming You find this on the Tools Menu/Options. With Option Explicit you must use Dim and Const

# 12 CS 105 Spring 2010 Variables used in debugging code Variables hold values that you can check while the code is stepping through…

# 13 CS 105 Spring 2010 Intrinsic Constants They are built-in: vbWhite vbMagenta

# 14 CS 105 Spring 2010 Named Constants Const strCOMPANY As String = "Spyglass" Const curTAX_RATE As Currency =.08 Data items that remain the same are called constants. Constants remain the same until the programmer changes them. The user cannot alter them (you can only change them at one place in the code—where you declare them) You declare a constant identifier in VBA by giving its name, its data type, and its value.

# 15 CS 105 Spring 2010 Constants and Data Types Const curTAX_RATE As Currency =.08 Prefix of the name curTAX_RATE indicates the type and scope of the constant or variable Currency is a data type--it limits the size of the number stored to only four decimal places. If you enter it will store only

# 16 CS 105 Spring 2010 Variables and Constants use the same data types Dim strName as String Gives size of memory to reserve States the data type to expect

# 17 CS 105 Spring 2010 Data Types Data Types determine the storage space a program will use to store a constant or a variable. Some data types are: Boolean True or False blnAnswer Currency Large, but only four decimal places (8 bytes) curIncome Single 7 significant digits (4 bytes) sngNumber (what is the largest # it can hold?)

# 18 CS 105 Spring 2010 Data Types Double 15 significant digits (8 bytes) dblMass Integer Whole numbers only up to 32,767 intIndex Long Larger whole numbers lngPopulation

# 19 CS 105 Spring 2010 More data types String1 byte per character, letters, digits, and other characters ( ) strName VariantHolds everything, converts from one type of data to another, but takes a lot of space vntInput (for example, user might enter '3' or 'three')

# 20 CS 105 Spring 2010 Error! Programs execute code one line at a time, going from the top to the bottom. What is wrong with this code: Option Explicit Private Sub cmdDisplay_Click() Dim intNum As Integer Range ("B1").Value = intNum intNum = 8754 End Sub

# 21 CS 105 Spring 2010 Error! Dim intNum as Integer Range ("B1").Value = intNum intNum = 8754 RAM intNum 0 0

# 22 CS 105 Spring 2010 Local Variable A local variable may be used only in a single procedure. It is – declared in that procedure (a button click, for example) – only available to that procedure – used in that procedure, and then –discarded when that procedure finishes (the next button click creates it anew in a different part of memory).

# 23 CS 105 Spring 2010 Local Variable Private Sub cmdAdd_Click() Dim intTotal as Integer intTotal = InputBox("Pick a number") Range("A2").Value = intTotal End Sub Created here Discarded after End Sub

# 24 CS 105 Spring 2010 What could be confusing here? Private Sub cmdShow_Click() Dim intTotal as Integer Range("A2").Value = intTotal End Sub Private Sub cmdReveal_Click() Dim intTotal as Integer intTotal = InputBox(“Enter a number") Range("A3").Value = intTotal End Sub

# 25 CS 105 Spring 2010 How to translate MP language into our language From an MP: “Create a string variable to store the value of the month taken from cell C35.” How do we create a variable? What does “initialize a variable” mean?

# 26 CS 105 Spring 2010 Errors we have seen Real life examples from CS105 Dim vntFirstName as Variant Range(“G33”).Value = vntFirstName

# 27 CS 105 Spring 2010 Real life examples from CS105 Dim vntFirstName as Variant vntFirtName = Range(“G33”).Value

# 28 CS 105 Spring 2010 Real life examples from CS105 “I push the button but nothing happens” vntMonth = Range(“B32”).Value

# 29 CS 105 Spring 2010 Entering a Danger Area

# 30 CS 105 Spring 2010 Module-level variables A variable may be accessible to any procedures affiliated with a module – This is a module level variable and has the prefix "m". –Declared in General/Declarations

# 31 CS 105 Spring 2010 What happens here? Dim mintTotal As Integer Private Sub cmdButtonAddOne_Click() mintTotal = 1 Range("A1").Value = mintTotal End Sub Private Sub cmdButtonMultiply_Click() Dim intNumberTwo As Integer mintTotal = mintTotal * 2 Range("C1").Value = mintTotal End Sub

# 32 CS 105 Spring 2010 Danger of Module-level variables The problem with module-level variables is that they can be changed by more than one procedure. We use them sparingly! You will lose points if you don’t follow directions on MPs (use them only when you are told to)

# 33 CS 105 Spring 2010 To Summarize: 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? What is a data type?