Visual Basic 6 Variables
Visual Basic 6 Variables
Variables-Operations
May 2002
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
Can Store only one piece of data at any time
Data is processed faster
Data Types
Integer - Whole numbers - 2 bytes - +/- 32,767
Long - Whole numbers - 4 bytes - +/- 2 billion
Single - Decimal - 4 bytes - +/- 1E-45 to 2E38
Double - Decimal - 8 bytes - +/- 5E-324 to 1.8E308
Currency - 15 left, 4 right of decimal - 8 bytes - +/- 9E14
String - Text - 1 byte per character - 65,400 chars for fixed strings, 2
billion for dynamic strings
Byte - Whole numbers - 1 byte – 0 to 255
Boolean - Logical values - 2 bytes - True or false
Date - Date and time - 8 bytes - 1/1/100 to 12/31/9999
Object - OLE objects - 4 bytes - N/A
Variant - Any preceding type - 16 bytes+1 byte per character - N/A
Use of 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
Variable Names
Can be no longer than 255 characters
Can not be a reserved word
Should be meaningful
First three characters should represent the
data type or should start with a letter
Remainder of name should represent the
variable’s purpose. No spaces, periods, or
other punctuation characters are allowed
Three-Character Ids
Byte byt Long lng
Boolean bln Object obj
Currency cur
Single sng
String str
Date/Time dtm
Variant vnt
Double dbl
Integer int
Variable Declaration
Dim variablename [As datatype] [,
variablename2 [As datatype2]]
Public variablename [As datatype]
Private variablename [As datatype]
Static variablename [As datatype]
Assigning Values to Variables
Assignment statement
variablename = value
Examples:
sngHours = 38.5
curBonus = curSales * .1
strName = “Susan”
Scope of a Variable
Indicates which procedures can use the
variable
Dim x (1 to 4) As Currency
Dim xtotal As Currency
xtotal=0
For xcounter = 1 to 4
xtotal=xtotal+x(xcounter)
Next xcounter
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, Editor tab, Require
Variable Declaration to have Visual Basic
include Option Explicit in every new form and
module
If you fail to define a variable you will receive an
error message “Variable not defined”
Constants
Literal constant
an item of data whose value cannot change
while the program is running
7
“Janet”
Symbolic constant
a memory location whose contents cannot
be changed while the program is running
conPi
conRate
Creating a Symbolic Constant
A memory location whose value cannot
change during run time
Syntax: [Public] Const constname [As
datatype] = expression
Examples:
Const conPi As Single = 3.141593
Public Const conMaxAge as Integer = 65
Scope of a Symbolic Constant
Indicates which procedures can use the
symbolic constant
Global: Public Const statement in a code
module’s General declarations section
Form-level: Const statement in the form’s
General declarations section
Local: Const statement in an event procedure
Assignment Statements
IntNumStudents=25
numeric literal
StrTopStudent=“June”
string literal
IntAvgScore=TotScore/NumStudents
mathematical expression
StrSpouseName=“Mrs. ” & “Tina Fortner”
string expression
StrCapitalName=Ucase$(“Chris”)
return value of a function
Operator Order of Precedence
^ exponentiation
- negation
*, / multiplication and division
\ integer division
Mod modulus arithmetic
+, - addition and subtraction
Order is from left to right for same order or
precedence. Parentheses can be used to
override the order or precedence.
Function
A predefined Visual Basic procedure
Results:
Hello Mary
Syntax: Len(textbox.Text)
SelStart Property of a Text Box
textbox.SelStart = 0
textbox.SelLength = Len(textbox.Text)