Tutorial 3 1 Variable Memory location whose value can change as the program is running. Used to hold temporary information Used to control the type of data used in calculations Val returns a Double-type, which is often larger than necessary Can store only one piece of data at any time Data is processed faster
Variable. Memory location whose value can change as the program is running. Used to hold temporary information Used to control the type of data used in calculations Val returns a Double-type, which is often larger than necessary Can store only one piece of data at any time - PowerPoint PPT Presentation
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
Tutorial 3 1
Variable
Memory location whose value can change as the
program is running.
Used to hold temporary information
Used to control the type of data used in calculations
Val returns a Double-type, which is often larger than necessary
Can store only one piece of data at any time
Data is processed faster
Tutorial 3 2
Data Types
Byte
Boolean
Currency
Date
Double
Integer
Long
Object
Single
String
Variant
Tutorial 3 3
Use the Appropriate Data Type
Integer or Long - Used to store whole numbers
Single, Double, Currency - Used to store numbers with a decimal fraction
String - Used to store strings
Boolean - Used to store Boolean values (True and False)
Date - Used to store date and time information
Object - Used to store a reference to an object
Byte - Used to store binary data
Variant - Flexible, but not efficient
Tutorial 3 4
Variable Names
Should be meaningful
First three characters should represent the data type
Remainder of name should represent the variable’s purpose
Tutorial 3 5
Three-character Ids
Byte byt
Boolean bln
Currency cur
Date/Time dtm
Double dbl
Integer int
Long lng
Object obj
Singlesng
String str
Variant vnt
Tutorial 3 6
Rules for Naming Variables
Name must begin with a letter
Name can contain only letters, numbers, and the underscore. No punctuation characters or spaces are allowed
Name cannot exceeds 255 characters
Name cannot be a reserved word
Tutorial 3 7
Creating (declaring) a Variable
Dim variablename [As datatype]
Public variablename [As datatype]
Tutorial 3 8
Assigning Values to Variables
Assignment statement
variablename = value
Examples:
sngHours = 38.5
curBonus = curSales * .1
strName = “Susan”
Tutorial 3 9
ConstantsLiteral constant
an item of data whose value cannot change while the program is running
Examples: 7
“Janet”
Symbolic constanta memory location whose contents cannot be changed while the program is running
Examples: conPi
conRate
Tutorial 3 10
Scope of a VariableIndicates which procedures can use the variable
Determined by where the Dim or Public statement is entered
Can be either global, form-level, or local
Tutorial 3 11
Local Variables
Created with the Dim statement
The Dim statement is entered in an object’s event procedure
Only the procedure in which it is declared can use the variable
Removed from memory when the procedure ends
Tutorial 3 12
Form-level Variables
Created with the Dim statement
The Dim statement is entered in a form’s General declarations section
Can be used by any of the procedures in the form
Removed from memory when the application ends
Tutorial 3 13
Global VariablesCreated with the Public statement
The Public statement is entered in a code module’s General declarations section
Used in multi-form projects and can be used by any of the procedures in any of the project’s forms
Removed from memory when the application ends
Tutorial 3 14
Option Explicit Statement
Doesn’t allow you to create variables “on the fly”
Enter in every form’s, and every code module’s, General declarations section
Use Tools, Options, Environment tab, Require Variable Declaration to have Visual Basic include Option Explicit in every new form and module
Tutorial 3 15
Creating a Symbolic Constant
A memory location whose value cannot change during run time
Top, Left, Height, and Width properties are measured in twips
One twip is 1/1440 of an inch.
Tutorial 3 30
Timer ControlProcesses code at regular intervals
Interval property
Measured in milliseconds
A millisecond is 1/1000 of a second
Timer event
Contains the code that will be processed when each interval has elapsed
Tutorial 3 31
Removing a Coded Control
Remove all of the control’s code before removing the control
Unassociated code remains in the application
Look in the form’s General declarations section to verify that the application does not contain any unassociated code
Tutorial 3 32
Appearance of the Mouse PointerControlled by the object’s MousePointer property
Use either an hourglass or an arrow/hourglass to indicate that the application is busy
The hourglass indicates that the mouse pointer is temporarily inactive, whereas the arrow/hourglass indicates that the mouse pointer still can be used in the current application
Tutorial 3 33
Debugging TechniqueAlways enter the Option Explicit statement in the General declarations of every form and module
If your application uses the InputBox function, test your application to see how it handles the various InputBox responses
When using the Val function, remember that Visual Basic must be able to interpret the string expression as a numeric value