variables, constants and data types
DESCRIPTION
Variables, Constants and Data Types. Variables. Three components define a variable: Name (memory location) Type Information (value) Variable name must: Start with a letter No spaces, periods nor punctuation characters Unique (within its scope) 255 characters in length - PowerPoint PPT PresentationTRANSCRIPT
Slide 1
Variables, Constants and Data Types
Slide 2
Variables
Three components define a variable:– Name (memory location)– Type– Information (value)
Variable name must:– Start with a letter– No spaces, periods nor punctuation characters– Unique (within its scope)– 255 characters in length– Not a VB reserved word
Slide 3
Variables
Variable naming conventions:– Descriptive (code is easy to read)– Short as possible (easy to type)– Use a prefix for type (easy for programmer to know its
type) String s sName Integer n nAge (table 8.1, page 161)
Slide 4
Types of Variables
Integer 2 bytes -32768 to +32767 Long 4 +/-2 billion Single 4 +/-1E-45 to 4E38 Double 8 +/-5E-324 to 1.8E308 Currency 8 +/- 9E14 String1B/char 65000 fixed, 2 billion dynamic Byte 1 0 to 255 Boolean 2 True or False Date 8 1/1/100 to 12/31/999 Object 4 Varient 16B+1B/Char
Slide 5
Variable Declaration
If a variable is not declared, it is declared by default by VB as Variant– Waste memory resources– Type might be invalid for use with functions
It is good programming practice to declare variables
Slide 6
Explicit Declaration
Explicit declaration uses statements to define the names and types of variables– Dim sFname as String, sLname as String– Private sFname as String, sLname as String– Static sFname as String, sLname as String– Public sFname as String, sLname as String
Multiple variables with multiple types If type is not indicated, Variant will be used
Slide 7
Implicit Declaration
VB defines the variable the first time it is used Variable name is preceded with a special character:
– Integer %– Long &– Single !– Double #– Currency @– String $
Example:– nNumVal% = 0– sFirstName$ = “Ali”
Slide 8
Fixed-Length Strings
Strings in general are variable-length Fixed length strings are declared like:
– Dim sName As String * 25
If a shorter string is assigned, it is appended with blanks
If a longer string is assigned, it is truncated
Slide 9
Variable Array
Array: is a group of variables of the same type, sharing the same name
Example: Dim nScores (1 to 35) As Integer Use for loop with array
For nCounter = 1 to 20
nScores(nCounter) = 0
Next nCounter
Slide 10
Scope
By default variables are local to the procedures where they are created in (local variable)
Public variables: can be accessed anywhere at the code
Example:Public sUserName As String
(Defined in the General Declarations Section)
Using Public variables:– Hard to debug– Bad use of resources
Slide 11
Static Variables
Variables declared local to a procedure are discarded afterwards
To preserve a variable in side a procedure use Static
Static nPages As Integer Using Static with functions or sub treats all
variables inside as Static
Slide 12
Option Explicit
Setting “REqire Variable Declaration” option (Tools – Options – Editor)
It places Option Explicit in the general section has no effect on Forms/Modules created before
setting it
Slide 13
Constants
Cannot be modified Easy to remember then it value Avoid typing long strings Minimize program modifications
Examples:– Const vbActibeTitleBar = -2147483646– Const PI = 3.141592654 – Public Const PI = 3.141592654
Slide 14
Converting Data Types
VB provides several conversion functions you can use to convert values into a specific data type.
To convert a value to Currency, for example, you use the CCur function:
PayPerWeek = CCur(hours * hourlyPay)
Slide 15
The Empty Value
Sometimes you need to know if a value has ever been assigned to a created variable. A Variant variable has the Empty value before it is assigned a value. The Empty value is a special value different from 0, a zero-length string (""), or the Null value. You can test for the Empty value with the IsEmpty function:– If IsEmpty(Z) Then Z = 0
When a Variant contains the Empty value, you can use it in expressions; it is treated as either 0 or a zero-length string, depending on the expression.
The Empty value disappears as soon as any value (including 0, a zero-length string, or Null) is assigned to a Variant variable. You can set a Variant variable back to Empty by assigning the keyword Empty to the Variant.
Slide 16
The Null Value
The Variant data type can contain another special value: Null.
Null is commonly used in database applications to indicate unknown or missing data. Because of the way it is used in databases, Null has some unique characteristics:
Expressions involving Null always result in Null. Thus, Null is said to "propagate" through expressions; if any part of the expression evaluates to Null, the entire expression evaluates to Null.
Passing Null, a Variant containing Null, or an expression that evaluates to Null as an argument to most functions causes the function to return Null.
Slide 17
The Null Value
You can also assign Null with the Null keyword:– Z = Null
You can use the IsNull function to test if a Variant variable contains Null:
If IsNull(X) And IsNull(Y) Then Z = Null
Else Z = 0
End If
Slide 18
Built-in Constants
Intrinsic Constants Colors, keycodes, shapes Described in help Object Browser