Top Banner
# 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
33

Variables and Constants

Jan 03, 2016

Download

Documents

Regina Gilmore

Variables and Constants. 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?. Conventions—Use them!. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Variables and Constants

# 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

Variables and Constants

CS 105 Spring 2010

Page 2: Variables and Constants

# 2CS 105 Spring 2010

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…

Page 3: Variables and Constants

# 3CS 105 Spring 2010

Data -- VariablesData -- 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

Page 4: Variables and Constants

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

Page 5: Variables and Constants

# 5CS 105 Spring 2010

Variables

Dim intNum as IntegerintNum = 99

Range("B1").Value = intNum

RAM

intNum 0 99

99

Page 6: Variables and Constants

# 6CS 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").ValueRange("C1").Value = Range("A1").Value

Page 7: Variables and Constants

# 7CS 105 Spring 2010

Why we use variables

• We can introduce a user-defined variable called vntTemp and solve the following by

vntTemp = Range("A1").ValueRange("A1").Value = Range("C1").ValueRange("C1").Value = vntTemp

Page 8: Variables and Constants

# 8CS 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 VariantDim intNum as Integer

Page 9: Variables and Constants

# 9CS 105 Spring 2010

What’s in a name?

Page 10: Variables and Constants

# 10CS 105 Spring 2010

Rules for Forming Names for Constants and Variables

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

Page 11: Variables and Constants

# 11CS 105 Spring 2010

Proper Naming is a MUST

Proper Naming is a MUST

Programmers make mistakes, thereforewe require students to use Option Explicitand correct naming You find this on theTools Menu/Options.

With Option Explicityou must use Dim and Const

Page 12: Variables and Constants

# 12CS 105 Spring 2010

Variables used in debugging code

• Variables hold values that you can check while the code is stepping through…

Page 13: Variables and Constants

# 13CS 105 Spring 2010

Intrinsic ConstantsIntrinsic Constants

• They are built-in:

vbWhitevbMagenta

Page 14: Variables and Constants

# 14CS 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.

Page 15: Variables and Constants

# 15CS 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 89.897097 it will store only 89.8971

Page 16: Variables and Constants

# 16CS 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

Page 17: Variables and Constants

# 17CS 105 Spring 2010

Data Types 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?)

Page 18: Variables and Constants

# 18CS 105 Spring 2010

Data Types Data Types

• Double 15 significant digits (8 bytes)

dblMass

• Integer Whole numbers only up to 32,767 intIndex

• Long Larger whole numbers lngPopulation

Page 19: Variables and Constants

# 19CS 105 Spring 2010

More data types

• String 1 byte per character, letters, digits, and

other characters (569-00-8978)

strName

• Variant Holds everything, converts from

one type of data to another, but

takes a lot of space vntInput

(for example, user might enter '3' or 'three')

Page 20: Variables and Constants

# 20CS 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 ExplicitPrivate Sub cmdDisplay_Click()

Dim intNum As Integer

Range ("B1").Value = intNum intNum = 8754

End Sub

Page 21: Variables and Constants

# 21CS 105 Spring 2010

Error!

Dim intNum as Integer

Range ("B1").Value = intNum

intNum = 8754

RAM

intNum 0

0

Page 22: Variables and Constants

# 22CS 105 Spring 2010

Local Variable 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).

Page 23: Variables and Constants

# 23CS 105 Spring 2010

Local Variable

Private Sub cmdAdd_Click()Dim intTotal as IntegerintTotal = InputBox("Pick a

number")Range("A2").Value = intTotal

End Sub

Created here

Discarded after End Sub

Page 24: Variables and Constants

# 24CS 105 Spring 2010

What could be confusing here?

Private Sub cmdShow_Click()Dim intTotal as IntegerRange("A2").Value = intTotal

End Sub

Private Sub cmdReveal_Click()Dim intTotal as IntegerintTotal = InputBox(“Enter a

number")Range("A3").Value = intTotal

End Sub

Page 25: Variables and Constants

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

Page 26: Variables and Constants

# 26CS 105 Spring 2010

Errors we have seenReal life examples from CS105

Dim vntFirstName as VariantRange(“G33”).Value =

vntFirstName

Page 27: Variables and Constants

# 27CS 105 Spring 2010

Real life examples from CS105

Dim vntFirstName as VariantvntFirtName = Range(“G33”).Value

Page 28: Variables and Constants

# 28CS 105 Spring 2010

Real life examples from CS105

“I push the button but nothing happens”

vntMonth = Range(“B32”).Value

Page 29: Variables and Constants

# 29CS 105 Spring 2010

Entering a Danger Area

Page 30: Variables and Constants

# 30CS 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

Page 31: Variables and Constants

# 31CS 105 Spring 2010

What happens here?Dim mintTotal As IntegerPrivate Sub

cmdButtonAddOne_Click()

mintTotal = 1Range("A1").Value = mintTotal

End Sub

Private Sub cmdButtonMultiply_Click()

Dim intNumberTwo As IntegermintTotal = mintTotal * 2Range("C1").Value = mintTotal

End Sub

Page 32: Variables and Constants

# 32CS 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)

Page 33: Variables and Constants

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