Module 3: Language and Syntax Enhancements
Module 3: Language and Syntax Enhancements
Syntax Enhancements
Overview
Data Types
Using Variables
Functions, Subroutines, and Properties
Exception Handling
Data Types
Value-Type Variables
Directly contain their data
Each has its own copy of data
Operations on one cannot affect another
Assignment creates a copy of the data
Reference-Type Variables
Store references to their data (known as objects)
Two reference variables can reference the same object
Operations on one can affect another
New Data Types
Integer Short
Long (32 bits, signed) Integer
(none) Long (64 bits, signed)
Variant Not supported: use Object
Currency Not supported: use Decimal
Date No longer stored as a Double
'Array
'Array declarations
declarations
Dim
Dim Month(12)
Month(12) AsAs Integer
Integer 'Creates
'Creates array
array with
with 13
13 elements
elements
'Initialize
'Initialize the
the array
array with
with 1212 elements
elements
Dim
Dim aMonth(
aMonth( )) As
As Integer
Integer == {1,2,3,4,5,6,7,8,9,10,11,12}
{1,2,3,4,5,6,7,8,9,10,11,12}
Declaring Multiple Variables
Dim
Dim I,
I, J,
J, XX As
As Integer
Integer
'Results
'Results inin II and
and JJ As
As Variant,
Variant, XX As
As Integer
Integer
Procedure scope
Variables accessible to entire procedure
Block scope
Variables only accessible within that block
Lifetime of block variable is entire procedure
Dim
Dim iLooper
iLooper As
As Integer
Integer 'Procedure
'Procedure level
level variable
variable
For
For iLooper
iLooper == 11 to
to 10
10
Dim
Dim iMax
iMax As
As Integer
Integer 'Block
'Block level
level variable
variable
iMax
iMax == iLooper
iLooper
Next
Next
MsgBox
MsgBox (iMax)
(iMax) 'This
'This line
line generates
generates aa compiler
compiler error
error
Creating Data Structures
Structure
Structure Customer
Customer
Public
Public CustID
CustID As
As Integer
Integer
Dim
Dim CustDayPhone
CustDayPhone AsAs String
String 'Defaults
'Defaults to
to public
public
Private
Private CustNightPhone As
CustNightPhone As String
String 'Private allowed
'Private allowed
End
End Structure
Structure
Compiler Options
Option Explicit
Default option
Option Strict
Enforces strict type semantics and restricts implicit type
conversion
Late binding by means of the Object data type is not
allowed
Option Base 1 Not Supported
Arrays must start at zero
Assignment Operators
*= Multiplication
/= Division
+= Addition
-= Subtraction
&= String concatenation
Example: iResult += 25
iResult equals the existing value for iResult, plus 25
Demonstration: Using Variables and Data Structures
Functions, Subroutines, and Properties
Dim
Dim rs
rs As
As ADODB.Recordset,
ADODB.Recordset, Lab1
Lab1 As
As Label
Label
'…initialization
'…initialization
rs.Fields.Item(1).Value
rs.Fields.Item(1).Value == Lab1.Text
Lab1.Text 'Valid
'Valid
rs.Fields(1).Value
rs.Fields(1).Value == Lab1.Text
Lab1.Text 'Valid
'Valid
rs.Fields(1)
rs.Fields(1) == Lab1.Text
Lab1.Text 'Not
'Not valid
valid
Lab1
Lab1 == "Data
"Data Saved"
Saved" 'Not
'Not valid
valid
Lab 3.1: Working with Variables and Procedures
Exception Handling
...
...
Try
Try
'' Include
Include code
code to
to be
be tried
tried here
here
'' Can
Can use
use Exit
Exit Try
Try to
to exit
exit block
block and
and resume
resume after
after End
End Try
Try
Catch
Catch
'' Define
Define exception
exception type
type and
and action
action to
to be
be taken
taken
'' Can
Can use
use series
series of
of statements
statements (multiple
(multiple error
error handling)
handling)
Finally
Finally
'' Optional
Optional block
block
'' Define
Define actions
actions that
that need
need to
to take
take place
place
End
End Try
Try
...
...
Using Try…Catch…Finally
Sub
Sub TrySimpleException
TrySimpleException
Dim
Dim i1,
i1, i2,
i2, iResult
iResult AsAs Decimal
Decimal
i1
i1 == 22
22
i2
i2 == 00
Try
Try
iResult
iResult == i1
i1 // i2
i2 '' Cause
Cause divide-by-zero
divide-by-zero error
error
MsgBox
MsgBox (iResult)
(iResult) '' Will
Will not
not execute
execute
Catch
Catch eException
eException As
As Exception
Exception '' Catch
Catch the
the exception
exception
MsgBox
MsgBox (eException.Message)
(eException.Message) '' Show
Show message
message to
to user
user
Finally
Finally
Beep
Beep
End
End Try
Try
End
End Sub
Sub
The System.Exception Class
Try
Try
If
If xx == 00 Then
Then
Throw
Throw NewNew Exception("x
Exception("x equals
equals zero")
zero")
Else
Else
Throw
Throw NewNew Exception("x
Exception("x does
does not
not equal
equal zero")
zero")
End
End If
If
Catch
Catch eException
eException As As Exception
Exception
MsgBox("Error:
MsgBox("Error: "" && eException.Message)
eException.Message)
Finally
Finally
MsgBox("Executing
MsgBox("Executing finally
finally block")
block")
End
End Try
Try
Demonstration: Structured Exception Handling
Lab 3.2: Implementing Structured Exception Handling
Review
Data Types
Using Variables
Functions, Subroutines, and Properties
Exception Handling