32
Variables in VB Keeping Track

Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

  • View
    221

  • Download
    4

Embed Size (px)

Citation preview

Page 1: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

Variables in VB

Keeping Track

Page 2: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

VariablesVariables

• Variables are named places in the computer memory that hold information.

• Variables hold only a single value at a time. Assigning a new value to them causes the old value to disappear.

• Values are assigned to a variable using an equals sign

A = 5

assigns the value 5 to the variable A

Page 3: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

What Variables AreWhat Variables Are

• Variables are named locations in memory.

• We could use 1’s and 0’s.

• Text Names are Easier

011001100010011010010010011001100010011010010010

oror

Grade_AverageGrade_Average

8787 8787

Page 4: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

Memory with Variables

000001010011100101110111

001010

011

100101

110111

000

Not UsedNot UsedStringStringIntegerIntegerSingleSingle

TypeType

Page 5: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

Looking at ContentsLooking at Contents

000001010011100101110111

001010

011

100101

110111

000

System MemorySystem Memory

00000000

010111fleeb

65

AddressAddressNameName

ValueValue

Type: IntegerType: Integer

01000001ContentsContents

Page 6: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

Variable NamesVariable Names

• Consist of letters, numbers and a few special characters.– They must begin with a letter.

– They can’t have more than 40 letters

– They can’t have an embedded period - this differentiates them from properties.

– They can’t be names already in use.• Dim True False

Page 7: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

Example NamesExample Names

Acceptable:Fred

Fred_Flintstone

Dimensional

Times7

Unacceptable:Dim

7times

Here.There

There+Goes

Page 8: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

What does 1000001 mean?What does 1000001 mean?

• 65?

• “A” ?

• Nothing?

• Context is the difference between data and information.

• Variable Types give us context.

Page 9: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

What are Variable Types?What are Variable Types?

• Context used to interpret data.• A variable of type Integer holding

1000001 is 65 because we view it as a binary number.

• A variable of type String holding 1000001 is an “A” because we view it as an ASCII character.

Page 10: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

ASCIIASCII

• Code used to represent characters.

• UNICODE is now being promoted as a replacement.

• ASCII does English (and Romance languages)

• UNICODE does all languages

Page 11: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

Question:Question:

• We know the binary value 10010010 in a byte is:– A: The number 146

– B: The number 152

– C: The number 15.3

– D: The character ‘C’

– E: None of the Above

Page 12: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

Variables in Visual BasicVariables in Visual Basic

• String Widely used.• Integer Widely used.• Single Widely used.• Long Frequently used.• Double Occasionally used.• Currency Occasionally used.• Variant Not to be used.

Page 13: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

StringsStrings

• Hold character data

• Cannot be used for calculations.

• Are limited to between zero and approximately 2 billion letters.

• Use one byte per character plus 10 bytes for the string.

Page 14: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

IntegerInteger

• Holds whole numbers (e.g. 1,2,3,4.....)

• Cannot hold fractional values (e.g. 3.2)

• Is limited to values between -32,768 and 32,767

• Uses 2 bytes

Page 15: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

Single• “Single Precision Floating Point Number”

• Holds numbers with a decimal part

• Actually holds an approximation. – Difference is irrelevant for most purposes

• Is limited to values between– -3.402E38 to -1.401E-45 for negative numbers

– 1.401E-45 to 3.403E38 for positive numbers.

• Uses 4 bytes.

Page 16: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

QuestionQuestion

• The number 8.5 can be stored in:A. An Integer

B. A 35 gallon cooler, provided there is enough ice to prevent spoilage.

C. A Single

Page 17: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

DoubleDouble

• “Double Precision Floating Point Number”• Larger and More Precise Than Single • Operations with these variables are much slower

than with single precision.• Is limited to values between

– -1.798E308 to -4.9407E-324 for negatives

– 4.9407E-324 to 1.7977E308 for positive

• Uses 8 bytes

Page 18: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

Long Long

• Long integers are used for storing larger integer values.

• Is limited to whole numbers from -2,147,483,648 to 2,147,483,647

• Uses 4 bytes.

Page 19: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

CurrencyCurrency

• Used to hold amounts of money.– A very long integer that has been shifted four decimal

places to the right.

– Gives 4 decimal places of accuracy.

– Is limited to numbers with 4 decimal places of accuracy.

• Is limited to values between– -922,337,203,685,477.5808 and

922,337,203,685,477.5807

Page 20: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

QuestionQuestion

• The number 98,943 can be stored in:– A. An Integer

– B. A Single

– C. A Double

– D. A Long

– E. Currency

Page 21: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

VariantVariant

• This variable can become any of the other types, depending on the value assigned to it.

• We try not to use these because they can cause problems.

Page 22: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

Get Explicit!Get Explicit!

• Put Option Explicit in your code from the editor.

• Causes an error at compile time if you haven’t declared a variable.

Page 23: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

Declaration of VariablesDeclaration of Variables

• The computer should know about variables before we use them.

• We do this using the declaration statement:

Dim Fred As Single

• This declares one variable called Fred of type Single

Page 24: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

More Declarations

• Dim Mary as Integer

• Dim Profit as Currency

• Dim FirstName as String

Page 25: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

QuestionQuestion

• How many bytes does a variable of type single take?– A. 1

– B. 2

– C. 3

– D. 4

– E. 8

Page 26: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

Object Variables

• A special kind of variable that holds portions of the Object Model.

• Workbooks, Worksheets, and Ranges are all examples

• Object variables have properties, identified by the period operator.

Page 27: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

The Range Type

• The Range is an Object Variable

• Ranges have:– Value

– Formula

– Count

– Interior

Page 28: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

Declaring a Range

• Same as other variables:

• Dim r As Range

• Dim GivenRange as Range

Page 29: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

Range Example

• Changing the Range’s Value:– GivenRange.Value = 5

• Copying the value from one Range to another:OldRange.Value = NewRange.Value

Page 30: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

Getting a Range

•Can be passed into a function.–Function Fleeb (R as Range)

–user gives us R as a Range and we can refer to it.

•We can determine it ourselvesDim NewR as Range

Set NewR = Worksheets(“S1”).Range(“A1”)

Page 31: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

Worksheets

• Worksheet is another type of object variable.

• Worksheets have:– Range

– Cells

• ActiveSheet is the sheet that is currently selected.

Page 32: Variables in VB Keeping Track. Variables Variables are named places in the computer memory that hold information. Variables hold only a single value at

Using Worksheets

• Getting a cell from a Worksheet– Worksheet(“Sheet 1”).Cells(1,2)

• Getting a cell from the ActiveSheet– ActiveSheet.Cells(1,4)