Visual Basic 6 Wikibook
Visual Basic 6 Wikibook
Visual Basic 6 Wikibook
en.wikibooks.org
On the 28th of April 2012 the contents of the English as well as German Wikibooks and Wikipedia
projects were licensed under Creative Commons Attribution-ShareAlike 3.0 Unported license. A
URI to this license is given in the list of gures on page 247. If this document is a derived work
from the contents of one of these projects and the content was still licensed by the project under
this license at the time of derivation this document has to be licensed under the same, a similar or a
compatible license, as stated in section 4b of the license. The list of contributors is included in chapter
Contributors on page 245. The licenses GPL, LGPL and GFDL are included in chapter Licenses on
page 251, since this book and/or parts of it may or may not be licensed under one or more of these
licenses, and thus require inclusion of these licenses. The licenses of the gures are given in the list of
gures on page 247. This PDF was generated by the LATEX typesetting software. The LATEX source
code is included as an attachment (source.7z.txt) in this PDF le. To extract the source from
the PDF le, you can use the pdfdetach tool including in the poppler suite, or the http://www.
pdflabs.com/tools/pdftk-the-pdf-toolkit/ utility. Some PDF viewers may also let you save
the attachment to a le. After extracting it from the PDF le you have to rename it to source.7z.
To uncompress the resulting archive we recommend the use of http://www.7-zip.org/. The LATEX
source itself was generated by a program written by Dirk Hnniger, which is freely available under
an open source license from http://de.wikibooks.org/wiki/Benutzer:Dirk_Huenniger/wb2pdf.
Contents
1
Introduction
1.1
How to use this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2
Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.3
VB Integrated Development Environment . . . . . . . . . . . . . . . . . . .
3
3
4
4
History
2.1
Evolution of Visual Basic . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.2
Timeline of Visual Basic . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.3
See also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5
6
6
7
Getting Started
3.1
Hello World . . . . . . . . . . . . . . . . . . . . . . . .
3.2
The Visual Basic Integrated Development Environment
3.3
Parts of a Visual Basic Program . . . . . . . . . . . . .
3.4
Controls . . . . . . . . . . . . . . . . . . . . . . . . . .
3.5
Reserved Words . . . . . . . . . . . . . . . . . . . . . .
3.6
REMs . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.7
Error Checking . . . . . . . . . . . . . . . . . . . . . .
3.8
Declaring Variables (Dimensioning) . . . . . . . . . . .
3.9
Simple output . . . . . . . . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
9
9
9
13
13
17
18
18
20
21
Simple Arithmetic
4.1
Arithmetic Operators . . . . .
4.2
Comparison Operators . . . .
4.3
Built in Arithmetic Functions
4.4
Trigonometrical Functions . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
25
25
29
29
30
Branching
5.1
If...Then Statement . . .
5.2
If..Then..Else Statement
5.3
Select Case . . . . . . . .
5.4
Unconditionals . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
33
33
34
35
35
Loops
6.1
For..Next Loops
6.2
Do Loops . . . .
6.3
While Loops . .
6.4
Nested Loops .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
39
39
40
42
42
Strings
7.1
Built In String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . .
43
43
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
III
Contents
7.2
8
Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
43
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
45
45
46
46
46
46
47
47
50
50
51
Regular Expressions
9.1
Class outline . . . . . .
9.2
Constructing a regexp .
9.3
Testing for match . . .
9.4
Finding matches . . . .
9.5
Finding groups . . . . .
9.6
Replacing . . . . . . .
9.7
Splitting . . . . . . . .
9.8
Example application .
9.9
External links . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
53
53
53
54
54
54
55
55
55
58
.
.
.
.
.
.
.
.
.
.
.
59
59
59
60
60
60
62
63
63
63
64
65
.
.
.
.
.
.
.
.
.
10 Arrays
10.1 Introduction . . . . . . . .
10.2 Use of Arrays . . . . . . .
10.3 Indices . . . . . . . . . . .
10.4 Size . . . . . . . . . . . . .
10.5 Dynamic Arrays . . . . . .
10.6 Variant Arrays . . . . . . .
10.7 Multi-Dimensional Arrays
10.8 Erasing Arrays . . . . . . .
10.9 Mixing Arrays . . . . . . .
10.10 Use of Matrices . . . . . .
10.11 Sorting . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
11 Collections
67
12 Dictionaries
71
13 Data Types
13.1 Built in Types . . . .
13.2 Byte, Integer & Long
13.3 Single & Double . . .
13.4 String . . . . . . . . .
13.5 Structure . . . . . . .
13.6 Enumeratation . . . .
IV
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
73
73
74
74
75
76
76
Contents
13.7
Type Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
77
79
79
79
80
80
81
81
82
15 Error Handling
83
16 Files
16.1 Layout of Data . . . . . . .
16.2 Input . . . . . . . . . . . . .
16.3 Output . . . . . . . . . . . .
16.4 How to specify the path to a
16.5 Examples . . . . . . . . . . .
85
85
85
87
88
89
. .
. .
. .
le
. .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
17 User Interfaces
91
18 Simple Graphics
18.1 Shape and Line controls . . . . . . . . . . . . . . . . . . . . . . . . . . . .
93
93
19 Windows Dialogs
19.1 Open and Save dialogs
19.2 Color dialog . . . . . .
19.3 Font dialog . . . . . . .
19.4 Print dialog . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
20 Databases
95
95
97
99
101
103
21 Windows API
107
21.1 Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
21.2 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
22 Subclassing
109
22.1 Why Subclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
22.2 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
23 External Processes
113
23.1 The Shell Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
23.2 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
24 Object Oriented
24.1 Types . . . .
24.2 Classes . . .
24.3 Inheritance .
24.4 Collections .
Programming
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
115
116
117
117
120
Contents
24.5
Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
25 Eective Programming
25.1 General Guidelines . . . . . . . . . . . . . . . .
25.2 Declaring variables . . . . . . . . . . . . . . . .
25.3 Comments . . . . . . . . . . . . . . . . . . . . .
25.4 Avoid Defensive Programming, Fail Fast Instead
25.5 Assertions and Design By Contract . . . . . . .
25.6 Tests . . . . . . . . . . . . . . . . . . . . . . . .
25.7 Scope, Visibility and Namespaces . . . . . . . .
25.8 Hungarian Notation . . . . . . . . . . . . . . . .
25.9 Memory and Resource Leaks . . . . . . . . . . .
25.10 Errors and Exceptions . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
121
121
122
123
125
126
128
129
129
130
134
26 Idioms
139
26.1 Resource Acquisition Is Initialization - RAII . . . . . . . . . . . . . . . . . 139
26.2 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
27 Optimizing Visual Basic
27.1 Integers and Longs .
27.2 Strings . . . . . . . .
27.3 ByRef versus ByVal .
27.4 Collections . . . . . .
27.5 Dictionaries . . . . .
27.6 The Debug Object .
27.7 Object Instantiation .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
145
145
148
151
154
156
159
160
28 Examples
167
29 Snippets
29.1 TopMost Function . . . . . . . . . . . . . . . . .
29.2 Form Close Button Disable . . . . . . . . . . . .
29.3 ComboBox Automation . . . . . . . . . . . . . .
29.4 Reversing a String . . . . . . . . . . . . . . . . .
29.5 Preventing icker during update . . . . . . . . .
29.6 Useful Date Functions . . . . . . . . . . . . . . .
29.7 Blast Eect . . . . . . . . . . . . . . . . . . . .
29.8 Sleep Function . . . . . . . . . . . . . . . . . . .
29.9 Random Numbers . . . . . . . . . . . . . . . . .
29.10 Animated Mouse Cursor . . . . . . . . . . . . .
29.11 Adding a bitmap to a menu entry . . . . . . . .
29.12 Application Launching . . . . . . . . . . . . . .
29.13 Rounding Things Up . . . . . . . . . . . . . . .
29.14 TCP/Winsock - Point-to-Point Connection . . .
29.15 TCP/Winsock - Point-to-MultiPoint Connection
169
169
169
170
170
171
172
172
172
173
173
174
175
175
176
178
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
30 The Language
181
30.1 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
30.2 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
VI
Contents
30.3
30.4
30.5
Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Unconditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
31 Coding Standards
31.1 Overview . . . . . . . . . . . . . . . . . . . . .
31.2 When Does This Document Apply? . . . . . .
31.3 Naming Standards . . . . . . . . . . . . . . . .
31.4 Variables . . . . . . . . . . . . . . . . . . . . .
31.5 Scope . . . . . . . . . . . . . . . . . . . . . . .
31.6 Data Type . . . . . . . . . . . . . . . . . . . .
31.7 Option Explicit . . . . . . . . . . . . . . . . .
31.8 Variable Names . . . . . . . . . . . . . . . . .
31.9 User Dened Types (UDTs) . . . . . . . . . .
31.10 Arrays . . . . . . . . . . . . . . . . . . . . . .
31.11 Procedure . . . . . . . . . . . . . . . . . . . .
31.12 Function Procedure Data Types . . . . . . . .
31.13 Parameters . . . . . . . . . . . . . . . . . . . .
31.14 Function Return Values . . . . . . . . . . . . .
31.15 Constants . . . . . . . . . . . . . . . . . . . .
31.16 Controls . . . . . . . . . . . . . . . . . . . . .
31.17 Specifying Particular Control Variants - NOT
31.18 Menu Controls . . . . . . . . . . . . . . . . . .
31.19 Cradle to Grave Naming of Data Items . . . .
31.20 Fields in databases . . . . . . . . . . . . . . .
31.21 Objects . . . . . . . . . . . . . . . . . . . . . .
31.22 Control Object Variables . . . . . . . . . . . .
31.23 API Declarations . . . . . . . . . . . . . . . .
31.24 Source Files . . . . . . . . . . . . . . . . . . .
31.25 Procedure Length . . . . . . . . . . . . . . . .
31.26 IF . . . . . . . . . . . . . . . . . . . . . . . . .
31.27 SELECT CASE . . . . . . . . . . . . . . . . .
31.28 DO . . . . . . . . . . . . . . . . . . . . . . . .
31.29 FOR . . . . . . . . . . . . . . . . . . . . . . .
31.30 Assignment Statements (Let) . . . . . . . . . .
31.31 GOTO . . . . . . . . . . . . . . . . . . . . . .
31.32 EXIT SUB and EXIT FUNCTION . . . . . .
31.33 EXIT DO/FOR . . . . . . . . . . . . . . . . .
31.34 GOSUB . . . . . . . . . . . . . . . . . . . . .
31.35 Procedure Calls . . . . . . . . . . . . . . . . .
31.36 Procedure Parameters . . . . . . . . . . . . . .
31.37 Error Handling . . . . . . . . . . . . . . . . .
31.38 Formatting Standards . . . . . . . . . . . . . .
31.39 White Space and Indentation . . . . . . . . .
31.40 Commenting Code . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
187
187
187
188
190
190
190
191
191
194
194
195
195
196
196
197
199
199
201
201
202
202
202
204
205
206
206
207
208
209
209
209
209
211
211
212
212
213
216
216
218
VII
Contents
32.2
32.3
32.4
32.5
32.6
32.7
Mathematical Functions
Input and output . . . .
Date and Time . . . . .
Miscellaneous . . . . . .
File Handling . . . . . .
Selection . . . . . . . . .
33 Glossary
33.1 A .
33.2 B .
33.3 C .
33.4 I . .
33.5 J .
33.6 O .
33.7 P .
33.8 R .
33.9 S .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
228
231
232
233
233
233
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
235
235
235
236
236
236
236
237
237
237
34 Work in Progress
34.1 Miscellaneous . . . . . . . . . .
34.2 Graphics . . . . . . . . . . . . .
34.3 Unicode Support . . . . . . . .
34.4 Networks . . . . . . . . . . . . .
34.5 Shell Integration . . . . . . . . .
34.6 Shell Context Menu . . . . . . .
34.7 Command Line . . . . . . . . .
34.8 Console Programs . . . . . . . .
34.9 CGI . . . . . . . . . . . . . . . .
34.10 Windows management Interface
34.11 Sound . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
239
239
239
239
239
240
240
240
240
240
240
240
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
35 Links
241
35.1 External Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
36 Contributors
243
36.1 Authors and Contributors . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
37 Contributors
245
List of Figures
247
38 Licenses
38.1 GNU GENERAL PUBLIC LICENSE . . . . . . . . . . . . . . . . . . . . .
38.2 GNU Free Documentation License . . . . . . . . . . . . . . . . . . . . . . .
38.3 GNU Lesser General Public License . . . . . . . . . . . . . . . . . . . . . .
251
251
252
253
1 Introduction
1.1 How to use this book
The rst page of the book is an annotated contents1 list, click the headings with the mouse
to go to the page you want to read.
You will need a copy of Visual Basic. You can see the Microsoft US2 or Microsoft
UK3 sites for information but the language has been retired and is now unsupported by
Microsoft4 . Visual studio 2005 is also available free of charge on the Microsoft website
but only supports VB.net.
You will often see Visual Basic abbreviated to VB, and for the purpose of conciseness,
this guide will use VB from this point onwards.
VB code is written in US English. There are some key dierences you should note, as
misspelling code can cause hours of problems. Below is a list of the dierences- when
writing in VB, you should use the right-hand column's spellings:
Many of these words you will not need until you progress into more complex
VB
Commonwealth English
Analyse
Centimetres
Centre
Colour
Co-ordinates
Grey(ed)
Licence
Millimetres
US English (VB)
Analyze
Centimeters
Center
Color
Coordinates
Gray(ed)
License
Millimeters
Words which can be spelled with an 'ise' and 'ize' sux should be spelled with 'ize' in VB.
This book will guide you step-by-step through Visual Basic. Some chapters of this book
contain exercises. The exercises are an integral part of the text and at least some should be
tried or you will not get the full benet of the book. There are no answers to the exercises
because most do not have a single correct answer. Physical exercises are done to build
muscle and stamina, intellectual exercises are the same thing for the brain. In almost every
1
2
3
4
http://en.wikibooks.org/wiki/..%2F
http://www.microsoft.com
http://www.microsoft.co.uk
http://en.wikipedia.org/wiki/Visual%20Basic%23Legacy%20development%20and%20support
Introduction
case you should be able to think of at least two 'solutions'. Some exercises require you
to write code, please write it and execute it, others expect you to think about or describe
something, please write it down even if only on a piece of scrap paper, it helps x it in your
mind.
If you try an exercise and fail to nd a solution please ask for help by adding a comment
to the relevant discussion page. It is of course entirely possible that the exercise is faulty
so reports of problems will help the authors too.
Please add comments to the discussion page (click the discussion button at the top of this
page). If there are specic topics that you want covered please say so in as much detail as
you can.
1.2 Terminology
Please refer to the ../Glossary/5 for denitions of any unfamiliar words or any words that
appear to be used in an unusual sense.
2 History
Visual Basic is Microsoft's high-level object-oriented rapid application development1 environment for the Windows platform. The rst versions of Visual Basic were intended to
target Windows 3.0 (a version for DOS existed as well), however it was not until version
3.0 for Windows 3.1 that this programming language gained large-scale acceptance in the
shareware and corporate programming community.
Using drawing tools that resemble those found in hardcopy page layout programs or PhotoShop, VB programmers make user interfaces by drawing controls and other UI components
onto forms. The programmer then adds code to respond to user interactions with the controls (for example, clicks, drag and drop, etc) known as events. The code can trigger events
in other controls (for example, by displaying text or an image), execute procedures (run
some algorithm based on the values entered in some control, output data, do business logic,
etc), or almost anything else one might do in code.
Visual Basic can be considered to be an interpreted language like its Basic2 ancestor, with
appropriate modications to accommodate object-oriented programming3 , and has implicit
type conversion. That is, the VB development environment goes to great lengths to format
(and aid the user in formatting) programming code so that it conforms to executable syntax.
For example, VB will appropriately change the case of newly typed variable names to match
those that have been declared previously (if they have been declared at all!). Traditionally, VB is known for compiling programs into pseudo-code (p-code, similar to Java's byte
code) which is interpreted at runtime, requiring the use of dynamically-linked libraries (for
example, VBRUN300.DLL for version 3 of Visual Basic, circa 1992) but newer versions can
compile code into something more closely resembling the ecient machine code generated
by C-like compilers. VB6 can be compile either into p-code or into native code; in fact VB6
uses the Microsoft C++ compiler to generate the executable.
For new Windows programmers, VB oers the advantage of being able to access much of
the Windows UI functionality without knowing much about how it works by hiding the
technical details. Although accessing low-level Windows UI functionality is possible, doing
so in VB is as, or more dicult compared to such access using Visual C++ or other lower
level programming languages. Recently VB.NET4 has gone a long way to xing some of
the limitations.
Using custom controls provided by Microsoft or third parties, almost any functionality that
is possible in Windows can be added to a VB program by drawing a custom control onto a
form in the project.
1
2
3
4
http://en.wikipedia.org/wiki/rapid%20application%20development
http://en.wikibooks.org/wiki/Subject%3ABASIC%20programming%20language
http://en.wikibooks.org/wiki/Object%20Oriented%20Programming
http://en.wikibooks.org/wiki/Visual%20Basic%20.NET
History
Visual Basic traditionally comes in at least entry level and professional versions, with various designations depending on Microsoft's contemporary marketing strategy. The dierent
versions are generally dierentiated by the number of custom controls included, and the
capabilities of the compiler. Higher priced packages include more functionality.
5
6
http://en.wikipedia.org/wiki/1991
http://en.wikipedia.org/wiki/Alan%20Cooper
See also
http://en.wikipedia.org/wiki/VBScript
3 Getting Started
3.1 Hello World
The very simplest program in VB is this:
Public Sub Main()
MsgBox("Hello World!")
End Sub
Start VB
A New Project window will appear. Select Standard.exe and click on Open.
Open VB's Project menu and click Add Module
Copy or type the Main subroutine shown above into the new code module.
Go to VB's Project menu again and click the Project1 Properties item (it should be the
last item in the menu).
In the Project Properties dialog box you should nd a dropdown list labeled Startup
Object, click there and choose Sub Main
Go to the Run menu and click Start (or press F5) to run the program. VB might ask if
you want to save, just say no this time.
The running program will show the words Hello World! in a small message window.
Getting Started
3.2.1 Availability
As of April 8, 2008, Microsoft no longer provides support to the Visual Basic 6.0
IDE.http://msdn.microsoft.com/en-us/vstudio/ms788708/ Nonetheless, Visual Basic
6 is still available via MSDN subscription, under the name of "Visual Studio 6".http:
//stackoverflow.com/questions/229868/how-to-compile-legacy-vb6-code
10
11
Getting Started
kept in its own module (and le) where it can be loaded and discarded at will. Such code
can be added, removed, or edited, until it is perfected (debugged) and copied into the real
program modules.
When you type that line into the Immediate Window and press Enter, VB interprets that
line as a VB command and executes the command. In this case it prints the number
3.14159265358979 on the line following the command. (For expediency, the question mark
can be substituted for the Print command in most circumstances.)
You can send text to that same window from a running program using the Print method of
the Debug object:
Private Sub Text1_KeyPress(KeyAscii As Integer)
Debug.Print Chr(KeyAscii), KeyAscii
End Sub
In this case, with a Textbox called Text1 on some form, every letter a user enters into the
textbox will be echoed to the Immediate Window, as well as its numerical ASCII value.
Because text is sent to the Immediate Window using the Debug object, many people refer
to the Immediate Window as the Debug Window. You may nd it listed either way in this
literature.
IDE NOTE:
It should be noted that running a program using the VB IDE is not identical to running
a stand alone executable program. The program running inside the IDE is running as a
sub-process of the IDE. There are a few small connection and timing issues associated with
running as a sub-process. On the whole, however, a program run in the IDE will look
and feel like the nished program, but to fully test a program as the user will see it, it is
advisable to create the program executable (.exe) and test that for correctness and usability.
12
3.3.2 Components
A component is an executable module stored either as a .VBX le (Visual Basic eXtension
for the 16-bit versions of VB), .OCX (OLE Control eXtension for 32-bit versions) le or as
a .DLL (Dynamic Link Library) le. Components are pre-compiled code modules used by
other program writers with/without knowledge and understanding of the details of its inner
workings. Pre-compiled components provide reuseable code that has already been written
and debugged. Components can be code only (.DLL) or have a visual component that can
be placed on a form (.VBX and .OCX). VB supplies many commonly used components
(Button, Textbox, Listbox, etc.) as controls in the Toolbox.
3.3.3 Events
An event is an activity that occurs during a program's execution usually in response to the
user's actions, such as a mouse click or a keypress. An event causes a procedure to execute.
You the programmer can decide what code (if any) you want to place within the dierent
event procedures.
3.4 Controls
3.4.1 Control Properties
To display the Control Properties window, select View\Properties Window or press the
F4 key. The Properties window initially appears on the right edge of the main window
and contains all the names of the editable properties as well as their current values. Some
control properties are only editable while the program is running, some are only editable
while in design mode.
3.4.2 Buttons
A button will be your best friend in Visual Basic. Each button should contain code, which
is added by you, the programmer. Upon clicking the button, the user will be instructing the
program to execute that portion of code. For example, you could set it so when pressed, the
13
Getting Started
program will make a message box that says "HELLO!". Good programming styles generally
use cmd<ButtonName> when naming a button.
3.4.4 Labels
Labels are one of the most used Visual Basic objects. They are often used to label other
controls (textboxes, images, etc.) or provide feedback to the user. They are usually named
like lbl<LabelName>.
3.4.5 Timers
Timers are interesting and easy to learn. If you want the program to perform a certain
task after a certain amount of time, the Timer is there to help you out. Their only event
procedure is _timer, which will be executed every time after a certain amount of time is
passed. The most common steps to use Timers is as simple as follows:
1. Add a timer to the form and give it a name.
2. Set the time interval in the Properties window to some value above 0.
3. Double click the timer and add the code you want executed at the set intervals.
Timers have very few properties too.
This is a possible use of timer: (To see it in action add a Command button, Shape control,
and Timer to a new form. Then set the Timer.Interval property using the Properties
window. Copy the code below into the form's code module and press F5 to run the program.)
Private Sub Command1_Click()
Timer1.Enabled = Not Timer1.Enabled
End Sub
Private Sub Timer1_Timer()
Shape1.Visible = Not Shape1.Visible
End Sub
14
Controls
This would make the command button stop and start the ashing (repeatedly disappear
and reappear) of the Shape control. The ash interval is determined by the Timer's Interval
property. Timer.Interval is measured in milliseconds.
Here is another timer example of a counter:
'***************************************************************** *
'APRON TUTORIAL PRESENTED BY MORROWLAND *
'***************************************************************** *
'Project Name : Timer * * Project Description : Using Timer and
'Counter * * Project Type : Visual Basic * * Author : Ronny Andr
'Reierstad * * Web Page : www.morrowland.com * * E-Mail :
'[email protected] * * Version : English (UK) * * Date :
'27.06.2002 *
'*****************************************************************
'Timers are the backbone in any good application, you will be able
'to decide when things will happen in millisecounds by using timers
'and counters you gain control
'declare counter as integer
Dim counter As Integer
Text1.Text = counter
End Sub
VB Timers are given low priority on the processing totempole. If any other code needs to
execute when the timed interval has elapsed, that other code is allowed to execute before
the timer's procedure is called. Other code can yield some of their processing time to
Windows by using the DoEvents command. That is the only VB command that will allow
code you write to yield time to any pending timer events in the program. Without the
use of DoEvents, each subroutine or function is executed start to nish, in serial
15
Getting Started
fashion. In other words, only one command or statement can be executed at a time. Each
subroutine or function must list the proper commands to execute, in their proper order.
3.4.8 References
Reference is a kind of link that is used by Visual Basic. The word reference is used in two
distinct ways:
Component reference
Are typically external libraries such as DLLs2 , OCXs3 , or type library4 . Component
references are added to the project, so that all parts of the program may interact with the
outside component.
Object reference
Is typically a pointer to an internal object5 . Object references dierentiate, or identify,
specic objects within the program. The term object is a very general term used to indicate
just about anything that has properties to set, or methods to call. For example, if your
form has 5 textboxes on it and you want to put text in just one of them, you need to use
the proper 'object reference' to address that specic textbox.
These two uses are quite distinct and generally do not cause any problems. Usually when
object reference is meant it will be written out in full whereas just references usually means
references to an external library. In both cases, accessing the properties (or methods) of a
1
2
3
4
5
16
Chapter 17 on page 91
http://en.wikibooks.org/wiki/..%2FDLL
http://en.wikibooks.org/wiki/..%2FOCX
http://en.wikibooks.org/wiki/..%2Ftypelib
http://en.wikibooks.org/wiki/..%2Fobjects
Reserved Words
component or object reference requires a special syntax that includes a period to separate
the object and property. The syntax is: .<property> or, by example: Form1.Caption =
"This", or Text1.Text = "That", etc. where Form1 or Text1 are the specic objects you
want to alter, and Caption or Text are the specc properties being changed.
You can create components6 in VB to be used by other programs (not just those written in
VB).
17
Getting Started
Stop
Sub
Then
Time
True
Until
While
With
3.6 REMs
While programming you may nd it necessary to leave yourself notes. This can be used
to easily identify areas of code, or as a reminder of sections of code with logic errors that
need to be xed. REMs are very simple. Merely place an apostrophe, " ' ", or the word,
"REM", before the line and that line will be ignored by the interpreter, whether it be a
message to yourself or a section of code. For example:
' I
REM
'If
REM
An apostrophe is most often used due to the comparative reduction in space and time as
REM has been included so as to be backward compatible with earlier versions of BASIC.
The REM statement is a basic command and must be preceded by a colon if following
other commands on the same line. An apostrophe needs no colon:
18
Error Checking
Logic errors are errors that the computer cannot spot. These are errors that, even though
the program runs, the result is not what you intended.
The rst two of these errors are generally easy to spot, and the debugging tool can be used
to high-light the line of text at which the error occurred. For example, if you wanted to
make a program to convert Celsius to Fahrenheit, and in the code you used a multiplication
symbol instead of division, the program would run ne, but the program would not convert
the temperatures correctly, as you desired. Although sometimes these logic errors can be
easy to spot, some are quite dicult. Logic errors become concealed in the lines of code
when making complex programs, such as a game. Just remember, the computer does not
know what the program is supposed to do, it only knows the code. Look through the code
step-by-step and think of how the computer would interpret the code.
VB won't let you just ignore syntax/compile errors. You have to x them before you can
run your program. But run-time errors, which are syntactically correct, but may cause an
error if an attempt is made to execute it, can be handled to possibly prevent your program
from crashing. The following example shows a very good way of handling a possible error:
Private Sub Form_Load()
On Error GoTo ErrorHappened
i = 1 / 0 'This line will cause an error to be raised as anything divided
by zero = infinity
'...
'Some code
Exit Sub 'Here the subroutine is exited if no errors occur
ErrorHappened:
'Informs the user an error happened.
MsgBox "Error Number " & Err.Number & ":" & Err.Description
End Sub
The output of this example is a message box that says "Error Number 11: Division by
zero". The statement On Error Goto ErrorHappened will skip everything from i = 1 / 0
to Exit Sub if any run-time error occurs within this procedure. When an error is detected
the program will continue to run from right after ErrorHappened:, where the error will be
displayed so that the programmer can nd out what it is and x it. This also prevents the
program from "crashing".
ErrorHappened is just a line label, you can name it anything you wish to identify that
section of the procedure you want to execute when an error happens. On Error Goto
can only reference line labels that are within the current procedure. You cannot (easily)
dene one error handler for all procedures. Exit Sub means to end the Form_Load event
immediately.
So if no error occurs, a message box will NOT be called because Exit Sub will already have
ended or exited our subroutine. And if an error does occur, the message box will pop up,
displaying the Error Number and the Error Description.
The above example is the safest way of detecting and handling any error that takes place in
the subroutine. However you can also choose to ignore errors by using "On Error Resume
Next" which means to ignore all errors. Ignoring errors is not a good idea in most scenarios,
but in some cases the proper way to handle an error is to do nothing, so they might as well
be ignored. Even using Resume Next, you can test the Err object (Err.Number) to see if
an error happened on preceding lines.
19
Getting Started
See Errors7 for more detail about error handling.
Of course, the variable name could be whatever you want. The type however, is dierent.
You have a choice of single, integer, or string. This tells the computer what type of information the variable holds. "Single" variables can hold numbers with decimal. "Integers"
variables can hold whole numbers, while "String" variables holds text or a set of characters.
If you don't dim a variable, the type would automatically be "Variant", which can hold
almost all kinds of information. For example:
Option Explicit
Dim intNumber As Integer
intNumber = 31
' This is ok
intNumber = "I didn't"
' Error: type mismatch (intNumber is an integer
while "I didn't" is a string)
Dimming is especially important for arrays and matrices. For an array, next to the variable
name, you enter the range of the array. For example:
Dim x(1 to 10) As Integer
Arrays will be covered more in depth later. Matrices are dimensioned almost exactly like
arrays are, however, instead of the having only one dimension (1 to 20), matrices may have
two: (1 to 20,1 to 5), or even three. Dimensioning can also be used to tell the computer
that variables are public. This will be discussed later in the Scope section.
Note: If you don't dimension your variables, you might end up with many unexpected
errors. It could be avoided by using the Option Explicit statement, which requires every
variable to be dened; if not every variable used in the program is dened, VB raises an
error: "Variable is not dened". To enable this, you just have to type Option Explicit at
the very top of ALL your code in the current module. It's a very good practice to do so.
20
Simple output
Try it. Are you tired of the boring "Hello world!"? Let's make a fancier one:
MsgBox("Fatal error: Your computer will be shut down in five seconds.",
vbCritical, "System")
3.9.2 Printing
Note: The word "printing" here means using the Print statement, it's not about using the
printer or printing les. Printing is a fairly simple part of Visual Basic, but also essential.
Printing is used to output information to the user. It proves to be a valuable troubleshooting tool. Whenever printing, you need an object to print on, followed by of course,
something to print. Printing may be used with various objects, however, the most common
in the picture box. For the sake of simplicity, we are assuming you renamed the picture box as "pic". In this wikibook though, print is done mainly on picture boxes and forms:
pic.Print "Hello world!!"
Print "Hello world!!!"
3.9.3 Spacing
There are various ways to alter how text is spaced when printing. The most common is
the comma. A comma will go to the next print zone. Print zones are 15 characters long.
You can think of it like pressing the tab key when typing something out. Remember that
print zones are xed, so if you've typed 1 letter, and then used a comma, then it will be
a big space. If you type 13 characters and use a comma, it will not be a large space. For
example:
21
Getting Started
Several new concepts are introduced in this example. The "Form_Click" contains a block
of code and it is called to run when the user clicks on the current Form(Form1). 'Me' is the
same as the current form (Form1). Don't be afraid to experiment. No matter what you do
in VB, its always reversible. Now, the comma isn't all that versatile. Another feature is
tab. Tab will move so many spaces from the BEGINNING of the line. Followed by tab in
parentheses is the amount of characters spaces. For example:
Form1.Print "Hello"; Tab(10); "Yay"
This will NOT print "yay" 10 spaces after the O of "Hello". Rather it will print 10 spaces
from the beginning of the line. You may use as many tabs as you want in the same print
command. Although tab is useful, sometimes it is better to space things in relation to
what has already been printed. This is where the space function comes in. The syntax of
space is identical to that of tab. Space will move the next printed text so many spaces over
from its CURRENT location. For example:
Pic.print "Hello"; Space(10); "Yay"
This will print the rst Y of "Yay" 10 spaces to the right of the O in "Hello". It is
important to note, if you write:
Pic.Print "Hello"
Pic.Print "Hello"
This can be easily dealt with in the need of having separate print statements print on the
same line. You merely have to change the code to: (note the semicolon)
Pic.Print "Hello";
Pic.Print "Hello"
22
Simple output
If you want to make a blank line in between the two "hello"'s, then you may simply have a
blank print statement WITHOUT a semicolon. For example:
Pic.Print "Hello"
Pic.Print
Pic.Print "Hello"
It is important to remember that if the rst print has a semicolon at the end, often referred
to as a trailing semicolon, the empty print will only reverse it, and print the second Hello
on the next line, and no blank line will appear.
23
4 Simple Arithmetic
Visual Basic has all of the common arithmetical functions. It does not have complex numbers nor does it allow you to overload operators so manipulating complex numbers or
matrices must be done by explicit function and subroutine calls.
- unary
*
/
\
Mod
Comments
Adds two numbers. Also concatenates strings but avoid this use because Visual Basic will try to convert the string to a number rst and
the results might not be what you expect.
Subtract the second number from the rst.
negate the operand.
Multiply two numbers. The result will be promoted to whatever data
type is needed to represent the size of the number if one of the numbers
is already that type (see note below about odd behavior).
Normal division. Treats both operands as real numbers and returns a
real result.
Integer division. Be careful with this because the arguments are converted to integers before the division is performed so use this with integer operands unless you comment your code carefully. Also note that
"/" and "*" are evaluated before "\", so (1000 \ 13 * 3) is not equal to
( (1000 \ 13) * 3)
Produces the remainder after integer division. Be careful with this as
the interpretation of the modulus operator in mathematics is ambiguous. a Mod b gives the same answer as this expression: a - (a \ b) *
b
Raises the rst operand to the power of the second. In Visual Basic either or both operands may be negative. If all you want is a square or
cube it is faster to explcitly multiply the number by itself the appropriate number of times.
For example:
25
Simple Arithmetic
Text2 = Text1 * 5
Will display the value in Text1, multiplied by 5, in Text2. E.g. if Text1 = 4 then Text2
will be equal to 20.
Exponentiation ()
Multiplication and normal division (* and /)
Integer division (\)
Mod (Mod)
Addition and subtraction (+,-)
General hint : If an expression uses more than (+,-,*) use all possible brackets to force the
expression to be evaluated the way you are thinking it.
or
Debug.Print (17000 * 3)
which both cause an error. This can be solved (in a direct but ugly way) by enclosing
numbers in CLng() (Convert to Long) so that
Debug.Print (CLng(17000) + CLng(17000))
neither of which cause an error. To avoid having to think about this, avoid using explicit
numbers in code and instead use "Long" variables and constants such as :
Const TEST_NUM As Long = 17000&
Debug.Print (TEST_NUM + TEST_NUM)
Dim TestNumVar As Long
TestNumVar = 17000
Debug.Print (TestNumVar + TestNumVar)
26
Arithmetic Operators
Meaning:
Negation
Conjuncton
Disjunction (logical addition)
Exclusive Or
Equivalence
Implication
When you construct logical expressions with these operators you get the following results:
27
A
T
T
F
F
B
T
F
T
F
A And B
T
F
F
F
A Or B
T
T
T
F
A Xor B
F
T
T
F
A Eqv B
T
F
F
T
A Imp B
T
F
T
T
Simple Arithmetic
28
Caution! Due to the internal structure of oating-point numbers (Single and Double), do
not use = or <> to compare them. Instead, use a small value (usually called Epsilon) as a
"maximum dierence". For example:
' This returns False :
Debug.Print (Sqr(1.234) * Sqr(1.234)) = 1.234
' This returns True :
E = 0.000001
Debug.Print Abs((Sqr(1.234) * Sqr(1.234)) - 1.234) < E
Operator
=
<>
<
>
>=
<=
Meaning
Equality
Inequality
Less than
Greater than
Greater than or equal to. Or put another way: not less than
Less than or equal to. Or put another way: not greater than
29
Simple Arithmetic
Randomize(x)
not really a mathematical function because it is actually a subroutine. This initializes the
random number generator.
Rnd(x)
produces the next random number in the series. Please read that sentence again! the
random numbers aren't really random, they are instead pseudo-random. If you initialize
the random number generator with the same number each time you start a program then
you will get the same series of values from Rnd()
Round(x,n)
returns a real number rounded to n decimal places (uses Banker's rounding).
Sgn(x)
returns plus one if x is positive, minus one if it is negative, zero if x is identically zero.
Sgn(-5)=-1 ; Sgn(5)=1 ; Sgn(0)=0
Sqr(x)
square root of x. Example: Sqr(25)=5. x must be non-negative. Thus Sqr(-25) will
generate an error
Notice that the range of applicability of these expressions is limited to the range -1<=x<=1.
Here are some more:
Secant
30
Sec(x) = 1 / Cos(x)
Trigonometrical Functions
Cosecant
Cotangent
Inverse Sine
Inverse Cosine
Inverse Secant
Inverse Cosecant
Inverse Cotangent
Hyperbolic Sine
Hyperbolic Cosine
Hyperbolic Tangent
Hyperbolic Secant
Hyperbolic Cosecant
Hyperbolic Cotangent
Inverse
Inverse
Inverse
Inverse
Inverse
cant
Inverse
gent
Hyperbolic
Hyperbolic
Hyperbolic
Hyperbolic
Hyperbolic
Sine
Cosine
Tangent
Secant
Cose-
Hyperbolic Cotan-
Cosec(x) = 1 / Sin(x)
Cotan(x) = 1 / Tan(x)
Arcsin(x) = Atn(x / Sqr(-x * x + 1))
Arccos(x) = Atn(-x / Sqr(-x * x + 1)) + 2 * Atn(1)
Arcsec(x) = Atn(x / Sqr(x * x - 1)) + Sgn((x) - 1) *
(2 * Atn(1))
Arccosec(x) = Atn(x / Sqr(x * x - 1)) + (Sgn(x) - 1) *
(2 * Atn(1))
Arccotan(x) = -Atn(x) + 2 * Atn(1)
HSin(x) = (Exp(x) - Exp(-x)) / 2
HCos(x) = (Exp(x) + Exp(-x)) / 2
HTan(x) = (Exp(x) - Exp(-x)) / (Exp(x) + Exp(-x))
HSec(x) = 2 / (Exp(x) + Exp(-x))
HCosec(x) = 2 / (Exp(x) - Exp(-x))
HCotan(x) = (Exp(x) + Exp(-x)) / (Exp(x) - Exp(x))
HArcsin(x) = Log(x + Sqr(x * x + 1))
HArccos(x) = Log(x + Sqr(x * x - 1))
HArctan(x) = Log((1 + x) / (1 - x)) / 2
HArcsec(x) = Log((Sqr(-x * x + 1) + 1) / x)
HArccosec(x) = Log((Sgn(x) * Sqr(x * x + 1) + 1) /
x)
HArccotan(x) = Log((x + 1) / (x - 1)) / 2
The very useful atan2 function (calculate the angle in all four quadrants of a vector) can
be simulated like this:
Public Const Pi As Double = 3.14159265358979
Public Function Atan2(ByVal y As Double, ByVal x As Double) As Double
If y > 0 Then
If x >= y Then
Atan2 = Atn(y / x)
ElseIf x <= -y Then
Atan2 = Atn(y / x) + Pi
Else
Atan2 = Pi / 2 - Atn(x / y)
End If
Else
If x >= -y Then
Atan2 = Atn(y / x)
ElseIf x <= y Then
Atan2 = Atn(y / x) - Pi
Else
Atan2 = -Atn(x / y) - Pi / 2
End If
End If
End Function
Other functions:
31
Simple Arithmetic
32
5 Branching
A branch is a point at which your program must make a choice. With these data structures,
it is now possible to make programs that can have multiple outcomes. You may be familiar
with the rst type from Algebra1 , an If-Then statement, or If P then Q. And they work
pretty much the same. Another type is the Select Case, this may prove to be easier at
times.
Another name for this concept is conditional clauses. Conditional clauses are blocks of code
that will only execute if a particular expression (the condition) is true.
to
If InStr(1, sName, " ") > 0 Then
sName = """" & sName & """"
End If
If there is only one reaction to the condition, the statement can also be expressed without
the End If:
If Y + 3 = 7 Then MsgBox "Y + 3 DOES = 7"
http://en.wikibooks.org/wiki/Algebra
33
Branching
There are also other parts to these statements to make them more complex. Two other
terms that can be used are Else, and ElseIf.
Else will, if the condition is false, do whatever comes between the Else statement and the
End If statement.
ElseIf will, if the condition directly proceeding it is false, check for another condition
and go from there.
If the original condition is met, then all the code within the rst statement is executed. The
optional Else section species an alternative statement that will be executed if the condition
is false. The If-Else statement can be extended to the following form:
If condition Then
statement
ElseIf condition Then
other statement
ElseIf condition Then
other statement
...
Else
another statement
End If
Only one statement in the entire block will be executed. This statement will be the rst
one with a condition which evaluates to be true. The concept of an If-Else-If structure is
easier to understand with the aid of an example:
Dim Temperature As Double
...
If Temperature >= 40.0 Then
Debug.Print "It's extremely hot"
ElseIf 30.0 <= Temperature And Temperature<=39.0 Then
Debug.Print "It's hot"
ElseIf 20.0 <= Temperature And Temperature <= 29.0 Then
Debug.Print "It's warm"
ElseIf 10.0 <= Temperature And temperature <= 19.0 Then
Debug.Print "It's cool"
ElseIf 0.0 <= Temperature And Temperature <= 9.0 Then
Debug.Print "It's cold"
Else
Debug.Print "It's freezing"
End If
34
Select Case
extremely hot"
hot"
warm"
cool"
cold"
freezing"
5.4 Unconditionals
Unconditionals let you change the ow of your program without a condition. You should
be careful when using unconditionals. Often they make programs dicult to understand.
Read Isn't goto evil? below for more information.
5.4.1 Exit
End a function, subroutine or property and return to the calling procedure or function.
Note that in Visual Basic returning from a function and assigning a return value requires
two separate statements.
35
Branching
For procedures:
Exit Sub
For functions:
Exit function
For properties:
Exit Property
5.4.2 End
This simple command ends the execution of the program
For example:
Private Sub cmd1_Click()
End
End Sub
In this example, when the button cmd1 is clicked, the program will terminate. The End
command is provided for backward compatibility and is rarely (if ever) needed to end a VB
program. The proper way to end a VB program is to release all object references, stop any
running timers, and exit out of any executing procedures. In a small program this could
mean to simply unload all the forms. When there are no active object references and no
code running, VB will terminate the program in a graceful manner. Using End to end the
program will terminate the program in an ungraceful manner (some things may not get
shut down properly).
5.4.3 Goto
Transfer control to the statement after the label (or line number).
Goto Label
Dont_Do_Something
Label:
...
In Visual Basic the target of a Goto statement must be in the same procedure; this prevents
abuse of the feature by making it impossible to jump into the middle of another subroutine.
Isn't Goto Evil?
One often hears that Goto is evil and one should avoid using goto. But it is often overlooked
that any exit statement which is not the last statement inside a procedure or function is
also an unconditional statement - a goto in disguise.
36
Unconditionals
Therefore if you have functions and procedures with more than one exit statement you can
just as well use goto. When it comes down to readability the following two samples are
almost the same:
Sub Use_Exit
Do_Something
If Test Then
Exit Sub
End If
Do_Something_Else
End Sub
Sub Use_Goto is
Do_Something
If Test Then
Goto Exit_Use_Goto
End If
Do_Something_Else
Exit_Use_Goto:
End Sub
In the pure structured approach, neither goto nor multiple exit statements are needed:
Sub Use_If is
Do_Something
If Not Test Then
Do_Something_Else
End If
End Sub
A couple of notes:
Return is a reserved keyword and can only be used with a matching Gosub and that the
Gosub must reside in the same function, sub or property. It cannot be used to return
from a function, sub or property - maybe it should read Exit Sub.
Error handling in Visual Basic does need the use of Goto, but with a dierent syntax.
Visual Basic, believe it or not, supports line numbering, so for example Goto 20 is perfectly
acceptable if line 20 exists!
Using Exit instead of Goto as shown above has some side eects. Calling Exit Sub clears
a current error state, i.e. it is the equivalent of an Err.Clear.
Compatibility with VB.Net
If you are concerned that you might want to port your code to VB.Net without recasting
it later you should probably avoid goto altogether.
One alternative is the pseudo loop. Its purpose is to fake a goto target that occurs later in
the routine:
Sub Use_PseudoLoop
Do 'Start the pseudo loop.
Try_Something
If Test Then
Exit Do
End If
Try_Again
If Test Then
37
Branching
Exit Do
End If
Try_Try_Again
Loop While False ' will never loop
Do_One_Last_Thing
End Sub
Of course this example can be recast using If..Then..Else..End If without the need for any
Exit statements at all so it is a little articial:
Sub Use_IfThen
Try_Something
If Not Test Then
Try_Again
If Not Test Then
Try_Try_Again
End If
End If
Do_One_Last_Thing
End Sub
38
6 Loops
Loops are control structures used to repeat a given section of code a certain number of
times or until a particular condition is met.
Visual Basic has three main types of loops: for..next loops, do loops and while loops.
Note: 'Debug' may be a reserved word in Visual Basic, and this may cause the code samples
shown here to fail for some versions of Visual Basic.
In this example, X is the counter, "1 to 100" is the range, and "2" is the step.
The variable reference in the Next part of the statement is optional and it is common
practice to leave this out. There is no ambiguity in doing this if code is correctly indented.
When a For..Next loop is initialized, the counter is set to the rst number in the range;
in this case, X is set to 1. The program then executes any code between the for and next
statements normally. Upon reaching the next statement, the program returns to the for
statement and increases the value of the counter by the step. In this instance, X will be
increased to 3 on the second iteration, 5 on the third, etc.
To change the amount by which the counter variable increases on each iteration, simply
change the value of the step. For example, if you use a step 3, X will increase from 1 to 4,
then to 7, 10, 13, and so on. When the step is not explicitly stated, 1 is used by default.
(Note that the step can be a negative value. For instance, for X = 100 to 1 step -1
would decrease the value of X from 100 to 99 to 98, etc.)
When X reaches the end of the range in the range (100 in the example above), the loop will
cease to execute, and the program will continue to the code beyond the next statement.
It is possible to edit the value of the counter variable within a for..next loop, although this
is generally considered bad programming practice:
For X = 1 To 100 Step 1
Debug.Print X
X = 7
Next
39
Loops
While you may on rare occasions nd good reasons to edit the counter in this manner, the
example above illustrates one potential pitfall:
Because X is set to 7 at the end of every iteration, this code creates an innite loop. To
avoid this and other unexpected behavior, use extreme caution when editing the counter
variable!
It is not required by the compiler that you specify the name of the loop variable in the Next
statement but it will be checked by the compiler if you do, so it is a small help in writing
correct programs.
The list is commonly a Collection or Array, but can be any other object that implements an
enumerator. Note that the iterating variable has to be either a Variant, Object or class
that matches the type of elements in the list.
6.2 Do Loops
Do loops are a bit more exible than For loops, but should generally only be used when
necessary. Do loops come in the following formats:
Do while
Do until
Loop while
Loop until
While loops (both do while and loop while) will continue to execute as long as a certain
conditional is true. An Until loop will loop as long as a certain condition is false, on the
other hand. The only dierence between putting either While or Until in the Do section or
the Loop section, is that Do checks when the loop starts, and Loop checks when the loop
ends. An example of a basic loop is as follows:
Do
Debug.Print "hello"
x = x + 1
Loop Until x = 10
This loop will print hello several times, depending on the initial value of x. As you may
have noticed, Do loops have no built in counters. However, they may be made manually
as shown above. In this case, I chose x as my counter variable, and every time the loop
execute, x increase itself by one. When X reaches 10, the loop will cease to execute. The
40
Do Loops
advantage of Do loops is that you may exit at any time whenever any certain conditional
is met. You may have it loop as long as a certain variable is false, or true, or as long as a
variable remains in a certain range.
In Visual Basic you cannot label the loop but you can of course place a label just before it,
inside it or just after it if you wish.
41
Loops
In Visual Basic you can also have more than one exit statement. You cannot exit named
outer loops using Exit Do because Visual Basic does not provide named loops; you can of
course use Goto instead to jump to a label that follows the outer loop.
The While loop will run until the condition tests false - or until an "Exit While" statement
is encountered.
This will print the word 'hi' twenty times. Upon the rst pass of the i loop, it will run the j
loop twice. Then on the second pass of the i loop, it will run the j loop another two times,
and so on.
42
7 Strings
Visual Basic has a traditional set of built in string operations. Unlike many languages
Visual Basic strings are always Unicode1 so they can hold any character. They are also
always dynamically allocated and are of almost unlimited length (theoretically up to about
231 characters, about 2000 million).
Note that Unicode uses more than one byte to represent each character. As far as VB
is concerned Unicode characters are two bytes which gives 216 or 65536 possible values.
This is enough even for Chinese, Japanase and Korean character sets (CJK). In fact Unicode
denes 17 planes each of which has room for 216 code points but VB (and Windows) uses
only the Basic Multilingual Plane2 (BMP).
See What Are VB Strings?3 for a concise explanation of the internal workings of Visual
Basic Classic strings.
1
2
3
http://en.wikipedia.org/wiki/Unicode
http://en.wikipedia.org/wiki/Basic%20Multilingual%20Plane
http://web.archive.org/web/20080626021549/http://www.romanpress.com/Articles/Strings_
R/Strings.htm
43
Strings
expressions are denitely worth learning even though they may seem intimidating at the
start. In practice most programmers nd that the more arcane expressions are rarely used
and the same idioms are recycled over and over so there is really not as much to learn as it
at rst appears.
44
To test whether two strings are equal by reference, that is, whether they start at the
same address, you can use StrPtr function.
To test whether a string is greater than or less than another using lexicographical order,
you can use <, >, <=, >=, <>. To decide whether a string is less than another, the two
are compared character by character and as soon as a character from one string is found
to have a lower ASCII code than the corresponding (same position) character in the other
string, the rst is declared to be less than the second. The converse test also works.
Another way of testing whether strings are equal or greater than one another is the StrComp function. Unlike =, <, >, etc., the StrComp function has an optional argument
that controls whether the comparison is case-sensitive.
Example:
strHello = "hello": strHello2 = "Hello"
If StrComp(strHello, strHello2, vbTextCompare) = 0 Then
Debug.Print "The strings are the same."
End If
Links:
StrComp Function1 , Access 2007, oce.microsoft.com
StrComp Function2 , Oce 2013, msdn.microsoft.com
1
2
http://office.microsoft.com/en-001/access-help/strcomp-function-HA001228914.aspx
http://msdn.microsoft.com/en-us/library/office/gg264346.aspx
45
8.2 Concatenation
The operator intended to perform string contatenation is &. The operator + can sometimes
be used to the same eect, but not always:
a = "123"
b = "456"
c = a & b 'Yields 123456
d = a + b 'Yields 123456: applied to two strings
Debug.Print c, d
a = 123
'A number, not a string
b = "456" 'A string
c = a & b 'Yields 123456
d = a + b 'Yields 579: applied to a number and to a string
Debug.Print c, d
8.3 Containment
To nd out if one string is a substring of another, use the InStr function as follows:
If InStr("Hello","He") > 0 Then
MsgBox "The second string is contained in the first string."
End If
If InStr("He","Hello") > 0 Then
MsgBox "This never gets printed; wrong argument order."
End If
InStr function returns the position of the substring if it is found or zero otherwise.
The two-argument use of InStr function is case-sensitive. A case-insensitive containment
test can be done as follows:
If InStr(UCase("Hello"), UCase("he")) > 0 Then
MsgBox "The second string is contained in the first string when we " & _
"disregard the case."
End If
8.4 Replacing
To replace a string with another string inside a third string, use the built-in function Replace
as follows:
Result = Replace("Teh Beatles", "Teh", "The") 'results into "The Beatles"
46
String constants
Mid$(String1, n, 1)
If you ask for more characters than are available, you get just what there is, no error is
raised:
Debug.Print Mid$("abcdef", 2, 33) 'Prints bcdef
'Prints aaaaaa
= "bbb"
'Prints abbbaa
= "cccccc"
'Prints abcbaa
= "d"
'Prints adcbaa
When Mid$ is on the left, it doesn't change the total length of the string: it just replaces
the specied number of characters. If the right-hand side species fewer characters than are
asked for, only that number of characters is replaced; if it species more, only the number
asked for is used.
Links:
MID, MIDB3 , Excel 2003, oce.microsoft.com
http://office.microsoft.com/en-gb/excel-help/mid-midb-HP005209175.aspx
47
48
String Functions
Returns a string corresponding to the given number. Like Val this is not locale aware.
This is the function you should use if you are creating a text le containing numbers to
be read on someone else's computer.
CStr
Converts the expression to a string. This procedure is locale aware and the correct function
to use if converting numbers and dierently typed values to strings for user-display. Usually
it is unnecessary because Visual Basic automatically converts when necessary and uses
Regional Settings to do so.
Format$
Converts a number to a string using a specic format. The format is provided as a string
of characters, that shows how many digits should be given before and after the decimal
point. Like CStr, Format$ is locale aware so the decimal separator will be whatever is
specied in the user's Regional Settings. Format$ also provides for conversion of dates to
various built-in and custom string formats.
CBool, CByte, CCur, CInt, CLng, CSng, CDbl, CDec
Locale aware conversions to Boolean, Byte, Currency, Integer, Long, Single, Double, Decimal.
Split
Chops a string into pieces and returns a Variant Array. If no delimiter is specied then
spaces will be used. Delimiters may be any string of any length. Two adjacent delimiters
delimit an empty string.
Hex$
Returns a string of Hex characters representing a number.
Oct$
Returns a string of Octal characters representing a number.
Replace$
Returns a string with occurrences of a specied substring replaced with a new string. Note
that the substring and the new string do not have to be the same size.
StrComp
Returns -1 if the rst string is less than the second, 0 if they are identical, +1 if the rst
is greater than the second. Takes an optional argument that determines the comparison
algorithm: vbBinary for exact comparisons using the character codes, vbTextCompare for
case insensitive comparisons.
49
One way is to use that Chr() function to specify the character code (34)
' Works fine
Debug.Print "Fred says " & Chr$(34) & "Hi" & Chr$(34) & " to you"
Another is to double the double-quotes. You might nd this more or less readable than the
above way.
' Works fine too
Debug.Print "Fred says ""Hi"" to you"
Furthermore, you can write these functions yourself using "Left" function:
Function StartsWith(Str1 As String, Str2 As String, Optional CaseIns As Boolean)
As Boolean
StartsWith = False
If CaseIns Then 'Case insensitive
If Left(LCase(Str1), Len(Str2)) = LCase(Str2) Then
StartsWith = True
End If
Else
If Left(Str1, Len(Str2)) = Str2 Then
StartsWith = True
End If
End If
End Function
Function EndsWith(Str1 As String, Str2 As String, Optional CaseIns As Boolean)
As Boolean
50
Pattern Matching
EndsWith = False
If CaseIns Then 'Case insensitive
If Right(LCase(Str1), Len(Str2)) = LCase(Str2) Then
EndsWith = True
End If
Else
If Right(Str1, Len(Str2)) = Str2 Then
EndsWith = True
End If
End If
End Function
Links:
Like Operator5 , Visual Basic for Applications, msdn.microsoft.com
Like Operator6 , Visual Studio 2005, msdn.microsoft.com
4
5
6
51
9 Regular Expressions
Sometimes, the built in string functions are not the most convenient or elegant solution to
the problem at hand. If the task involves manipulating complicated patterns of characters,
regular expressions can be a more eective tool than sequences of simple string functions.
Visual Basic has no built-in support for regular expressions. It can use regular expressions
via VBScript Regular Expression Library, though. If you have Internet Explorer installed,
you almost certainly have the library. To use it, you must add a reference to the project;
on the Project menu choose References and scroll down to Microsoft VBScript Regular
Expressions. There might be more than one version; if so, choose the one with the highest
version number, unless you have some particular reason to choose an old version, such as
compatibility with that version on another machine.
A method of constructing a regular expression object that requires that, in Excel, you set
a reference to Microsoft VBScript Regular Expressions:
Set Regexp = new RegExp
Regexp.Pattern = "[0-9][0-9]*"
53
Regular Expressions
An example of testing for match in which the whole string has to match:
Set RegExp = CreateObject("VBScript.RegExp")
RegExp.Pattern = "^[0-9][0-9]*$"
If RegExp.Test("354647") Then
MsgBox "Test 1 passed."
End If
If RegExp.Test("a354647") Then
MsgBox "Test 2 passed." 'This one does not pass
End If
54
Replacing
9.6 Replacing
An example of replacing all sequences of dashes with a single dash:
Set Regexp = CreateObject("VBScript.RegExp")
Regexp.Pattern = "--*"
Regexp.Global = True
Result = Regexp.Replace("A-B--C----D", "-") '="A-B-C-D"
An example of replacing doubled strings with their single version with the use of two sorts
of backreference:
Set Regexp = CreateObject("VBScript.RegExp")
Regexp.Pattern = "(.*)\1"
Regexp.Global = True
Result = Regexp.Replace("hellohello", "$1") '="hello"
9.7 Splitting
There is no direct support for splitting by a regular expression, but there is a workaround.
If you can assume that the split string does not contain Chr(1), you can rst replace the
separator regular expression with Chr(1), and then use the non-regexp split function on
Chr(1).
An example of splitting by a non-zero number of spaces:
SplitString = "a b c
d"
Set Regexp = CreateObject("VBScript.RegExp")
Regexp.Pattern = " *"
Regexp.Global = True
Result = Regexp.Replace(SplitString , Chr(1))
SplitArray = Split(Result, Chr(1))
For Each Element In SplitArray
MsgBox Element
Next
frame
<nowiki>
<html>
<head>
55
Regular Expressions
<title>RegEx Example</title>
</head>
<body>
<h1>RegEx Example</h1>
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
<h2>Level Two in RegEx Example</h2>
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
<h1>Level One</h1>
cccccccccccccccccccccccccccccccccccccc
<h2>Level Two in Level One</h2>
dddddddddddddddddddddddddddddddddddd
</body>
</html>
</nowiki>
What we want to do is extract the text in the two h1 elements and all the text between the
rst h1 and the second h1 as well as all the text between the second h1 element and the
end of body tag.
We could store the results in an array that looks like this:
"RegEx Example"
"Level One"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n<h2>Level
Two in RegEx Example</h2>\nbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
" cccccccccccccccccccccccccccccccccccccc\n<h2>Level Two in Level One</h2>\n
dddddddddddddddddddddddddddddddddddd"
The \n character sequences represent end of line marks. These could be any of carriage
return, line feed or carriage return followed by line feed.
A regular expression species patterns of characters to be matched and the result of the
matching process is a list of sub-strings that match either the whole expression or some
parts of the expression. An expression that does what we want might look like this:
"<h1>\s*([\s\S]*?)\s*</h1>"
The name of the item is in the SubMatches of each item but where is the text? To get that
we can simply use Mid$ together with the FirstIndex and Length properties of each match
to nd the start and nish of the text between the end of one h1 and the start of the next.
However, as usual there is a problem. The last match is not terminated by another h1
element but by the end of body tag. So our last match will include that tag and all the stu
that can follow the body. The solution is to use another expression to get just the body rst:
56
Example application
"([\s\S]*)"
This returns just one match with on sub-match and the sub match is everything between
the body and end body tags. Now we can use our original expression on this new string
and it should work.
Now that you have seen an example here is a detailed description of the expressions used
and the property settings of the Regular Expression object used.
A regular expression is simply a string of characters but some characters have special
meanings. In this expression:
"([\s\S]*)"
Each of these parts is also a regular expression. The rst and last are simple strings with
no meaning beyond the identity of the characters, they will match any string that includes
them as a substring.
The middle expression is rather more obscure. It matches absolutely any sequence of characters and also captures what it matches. Capturing is indicated by surrounding the expression
with round brackets. The text that is captured is returned as one of the SubMatches of a
match.
(
[
\s
\S
]
*
)
matches just
begins a capture expression
begins a character class
species the character class that includes all white space characters
species the character class that includes all non-white space characters
ends the character class
means that as many instances of the preceding expression as possible are to
be matched
ends the capture expression
matches
57
Regular Expressions
In the case studies section of this book there is a simple application that you can use to
test regular expressions: Regular Expression Tester1 .
1
2
3
4
58
http://en.wikibooks.org/wiki/..%2FRegular%20Expression%20Tester
http://www.regular-expressions.info/vb.html
http://www.regular-expressions.info/vbscript.html
http://msdn.microsoft.com/en-us/library/30wbz966%28v=vs.71%29.aspx
10 Arrays
10.1 Introduction
Arrays are extremely useful in Visual Basic, and are present in many other programming
languages. Arrays are used to group similar data together, to make it easier to search and
sort through this data. The best way to look at an array is to show you one. Say you
want to make a phone book for your computer. Rather than make a new variable with a
new name whenever you want to add a name, you can make an array. Arrays consist of
the variable name and a subscript, which is put in parentheses for computer use. So, if
you want to make a phone book of say, 100 people, rather than make a bunch of variables
named 'person1, person2, person3....person100', you may use an array. All you have to do
is dimension the array (see above). So now those 100 variables turn into person(1 to 100)
in a dim statement. Now you may be asking, what do I do now? Each object stored in an
array is called an element. What you have done is created an array which can hold 100
elements. If you want to print the 32nd person in your phone book, you simply would type:
Debug.Print Person(32)
The above shows an example of read-write iteration over an array. For Each loop can be
used for read-only iteration over an array:
Dim MyArray(9)
For i = 0 To 9
59
Arrays
MyArray(i) = i
Next
For Each Item In MyArray
Debug.Print Item
Next
10.3 Indices
Per default, array indices start at 0, unless "Option Base 1" declaration is used. Without
the declaration used, an array declared as "Dim MyArray(5)" has 6 elements: 0, 1, 2, 3, 4,
5.
The index range of an array has to be a continuous sequence of integers, including negative
numbers. Thus, the following is possible:
Dim MyArray1(-5 to 5)
Dim MyArray2(-10 to -5)
For i = LBound(MyArray1) to UBound(MyArray1)
MyArray1(i) = i + 5
Debug.Print i, MyArray1(i)
Next
For i = LBound(MyArray2) to UBound(MyArray2)
MyArray2(i) = i + 5
Debug.Print i, MyArray2(i)
Next
10.4 Size
The size of an array can be obtained using LBound and UBound as follows:
Dim MyArray1(-5 to 5)
Dim MyArray2(10-1)
Size1 = UBound(MyArray2) - LBound(MyArray2) + 1
Size2 = UBound(MyArray2) + 1 'For a same array with indexing starting at zero
Debug.Print Size1, Size2
60
Dynamic Arrays
StrNames(0) = "Alec"
StrNames(1) = "Jack"
StrNames(2) = "Pie"
But say you want to add more friends to your phone book, when a button is clicked, for
example. What do you do? You can dene a dynamic array instead:
Dim Names() As String
This array initially has no elements. You can add more elements, by using the ReDim
keyword:
ReDim Names(0 to 1) As String
This would create two new elements. You can assign names to these elements in the same
way as before:
Names(0) = "Alec"
Names(1) = "Jack"
But you'll end up losing all the old records! If you want to keep them, you must also use
the Preserve keyword:
ReDim Preserve Names(0 To 3) As String
Names(1) = "Eugene H / Orage" 'Whoa this person's name is too long to remember
To iterate AKA loop over the elements of a dynamic array, you have to use LBound
and UBound functions or For Each loop, as described at #Use of Arrays1 .
To add an element to an initialized dynamic array, you can proceed as follows, using
UBound function:
ReDim Preserve Names(0 To UBound(Names) + 1) As String
Names(UBound(Names)) = "my new friend"
However, UBound does not work with an un-initialized dynamic array, so the following
would cause a run-time error:
Dim MyArray() As String
Debug.Print UBound(MyArray)
61
Arrays
ReDim Preserve Names(0 To 0) As String
ArrayIsEmpty = False
End If
Names(UBound(Names)) = "my new friend"
Another way to avoid the run-time error is to catch the error created by the use of "UBound"
on an uninitialized array:
Dim MyArray() As String
NumberOfElements = 0
On Error Resume Next
NumberOfElements = UBound(MyArray) + 1
On Error GoTo 0
ReDim Preserve MyArray(0 To NumberOfElements) As String
MyArray(UBound(MyArray)) = "my new friend"
Finally, you can use the following hack to check if a dynamic array has been initialized:
Dim MyArray() As String
If (Not MyArray) = -1 Then
NumberOfElements = 0 'The array is uninitialised
Else
NumberOfElements = UBound(MyArray) + 1
End If
ReDim Preserve MyArray(0 To NumberOfElements) As String
MyArray(UBound(MyArray)) = "my new friend"
For very large arrays, the overhead of using variant arrays, which have the element type of
Variant rather than String or other narrower type, may well be restrictive, in which case
62
Multi-Dimensional Arrays
conventional dynamic arrays should be used. What seems also much faster than variant
arrays for adding items one at a time are ../Collections/2 .
The LBound and UBound functions can be used to nd the bounds of a particular dimension:
Debug.Print UBound(FunkyArray, 4)
Would give 4.
Using UBound without the second parameter gives the rst dimension
Debug.Print UBound(FunkyArray)
' Displays 2
Debug.Print UBound(VeryFunkyArray)
' Displays 2
63
Arrays
What do we have here? Essentially two arrays inside another. They can be referenced like
this:
Debug.Print VariantArray(0)(2)
Would show 3.
You can nest arrays like this to any depth and in any order and they can be of any size.
A note of caution must be taken when using the ReDim statement, however. You cannot
specically re-dimension a particular dimension of an array; instead you need to temporarily
copy to a variant.
Dim vtemp As Variant
vtemp = VariantArray(0)
ReDim vtemp(1+UBound(vtemp)) As Variant
vtemp(UBound(vtemp)) = 7
VariantArray(0) = vtemp
This will create a matrix that is 10 by 10 and comprised of integers. In reality, a matrix is
much like an array of arrays. The rst thing you'll want to do is know how to create a loop
for reading and lling a matrix. But before that, even, you should know how matrices are
structured. An example matrix would be:
1 2 3 4 5 6 7 8 9
1
2
3
4
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
This is a 4 by 9 matrix. When talking about matrices, rows are always stated before
columns. You will also notice column numbers travel from left to right, while row numbers
3
64
Sorting
travel from top to bottom. This is very important. And of course, matrices need not consist
of merely 1s. So, now you want to ll a matrix? Its very similar to an array.
For Row = 0 To 3
For Column = 0 To 8
Matrix(Row, Column)=1
Next
Next
This will of course ll the matrix with nothing but ones, but random numbers may be used
(see references). As you may notice, this involves nested loops. The loop lls left to right,
top to bottom (the same directions we use when reading).
10.11 Sorting
For sorting variant arrays,
vba-array-sort-function.
see
http://stackoverflow.com/questions/152319/
65
11 Collections
Collection class provides an array-like container more exible than an array in some ways
and less exible in other ways. More exible in some ways than Collection is Dictionary
class.
Creating a new collection:
Dim Cats As Collection
Set Cats = New Collection
Adding an item:
Cats.Add "Item"
Cats.Add "Item", "Key"
...
Removing an item:
Collection.Remove Index
Collection.Remove HashKey
67
Collections
'Increment each item by 1
For i = 1 To MyCollection.Count
NewValue = MyCollection(i) + 1
MyCollection.Add NewValue, Before:=i
MyCollection.Remove Index:=i + 1
Next
68
Sorting
' The key has to be a string, hence "Str(i)" below.
Set NumberSet = New Collection
For i = 1 To 10
NumberSet.Add i, Str(i) 'Value, Key
Next
For i = 5 To 15
'Add while catching errors resulting from existence of the key
On Error Resume Next
NumberSet.Add i, Str(i) 'Value, Key
On Error GoTo 0
'End If
Next
For i = 14 To 16
'Remove
On Error Resume Next 'Catch error if element not present
NumberSet.Remove Str(i)
On Error GoTo 0
Next
Set NumberSet2 = New Collection
For i = 10 To 25
NumberSet2.Add i, Str(i)
Next
'Union
Set SetUnion = New Collection
For Each Item In NumberSet
SetUnion.Add Item, Str(Item) 'Value, Key
Next
For Each Item In NumberSet2
On Error Resume Next
SetUnion.Add Item, Str(Item) 'Value, Key
On Error GoTo 0
Next
'Intersection
Set SetIntersection = New Collection
For Each Item In NumberSet
On Error Resume Next
Dummy = NumberSet2(Str(Item))
If Err.Number = 0 Then
SetIntersection.Add Item, Str(Item) 'Value, Key
End If
On Error GoTo 0
Next
'Set difference
Set SetDifference = New Collection
For Each Item In NumberSet
On Error Resume Next
Dummy = NumberSet2(Str(Item))
If Err.Number <> 0 Then
SetDifference.Add Item, Str(Item) 'Value, Key
End If
On Error GoTo 0
Next
69
12 Dictionaries
Dictionary class provides some functions beyond those provided by Collection class. A
dictionary is a list of key, value pairs. Unlike Collection, Dictionary class is only available
via Microsoft Scripting Runtime, a reference to which needs to be added to your project in
order to use the class.
Creating a dictionary:
Set Dict = New Dictionary
Set Dict = CreateObject("Scripting.Dictionary") 'An alternative
Removing an item:
Dict.Remove "Key3"
71
Dictionaries
Removing all items or emptying:
Dict.RemoveAll
72
13 Data Types
Data types in Visual Basic can be divided into three groups:
Native: Types that are understood directly by the Visual Basic compiler without assistance from the programmer
User-dened: commonly referred to by the initials UDT, meaning User dened Type,
these correspond to Pascal records or C structs
Classes: the basis for object oriented programming in Visual Basic. Classes include
forms, add-ins, and database designers.
73
Data Types
Object
a holder for any type of Object.
Variant
a holder for any type of value or object.
a as Byte
i as Integer
x,y as Long 'Define two variables. Note that only the last variable will
long integer.
Now those variables will only be capable of storing integer values (without decimal). Long
integers can store a number with a bigger range of value than integers but they occupy a
bigger space of RAM.
Type
Byte
Integer
Long
Storage
1 byte
2 bytes
4 bytes
Range of Values
0 to 255
-32,768 to 32,767
-2,147,483,648 to 2,147,483,647
Storage
4 bytes
Double
8 bytes
Range of Values
-3.402823E+38 to -1.401298E-45 for negative
values1.401298E-45 to 3.402823E+38 for positive values.
-1.79769313486232e+308 to -4.94065645841247E324 for negative values4.94065645841247E-324 to
1.79769313486232e+308 for positive values.
74
String
Round() rounds o a decimal to a certain number of decimal digits that the programmer
wants. The rst argument should be a decimal value which you want to round o. The
second argument species the number of decimal digits you want, for example:
Dim pi as Double
pi=3.141592653589
pi=Round(pi,2) 'Rounds off 3.141592653589 to only two decimal digits
Print pi 'Prints 3.14
13.4 String
A string is an array of characters. As an example:
Dim a As String
a = "This is a string"
Strings can be concatenated (connected together to form a new string) using the "&"
operator. For example,
dim b as String
b = "Wiki" & "book" & "s"
Print b 'Prints "Wikibooks"
A normal string variable occupies 10 bytes of RAM, plus the string's size, and can hold up
to 2 billion characters!
Some frequently used built-in string constants: vbTab, vbCrLf
vbTab contains a string that does the same thing as the Tab key on your keyboard, while
vbCrLf creates a character return and a line feed(similar to the Enter key):
Print "Jack:" & vbTab & "1 pie" & vbCrLf & "me:" & vbTab & "10 pies"
Will print:
Jack:
me:
1 pie
10 pies
To include special characters and quotation marks in the strings, the Chr() function may
be used:
Dim a As String
Print "A quotation mark: [" & Chr(34) & "]"
a = "Replace 'apostrophes' for quotation marks"
Replace( a, "'", Chr(34) )
Print a
75
Data Types
Str() converts any numerical value into a string value while Val() converts a string value
into a numerical value(only when it's convertible).
Dim MyString As String
Dim MyNumber As Single
MyString=Str(300) 'converts a number into a string
MyNumber=Val("300") 'converts a string into a number
Even if you don't do the conversion, you will not end up getting Type Mismatch Errors.
However, it is considered better to use explicit type conversions, because it is easier to
debug.
Even if you do be prepared for next to impossible to debug problems caused by VB refusing
to convert something and refusing to tell you what the heck it is. VB is extremely touchy
and raises an exception at the least expected times.
13.5 Structure
An example denition of a structured type:
Type E2Point
x As Double
y As Double
End Type
Sub Test()
Dim MyPoint As E2Point
Dim MyPoint2 As E2Point
MyPoint.x = 4
MyPoint.y = -5
MyPoint2 = MyPoint 'Make a copy
MyPoint2.x = 3
MyPoint2.y = -6
Debug.Print MyPoint.x, MyPoint.y '4, -5: not overriden with 3 and -6
Debug.Print TypeOf MyPoint Is E2Point 'True
Debug.Print TypeOf MyPoint Is Object 'False
End Sub
13.6 Enumeratation
An example denition of an enumerated type:
1
2
76
http://msdn.microsoft.com/en-us/library/office/gg278730.aspx
http://msdn.microsoft.com/en-us/library/aa266315.aspx
Type Test
Enum Colors
Red '=0
Green '=1
Blue '=2
End Enum
Enum Colors2
Red2 = 1
Green2 '=2
Blue2 '=3
End Enum
Sub Test()
Debug.Print Red, Green, Blue
Debug.Print Red2, Green2, Blue2
Dim MyVar As Colors 'Ends up being typed as Long
MyVar = 8 'Does not lead to an error: no restriction on values
End Sub
Links:
Enum Statement3 , Oce 2013, msdn.microsoft.com
Enum Statement4 at Visual Basic for Applications Reference, msdn.microsoft.com
You can further nd out about a type using IsObject and TypeName functions:
Debug.Print IsObject(Selection) 'True
3
4
http://msdn.microsoft.com/en-us/library/office/gg251516.aspx
http://msdn.microsoft.com/en-us/library/aa243358.aspx
77
Data Types
Debug.Print
Debug.Print
Debug.Print
Debug.Print
Debug.Print
IsObject("Hello") 'False
TypeName("Hello") 'String
TypeName(4) 'Integer
TypeName(3.5) 'Double
TypeName(Selection) 'Range
Links:
If...Then...Else Statement5 at Visual Basic for Applications Reference,
msdn.microsoft.com
TypeName Function6 at Visual Basic for Applications Reference, msdn.microsoft.com
IsObject Function7 at Visual Basic for Applications Reference, msdn.microsoft.com
IsArray Function8 at Visual Basic for Applications Reference, msdn.microsoft.com
IsDate Function9 at Visual Basic for Applications Reference, msdn.microsoft.com
IsNumeric Function10 at Visual Basic for Applications Reference, msdn.microsoft.com
IsNull Function11 at Visual Basic for Applications Reference, msdn.microsoft.com
5
6
7
8
9
10
11
78
http://msdn.microsoft.com/en-us/library/aa243382.aspx
http://msdn.microsoft.com/en-us/library/aa263394.aspx
http://msdn.microsoft.com/en-us/library/aa445054.aspx
http://msdn.microsoft.com/en-us/library/aa445040.aspx
http://msdn.microsoft.com/en-us/library/aa445042.aspx
http://msdn.microsoft.com/en-us/library/aa445052.aspx
http://msdn.microsoft.com/en-us/library/aa445050.aspx
79
14.4 Visibility
This seems like a very unimportant part of a procedure declaration to most people but in
fact it is a very helpful feature. With it you can show that some procedures are just for use
inside a module (Private), some only for use in this component (Friend) or available for the
whole world (Public). You should mark procedures Private unless they will be called from
outside the module. this will encourage you, and anyone who edits your program, to place
related procedures in the same module which obviously makes maintenance easier.
Marking a procedure Private also means that you can have another procedure with exactly
the same name in another module.
80
Early Termination
The two procedures do essentially the same thing, that is, divide a number by two. The
Function version does it by assigning the new value to the name of the function while the
Sub version assigns it to the name of one of the arguments. This aects how you use them.
The function version can be used in an expression as follows:
Debug.Print FunctionHalf(10) 'Prints 5
To use the subroutine to return value, you need to store the value in a variable, like this:
Dim nHalf as Double
SubroutineHalf 10, nHalf
Debug.Print nHalf
Generally, you use a Function when the result is a single thing (number, string, object) and
a Sub when you either want to return several distinct things or nothing at all.
Properties are also a form of procedure. Property Get is a function; Property Let and
Property Set are subroutines. For more discussion of Properties, see the ../Object Oriented
Programming/2 chapter.
81
Once an argument is declared optional, all arguments at the right of it have to be declared
optional as well.
Links:
Using Optional Arguments3 , Oce 2000, msdn.microsoft.com
82
http://msdn.microsoft.com/en-us/library/office/aa164532.aspx
15 Error Handling
Error handling in Visual Basic, an outline:
Suppressing the error and detecting it using the non-zero error number
Set MyCollection = New Collection
MyCollection.Add "Item", "Item"
On Error Resume Next
MyCollection.Add "Item", "Item" 'This result in an error
MyErrNumber = Err.Number
On Error Goto 0 'Restore the absence of error handling
If MyErrNumber <> 0 Then
'Error occurred
MsgBox "Item already present in the collection."
End If
83
Error Handling
Case Else
'...
End Select
End Sub
See
also
../Eective
Programming#Errors_and_Exceptions1
ing_Standards#Error_Handling2 .
1
2
84
and
../Cod-
16 Files
Another essential part of Visual Basic is le Input/Output, that is, working with les.
While programming, you may want at some point to save data so they may be accessible
for further use. This is where le I/O comes in. VB allows us to perform most operations
available in Windows Explorer and DOS command line. Before you can do this, you need
to understand how le I/O works.
This is obviously data containing names. Each line of data is called a record. So the rst
record contains Bob, Larry, and George. A eld is each item in a record. So the rst eld
in the rst record would be Bob, the second eld would be Larry, and so on. There may
be as many elds and records as you see t.
Records and elds are created really just for the programmer's convenience. For greater
exibility, you can also view the whole thing as a string. And you can split the string into
records and elds yourself with the built-in Split() function.
16.2 Input
When a program receives data from an outside source, it is considered input. In Visual
Basic, this source is usually a standard text le, with a .txt le extension(as which could
be read with Notepad). First, you need to open the le from which you want to read the
data. This is done using the following code:
Open <filename> For <mode> As <channelnumber>
For example:
85
Files
The le path can be anything you want, if it doesn't exist, a new le (and directory(s)) will
be created. The extension of the le doesn't matter much. It will not aect the content of
the le nor the process of writing/reading the le. So you can also do this:
Open "c:\filename.myfile" For Input As #1
Or this:
Open "c:\filename" For Input As #1
The open and le path are self explanatory. However, "for input" states it is being used to
receive data from an outside le, VB is "inputting" data from a le into variables . When
outputting(writing to a le), this is changed to "for output". When we want to open a le
in binary, it's "for binary". "As #1" is which channel is being used. When you are seeking
specic information from the le you would use "for random". A channel could be seen
as Visual Basic setting up a pathway to the le. Only one le can be used on a channel
at a time. So if you want to open multiple les without closing any, you can use dierent
channels. To input a line of data, it is fairly simply:
Input <channel>, <variables>
For example:
Input #1, x, y, z
This will read data from whatever channel 1 is connected to. It will store the rst eld of
the current record into X, and the second eld into Y, and the third into Z.
Finally, we should ALWAYS close the le after the work is done. The syntax is simple:
Close <channel>
As an example:
Close #1
86
Output
16.3 Output
Output is very similar to input. The only dierence is, well, it sends information OUT of
the program and into a le. Once again, we shall only use text les (.txt). First you must
open the le:
Open "C:\filepath.txt" For Output As #1
You may have noticed, the only thing that diers from opening a le for input, is the "For
Output". Once this is done, everything else is simple.
Write #1, x, 5
You can write any value or variable to a le, however it is important to note that, when
writing a string, it should be placed in quotes. If it is a variable containing a string, this is
done automatically. Another important note is that two consecutive write statements will
write to separate lines. For example:
Write #1, 5, 4
Write #1, 7, 4
Write #1, "this is a string"
Keep in mind, when outputting to a le, it will ERASE all prior information in the text.
To add on to existed information, the "Append" command must be used.
If you are writing data to a le for use by some other program, you may nd that you
don't want the quotes that VB puts around your strings. You can avoid writing the quotes
by using the Print command instead of the Write command. For example:
87
Files
If, during runtime, the le at hand was modied, and a new line was added which contained
necessary data, and the open statement ended as "#1", whatever was in line 1 of the le
would be written over(only if the le was opened as Output). To solve this, a variable is
dimmed as FreeFile (Dim FF as FreeFile). This will open the rst free slot or line in the
le at hand. The new Open statement would become:
Open "C:\filepath.txt" For Output As FF
The variable, s, simply needs to hold a path to the le. The path can be an absolute path
such as:
s = "d:\users\me\my file.txt"
88
Examples
s = "my file.txt"
or like this:
s = "mysub\myother file.txt"
For the more adventurous it can also include a reference to a shared le on a server that
doesn't use a drive letter:
s = "\\someserver\someshare\somefile.txt"
16.5 Examples
An example form using le reading can be found here.1 Right click 'here' in Internet
Explorer and then 'Save Target As...' to save the form le.
Please upload more les with examples. It will be really helpful for the developer.
http://www.freewebs.com/3zzzpmk/TextView.frm
89
17 User Interfaces
A user interface is the part of your program that is visible to a human user. It can be as
simple as a command line1 or as sophisticated as an immersive virtual reality simulator (see
Death Dream by Ben Bova2 ). However, in the context of Visual Basic it usually means
what is commonly referred to as a Graphical User Interface or GUI which generally consists
of one or more Forms that contain text boxes, labels, buttons, picture boxes, etc.
A more common example of a GUI would be a standard EXE( Windows Forms3 ).
1
2
3
http://en.wikibooks.org/wiki/Guide%20to%20Windows%20commands
http://www.ibdof.com/IBDOF-book-detailedview.php?book_id=5023
http://en.wikipedia.org/wiki/Windows_Forms
91
18 Simple Graphics
This chapter is a simple introduction to drawing simple graphics in Visual Basic Classic. It is
possible to do very sophisticated graphics programming in VB but here we will concentrate
of the basics.
There are two ways of creating simple two dimensional graphics in VB:
draw lines, boxes, circles, and ellipses at run time by executing statements or
add shape objects to the form at design time.
If you know in advance exactly what to draw, then it might be simpler to use the second
method, as you can then see exactly what you will get as you drag and drop shapes to the
form. However if the picture is to be generated based on, say, some statistics, you might be
better o using the drawing methods of a Form or PictureBox.
93
Simple Graphics
Figure 3
Note that you can modify the properties of Shape and Line controls at run time just as
you can any other control. You can also create them at run time, either by using Control
Arrays or by using the CreateObject function.
94
19 Windows Dialogs
Windows dialogs are useful when you would like to retrieve information from the user, as in
asking where to save a le, letting the user to choose a color or font and what settings to use
when printing (copies, what printer to use, ect.). This saves you from a lot of unnecessary
work reinventing the wheel - as redesigning and making dialogs can be quite time-consuming
- but is also essential in establishing a standard user interface across applications. Standardization of dierent aspects of the user interface, both in terms of normal widgets1 as
well as common ways of combining them, ensures that applications are intuitive - i.e. that
once someone has learned to use an application, this knowledge can be employed to other
applications as well.
The easiest way to access these controls is to use a component called Microsoft Common
Dialog Control 6.0. To add this component to your project, choose Project - Components,
and mark it in the controls list. Subsequently, a new control should appear in your control
toolbox. Choose it, and place the control onto your form (note it's only visible in design
time).
The control allows the use of the following dialogs. The ags mentioned is dierent values
that can be set to the Flags property, changing the behaviour or design of the dierent
dialogs.
http://en.wikipedia.org/wiki/widget_%28computing%29
95
Windows Dialogs
Figure 4
Here, the description is the string that shows up in the list box of all the available lters to
choose from. The lter is a le glob expression (otherwise known as a wild card expression)
that does the actually ltering, for example, "*.txt", which disallows any le extensions
but TXT. It is important to recognize the dierence between expressions of this kind and
regular expressions which are much more powerful but also more complicated, see ../Regular
Expressions/2 .
96
Color dialog
With CommonDialog1
' Raises an error when the user press cancel
.CancelError = True
' Set the file extensions to allow
CommonDialog1.Filter = "All Files (*.*)|*.*|TextFiles
(*.txt)|*.txt|Batch Files (*.bat)|*.bat"
' Display the open dialog box.
.ShowOpen
' Ignore this if the user has canceled the dialog
If Err <> cdlCancel Then
' Open the file here using FileName
MsgBox "You've opened '" & .FileName & "'"
End If
End With
97
Windows Dialogs
Figure 5
98
Font dialog
' Raises an error when the user press cancel
.CancelError = True
' Set the Flags property.
.Flags = cdlCCRGBInit
' Display the Color dialog box.
.ShowColor
' Ignore this if the user has canceled the dialog
If Err <> cdlCancel Then
' Set the form's background color to the selected color.
Me.BackColor = .Color
End If
End With
Figure 6
99
Windows Dialogs
19.3.1 Properties
Property:
Color
FontBold
FontItalic
FontStrikethru
FontUnderline
FontName
FontSize
Determines:
The selected color. To use this property, you must rst set the
Flags property to cdlCFEects.
Returns whether the bold checkbox was selected.
Returns whether the italic checkbox was selected.
Returns whether the strikethrough checkbox was selected.
Returns whether the underline checkbox was selected.
Returns the selected font name.
Returns the selected font size.
100
Print dialog
Figure 7
19.4.1 Properties
Property:
Copies
FromPage
ToPage
hDC
Determines:
The amount of copies to print.
The starting page of the print range.
The ending page of the print range.
The device context for the selected printer.
101
Windows Dialogs
Property:
Orientation
Determines:
The page's orientation (as in portrait or landscape).
102
20 Databases
Database management as a subject in its own right is far too big to be dealt with in this
book and would distract from the principal subject which is programming in VB Classic.
Nonetheless we can explain the basics of connecting to databases, retrieving information
from them and so on.
The usual way to connect to a database from VB is to use ADO (ActiveX Data Objects).
Connecting to databases usually happens with a connection string. Here are some example
connection strings to connect to some common database types
A
ACCESS
ODBC
Standard Security
Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\mydatabase.mdb;Uid=Admin;Pwd=;
Workgroup
Driver={Microsoft Access Driver
(*.mdb)};Dbq=C:\mydatabase.mdb;SystemDB=C:\mydatabase.mdw;
Exclusive
Driver={Microsoft Access Driver
(*.mdb)};Dbq=C:\mydatabase.mdb;Exclusive=1;Uid=admin;Pwd=;
103
Databases
Specifying locale identier Use this to specify the locale identier which can help with
non-US formatted dates.
Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\mydatabase.mdb;Locale
Identifier=2057;Uid=Admin;Pwd=;
With database password This is the connection string to use when you have an access
database protected with a password using the Set Database Password function in Access.
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;Jet
OLEDB:Database Password=MyDbPassword;
DataDirectory functionality
Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=|DataDirectory|\myDatabase.mdb;User Id=admin;Password=;
104
Print dialog
Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\myFolder\myAccess2007file.accdb;Persist Security Info=False;
Datadirectory functionality
Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=|DataDirectory|\myAccess2007file.accdb;Persist Security Info=False;
E
Excel Files
ODBC
OLEDB
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended
Properties="Excel 8.0;HDR=Yes;IMEX=1";
Important note!
The quote " in the string needs to be escaped using your language specic escape syntax.
c#, c++
\"
VB6, VBScript
""
xml (web.config etc)
"
or maybe use a single quote '.
"HDR=Yes;" indicates that the rst row contains columnnames, not data. "HDR=No;"
indicates the opposite.
"IMEX=1;" tells the driver to always read "intermixed" (numbers, dates, strings etc) data
columns as text. Note that this option might aect Excel worksheet write access negative.
105
Databases
SQL syntax "SELECT * FROM [sheet1$]". -- i.e. Excel worksheet name followed by
a "$" and wrapped in "[" "]" brackets.
106
21 Windows API
APIs, short for "application programming interface", allows you to access a wide array
of functions and methods exposed by the operating system or other applications (DLLs),
performing operations not normally implemented by the standard virtual machine that
Visual Basic provides. This includes operations such as shutting down the computer or
listing all the current running processes.
Although VB6 compiler can't create true DLLs, it has ability to call existing DLLs (not
necessarily just Windows API of course). Declaration of such function contains extra keyword declare and lib, plus optionally can be dened an alias if the real name of function
diers from name of dll function alias. For example it can be:
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As
Any, pSource As Any, ByVal ByteLen As Long)
For datatype Any accepts compiler any parameter provided. For eective using of API calls
are important operators VarPtr which returns pointer at variable specied and AddressOf
which is important for callback functions. Beginners often do mistake on missing or badly
specied ByRef/Byval in declaration. Simply said, ByVal means that on stack will be
pushed value and ByRef (or missing specication of calling convention) means that on
stack will be pushed pointer at variable. In practice parameter type Long (4 bytes) will be
called ByVal, but String or complex data types will be called ByRef.
Unfortunately, VB6 doesn't support C-like memory handling. To deal with this problem
VB programmers uses function CopyMemory to copy memory like this:
Dim a As Long
Dim b() As Byte
ReDim b(3)
CopyMemory ByVal varptr(a), ByVal varptr(b), 4
which is relatively safe. Some of programmers at the higher level of knowledge can modify pointers at objects as well. Although most of API functions can be called this way,
there is not possible to call entry points of dlls dynamically (LoadLibrary-GetEntryPointFreeLibrary) for general case. Much clearer way how to deal with such tasks is to move
code of this kind into separated dll written in C++ and call this dll.
21.1 Declaration
An example of a declaration is the GetTickCount function which returns the amount of
milliseconds that have elapsed since Windows was started. To use it, insert the following
into a standard module in your project:
107
Windows API
To access it, simply call it like you would call any normal function. Note, however, that
this is a Public declaration and is limited to modules. If you don't need it to be accessible
in your entire project, use the Private declaration and insert it in the class module or form
directly:
Private Declare Function GetTickCount Lib "kernel32.dll" () As Long
21.2 Resources
For a beginner it is normally dicult to create a declare statement since it requires knowing
how to map C datatypes to Visual basic datatypes. There is a built in program that comes
along with VB6 called API Text Viewer which lists all commonly used API functions,
contants and types. A VB programmer can just nd the required function from said program
and insert into the VB module.
108
22 Subclassing
Subclassing can be useful when you want to add functionality that is not directly supported
by Visual Basic. To explain how this works we must rst go through a little background
theory.
All windows in Windows (for example a form, a button, a listbox, etc.) have a function
that the operating system or other programs can call to communicate with the program.
Windows can, for example, send messages about events such as the mouse pointer moving
over the window, a key being pressed when the window has the focus and much more.
Programs can also send messages that ask for information about the window; for example,
the EM_GETLINECOUNT message asks a textbox to send back the number of lines in
the text it holds. One can also dene one's own functions.
To call these special functions you can use PostMessage, SendMessage or CallWindowProc
(the last only if you know the address of the function).
Usually such a procedure looks something like this:
Public Function WindowProc(ByVal
ByVal
ByVal
ByVal
hwnd As Long, _
uMsg As Long, _
wParam As Long, _
lParam As Long) As Long
In this function hwnd is the handle of the window that the caller has tried to contact; uMsg
is the message identier which says what the call is about; wParam and lParam are used
for whatever purpose the caller and window agree on. The handle, hwnd, is not an address
but is used by Windows to nd the address.
If, for example, we want to set the text that appears on the title bar of a form we can use
the following code:
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Integer, _
ByVal lParam As Any) As Long
109
Subclassing
Private Const WM_SETTEXT = &HC
Private Sub Form_Load()
SendMessage Me.hwnd, WM_SETTEXT, 0&, "This is a test"
End Sub
The receiver will get this message via its window function, which will look
something like this:
Public Function WindowProc(ByVal
ByVal
ByVal
ByVal
'
'
'
'
hwnd As Long, _
uMsg As Long, _
wParam As Long, _
lParam As Long) As Long
' It doesn't actually look like this of course, but this gives
' a good enough impression of what happens under the surface
Select Case uMsg
Case WM_SETTEXT
Me.Caption = lParam
'Case ...
'... many more here
End Select
End Function
22.2 Example
To specify that our window function is to be used we use the API call SetWindowLong.
Study the following example and put it in a base module:
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc As Long, _
ByVal hwnd As Long, _
ByVal Msg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Declare Function SetClipboardViewer Lib "user32" _
(ByVal hwnd As Long) As Long
110
Example
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Integer, _
ByVal lParam As Any) As Long
Public Const WM_SETTEXT = &HC
Public Const GWL_WNDPROC = (-4)
Private PrevProc As Long ' The address of the original window function
Public Sub SubclassForm(F As Form)
' AddressOf WindowProc = finds the address of a function
PrevProc = SetWindowLong(F.hwnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub
Public Sub UnSubclassForm(F As Form)
' It is _very_ important that we restore the original window function,
' because VB will crash if we don't.
SetWindowLong F.hwnd, GWL_WNDPROC, PrevProc
End Sub
Public Function WindowProc(ByVal
ByVal
ByVal
ByVal
hwnd As Long, _
uMsg As Long, _
wParam As Long, _
lParam As Long) As Long
When you click the cmdTest button, you'll see that the text that appears is not "This is a
test", but "Subclassing".
111
23 External Processes
It is often the case that a program already exists to do some job so rather than rewrite the
program in Visual Basic it is more ecient to run the existing program from Visual Basic.
This can work very well for command line programs or scripts written in any language.
The general scheme is to create a string containing the command line, call a function to
run it and then wait for the command to complete. There are many renements that can
be made including all sorts of ways of sending data to the new process, reading results from
it, pausing it, setting its priority and so on.
Shell is a built in Visual Basic function that executes a command line and returns a handle
to that process. Shell takes an optional argument that controls the window style of the new
process (maximized, normal, hidden, etc.).
Unfortunately running another program like this doesn't give you much control over what is
happening. In particular, there is no obvious way to nd out if the program has completed
its work.
Luckily Shell actually does slightly more than launch the program. It also returns the process
id. This is the number displayed by Task Managers and can be used to check the status of
the process. Unfortunately, as is so often the case with Visual Basic and Windows, there is
no built in function nor even a single API1 function that can use it directly. Therefore, we
must write a little more code. First, we must declare a few API2 functions and constants:
Option Explicit
Const SYNCHRONIZE = &H100000
Const INFINITE = &HFFFF
'Wait forever
Const WAIT_OBJECT_0 = 0
'The state of the specified object is signaled.
Const WAIT_TIMEOUT = &H102 'The time-out interval elapsed, and the objects
state is nonsignaled.
Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) _
1
2
113
External Processes
As Long
Private Declare Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, _
ByVal dwMilliseconds As Long) _
As Long
Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) _
As Long
Dim ProcessID As Long
Dim hProcess As Long
Here is an example of a function that executes a command and waits for it to complete:
Function ShellWait(CommandLine As String, _
TimeOut As Long, _
WindowState As VbAppWinStyle) As Boolean
Dim ProcessID As Long
Dim hProcess As Long
ProcessID = Shell(CommandLine,WindowState)
If ProcessID <> 0 Then
'non-zero (True) so Shell worked
' Get a process handle for the PID (Wait takes a handle)
hProcess = OpenProcess(SYNCHRONIZE, False, ProcessID)
If hProcess <> 0 Then
' Got process handle
' Wait until process finishes before going on
If WaitForSingleObject(hProcess, TimeOut) = WAIT_OBJECT_0 Then
ShellWait = True
Else
ShellWait = False
End If
Else
'Failed to get process handle.
'Perhaps the process terminated very quickly
'or it might not really have executed at all even though Windows
' started a process.
ShellWait = False
End If
Else
' PID zero (False) so Shell failed
ShellWait = False
End If
End Function
23.2 Exercises
Modify the ShellWait example function so that the caller can distinguish between failure
to launch the process and timeout.
Suggest a reason why this might be a good idea.
114
'What colour?
'What kind of fruit?
'How much?
String, f As String, v As Single)
'Make some
Behold. That's our rst class. Don't worry about anything right now. I will go over those
later. Anyway, let's assume that this class is named, "Juice". Think of it as a general
description of a bottle of juice. Now we can create an actual bottle of apple juice:
Private AppleJuice As Juice 'Define
Set AppleJuice = New Juice
'Create a new instance of Juice
AppleJuice.MakeJuice "brown", "apple", 30
Watch:
AppleJuice.Evaporate
115
'10 left
'Yes!!
"Wait", you interrupted my rant once again, "but why do I have to use OOP? Just because
you hate apple juice?"
OOP is good for large-scale programming - As your code grows large, there are going to
be more and more procedures/functions to your program, and your code is going to be
so cluttered that one more look at it, you'd scream. That is why I'm teaching you OOP!
(So instead of looking at hundreds of scattered procedures/functions, you look at organized
Objects.)
24.1 Types
You can think of a type as a simple form of class. Types can only hold variables, not
procedures/functions.
An important distinction between user dened types ('UDT') and 'classes' in 'VB6' is that
'UDTs' are value types while 'classes' are reference types. This means that when you store
an object ('instance' of a 'class') in a variable what actually happens is that a pointer to
the existing object is stored but when you store a 'UDT' instance in a variable a copy of
the instance is stored. For instance imagine that you have dened a class, 'Class1', and a
UDT, 'Type1':
Dim
Dim
Dim
Dim
ac
bc
at
bt
As
As
As
As
Class1
Class1
Type1
Type1
and at to bt
bt = at
Now make a change to one of the properties of bc and look at the corresponding property
in ac. You should see that when you change bc that ac also changes. This is because ac
and bc are really pointers to the same block of memory. If you do the same exercise with
bt and at you should see that changing properties of bt does not aect at. This is because
at and bt are values not references and occupy distinct locations in memory.
This is important when storing objects in 'Collections'. If you add an object to a collection
you can later change the values of its properties and the object in the collection will also
seem to change. in fact it is the same object because what is stored in the collection is the
reference ('pointer') to the object. However if you store a 'UDT' in a collection what is
stored is a copy of the values not a reference so changing the original UDT will not aect
the content of the Collection.
116
Classes
24.2 Classes
Classes are a combination of subroutines and types. By that I mean that when you write a
class it looks very much like a type1 but is also contains subroutines and functions.
Each class in VB6 is a distinct le, there can only be one class in a le and the class cannot
be spread out in several les; this is a feature of VB6 not a requirement of object oriented
programming.
A class looks very much like an ordinary module but has the le extension .cls instead of
.bas
24.3 Inheritance
There are two types of inheritance: implementation and interface. However Visual Basic
Classic provides only Interface inheritance. Implementation inheritance can be simulated
by a combination of interface inheritance and delegation.
One of the common ways to introduce inheritance is to show a program that creates dog
and cat objects or car and train objects.
For example in Visual Basic we can declare two classes dog and cat like this:
Chapter 12 on page 72
117
Now this is all very well until you decide that you would like an array of pets:
Dim aPets(1 to 10) as ????
Unless you declare the aPets array as variant you have to give it a type. One way is to
create a class called cPet and use it instead of cDog and cCat:
118
Inheritance
This should print:
Woof
Woof
Woof
Woof
Woof
Meow
Meow
Meow
Meow
Meow
Look reasonable? For the requirements so far revealed this works ne. But what happens
when you discover that there are things that cats do that have no analogue in dogs? What
about other kinds of pet? For example, you want your cat to purr. You can have a method
called purr but where will you put it? It surely doesn't belong in cPet because dogs can't
purr and neither can guinea pigs or sh.
The thing to do is to separate out those features that are common to all pets and create a
class that deals only with those, we can rewrite cPet for this purpose. Now we can go back
to our original cDog and cCat classes and modify them to inherit the 'interfacefromcPet.
Notice that the Sound method is empty. this is because it exists only to dene the interface.
The interface is like the layout of the pins on a plug or socket; any plug that has the same
layout, size and shape will plug in to a socket implementing the same interface. What goes
on inside the thing you plug in is a separate issue; vacuum cleaners and television sets both
use the same mains plug.
To make this work we must now modify cDog and cCat:
119
24.4 Collections
In the pet examples above an array of cPet objects is used to hold the pets. This works
well if you know exactly how many pets you have. But what happens if you adopt another
stray puppy or your cat has kittens?
One solution is to use a Collection object instead of an array. A collection object looks a
little like an array but lets you add and remove items without worrying about the declared
size, it just expands and contracts automatically.
24.5 Iterators
120
25 Eective Programming
When programming eectively in any computer language, whether it is VB or C++ for
example, there should be consistency to your style, it should be organized, and should
aim to be as ecient as possible in terms of speed of execution and use of resources (such
as memory or network trac). With established programming techniques, errors can be
reduced to a minimum and be more easily recognized, and it will make the job of the
programmer much easier and more enjoyable.
There are many dierent aspects to writing reliable programs. For a short program used
interactively and only by the author it can be reasonable to break all the rules in the service
of quickly getting the answer. However, if that little programs grows into a large program
you will end up wishing that you had started on the right path. Each programming language
has its strengths and weaknesses and a technique that assists in writing good programs in
one might be unnecessary, impossible or counterproductive in another; what is presented
here applies to VB6 in particular but much of it is standard stu that applies or is enforced
in Pascal, C, Java and other similar imperative languages.
121
Eective Programming
Have as few variables as possible declared Public in .bas modules; such variables are
public to the whole component or program.
Group related functions and subs together in a module, create a new module for unrelated
routines,
If a group of variables and procedures are closely related consider creating a class to
encapsulate them together,
Include assertions in the code to ensure that routines are given correct data and return
correct data,
Write and execute tests,
Make the program work rst and work fast afterwards
Where a variable can hold a limited range of discrete values that are known at compile
time use an 'enumerated type,
Break large programs into separate components (DLLs or class libraries) so that you can
reduce the visibility of data and routines, to just those other pieces of code that need to
use them,
Use a simple prex notation to show the type of variables and the scope of routines.
Declaring something at the top of your form code will make it private to that form, therefore, if you have X=10 in one form, and X=20 in another, they will not interfere. If the
variable was declared public, then there would be interactions between the values. To
declare something in your form, it is traditional to use "Private".
Private X as Integer
And nally, there are the subprograms. Dimensioning variables only to a subprogram is
highly eective, this way you can use default variables (such as sum for sums) in all subs
without the need to worry about one value changing because of another section of code.
There is, however, a twist. Dim, what you are accustomed to, will not retain the value of
the variable after the sub is done. So after rerunning the sub, all the local variables in the
sub will be reset. To get around this, a "Static" may be used.
Static X as Integer
122
Comments
Some of you may want to take the easy way out, and just use Public on everything. However, it is best to declare something on the smallest level possible. Generally arguments are
the best way to send variables from one sub to the other. This is because arguments make
it far easier to track exactly where variables are being changed in case of a logic error, and
almost limits the amount of damage a bad section of code can do. Declaring variables is,
again, useful when using default variables. Common default variables are:
I for loops
J for loops
Sum(self explanatory)
X for anything
So, rather then making variables I and II or Sum1,Sum2, you can see why keeping variables
local is a useful skill.
25.3 Comments
Every programmer I know dislikes writing comments. I don't mean just illiterate scriptkiddies writing some brain dead Visual Basic Script to delete all the les from some poor
grandmother's PC, I mean them and everyone up to people with multiple Ph.D.s and
honorary doctorates.
So if you nd it dicult to convince yourself that comments are a good idea you are in good
company.
Unfortunately this isn't a case of you and I being as good as them but of them being as
bad as us. Good comments can be critical to the longevity of a program; if a maintenance
programmer can't understand how your code was supposed to work he might have to rewrite
it. If he does that he will also have to write more comments and more tests. He will almost
certainly introduce more bugs and it will be your fault because you didn't have the courtesy
to explain why your program was written the way it was. Maintenance programmers are
often not part of the original team so they don't have any shared background to help them
understand, they just have a bug report, the code and a deadline. If you don't write the
comments you can be sure that no one will add them later.
Comments need to be written with the same care and attention as the code itself. Sloppily
written comments that simply repeat what the code says are a waste of time, better to say
nothing at all. The same goes for comments that contradict the code; what is the reader
meant to believe, the code or the comment. If the comment contradicts the code someone
might 'x' the code only to nd that it was actually the comment that was broken.
Here are some example comments:
Dim cbMenuCommandBar As Office.CommandBarButton 'command bar object
That comes from one of my own programs that I created by hacking at a template provided
by someone else. Why he added the comment is beyond me, it adds nothing to the code
which contains the word CommandBar twice anyway!
Here is another from a similar program:
123
Eective Programming
Same degree of pointlessness. Both examples are from programs that I use everyday.
Another from the same program which shows both a good comment and a pointless one:
Public Sub Remove(vntIndexKey As Variant)
'used when removing an element from the collection
'vntIndexKey contains either the Index or Key, which is why
'it is declared as a Variant
'Syntax: x.Remove(xyz)
mCol.Remove vntIndexKey
End Sub
The rst comment line simply repeats the information contained in the name, the last
comment line tells us only what we can easily glean from the declaration. The middle
two lines say something useful, they explain the otherwise puzzling use of the Variant data
type. My recommendation is to delete the rst and last comment lines, not because they
are incorrect but because they are pointless and make it harder to see the comment that
actually matters.
To summarize: good comments explain why not what. They tell the reader what the code
cannot.
You can see what is happening by reading the code but it is very often hard or impossible
to see why the code is written as it is.
Comments that paraphrase the code do have a place. If the algorithm is complex or sophisticated you might need to precis it in plain human language. For example the routines that
implement an equation solver need to be accompanied by a description of the mathematical
method employed, perhaps with references to textbooks. Each individual line of code might
be perfectly clear but the overall plan might still be obscure; a precis in simple human
language can make it plain.
If you make use of a feature of VB that you know is little used you might need to point out
why it works to prevent well-meaning maintenance programmers cleaning it up.
If your code is solving a complex problem or is heavily optimized for speed it will need more
and better comments than otherwise but even simple code needs comments that explain
why it exists and outlines what it does. Very often it is better to put a narrative at the
head of a le instead of comments on individual code lines. The reader can then read the
summary instead of the code.
25.3.1 Summary
Comments should add clarity and meaning,
Keep the comments short unless the complexity of the code warrants a narrative description,
Add comments to the head of each le to explain why it exists and how to use it,
Comment each function, subroutine and property to explain any oddities such as the use
of the Object or Variant data types.
124
25.3.2 Exercises
Take a piece of code written by someone else and try to understand how it works without
reading the comments.
Try to nd some code that doesn't need any comments. Explain why it doesn't need
them. Does such a thing exist?
Search the web, your own code or a colleague's code to nd examples of good comments.
Put yourself in the position of a maintenance programmer called in to x a bug in a
dicult piece of your own code. Add comments or rewrite the existing comments to
make the job easier.
The reason it is written like this is probably that the programmer of the class in which
it sits is afraid that the author of the code that uses it will forget to initialize the object
properly. So he or she provides a default value.
The problem is that the programmer of the MaxSlots property has no way of knowing how
many slots that the client code will need. If the client code doesn't set MaxSlots it will
probably either fail or, worse, misbehave. It is much better to write the code like this:
Public Property Let MaxSlots(RHS as Long)
mlMaxSlots = RHS
End Property
Public Property Get MaxSlots() as Long
if mlMaxSlots = 0 then
Err.Raise ErrorUninitialized, "MaxSlots.Get", "Property not initialized"
End If
MaxSlots = mlMaxSlots
End Property
125
Eective Programming
Now when client code calls MaxSlots Get before calling MaxSlots Let an error will be raised.
It now becomes the responsibility of the client code to x the problem or pass on the error.
In any case the failure will be noticed sooner than if we provide a default value.
Another way of viewing the distinction between Defensive Programming and Fail Fast is to
see Fail Fast as strict implementation of a contract and Defensive Programming as forgiving.
It can be, and is often, debated whether being forgiving is good always a good idea in human
society but within a computer program it simply means that you don't trust all parts of
the program to abide by the specication of the program. In this case you need to x the
specication not forgive transgressions of it.
25.4.1 Exercises
Take a working non-trivial program and search the code for defensive programming,
Rewrite the code to make it fail fast instead,
Run the program again and see if it fails,
Fix the client part of the program to eliminate the failure.
25.4.2 References
For a succinct article on this subject see http://www.martinfowler.com/ieeeSoftware/
failFast.pdf.
If the statement is true then the program continues as if nothing happened but if it is not
then the program will stop at that line. Unfortunately VB6 has a particularly weak form of
assertions, they are only executed when the code is running in the debugger. This means
that they have no eect at all in the compiled program. Don't let this stop you from using
them, after all this is a feature that doesn't exist at all in many common programming
languages.
If you really need the assertions to be tested in the compiled program you can do something
like this:
If Not(0 < x) Then
Debug.Assert False
Err.Raise ErrorCodes.AssertionFailed
End If
Now the program will stop at the failure when in the IDE and raise an error when running
compiled. If you plan to use this technique in more than a few places it would make sense
to declare a subroutine to do it so as to reduce clutter:
126
Assertions can be used to implement a form of design by contract1 . Add assertions at the
beginning of each routine that assert something about the values of the arguments to the
routine and about the values of any relevant module or global variables. For instance a
routine that takes a single integer argument that must be greater than zero would have an
assertion of the same form as the one shown above. If it is called with a zero argument the
program will halt on the line with the assertion. You can also add assertions at the exit of
the routine that specify the allowed values of the return value or any side eects.
Assertions dier from explicit validation in that they do not raise errors or allow for the
program to take action if the assertion fails. This is not necessarily a weakness of the
assertion concept, it is central to the dierent ways that assertions and validation checks
are used.
Assertions are used to do several things:
They specify the contract that must be followed by the calling and called code,
They assist in debugging by halting execution at the earliest point at which it is known
that something is wrong. Correctly written assertions catch errors long before they cause
the program to blow up.
Assertions generally assist in nding logic errors during the development of a program,
validation, on the other hand, is usually intended to trap poor input either from a human
being or other external unreliable source. Programs are generally written so that input that
fails validation does not cause the program to fail, instead the validation error is reported to
a higher authority and corrective action taken. If an assertion fails it normally means that
two internal parts of the program failed to agree on the terms of the contract that both were
expected to comply with. If the calling routine sends a negative number where a positive
one is expected and that number is not provided by the user no amount of validation will
allow the program to recover so raising an error is pointless. In languages such as C failed
assertions cause the program to halt and emit a stack trace but VB simply stops when
running in the IDE. In VB assertions have no eect in compiled code.
Combined with comprehensive tests, assertions are a great aid to writing correct programs.
Assertions can also take the place of certain types of comments. Comments that describe the
allowed range of values that an argument is allowed to have are better written as assertions
because they are then explicit statements in the program that are actually checked. In VB
you must exercise the program in the IDE to get the benet of the assertions, this is a minor
inconvenience.
http://en.wikipedia.org/wiki/design%20by%20contract
127
Eective Programming
Assertions also help ensure that a program remains correct as new code is added and bugs
are xed. Imagine a subroutine that calculates the equivalent conductance of a radiator
due to convection (don't worry if the physics is unfamilar):
Public Function ConvectionConductance(Byval T1 as Double, Byval T2 as Double)
as Double
ConvectionConductance = 100 * Area * (T2 - T1)^0.25
End Sub
Now it so happens, if you know the physics, that conductance is always a non-negative
number regardless of the relationship between the temperatures T1 and T2. This function,
however, makes the assumption that T1 is always greater than, or equal to, T2. This
assumption might be perfectly reasonable for program in question but it is a restriction
nonetheless so it should be made part of the contract between this routine and its callers:
Public Function ConvectionConductance(Byval T1 as Double, Byval T2 as Double)
as Double
Debug.Assert T2 < T1
ConvectionConductance = 100 * Area * (T2 - T1)^0.25
End Sub
In this particular case it looks as if the assertion on the result is worthless because if the
precondition2 is satised it is obvious by inspection that the postcondition3 must also be
satised. In real life the code between the precondition4 and postcondition5 assertions is
usually much more complicated and might include a number of calls to functions that are not
under the control of the person who created the function. In such cases the postcondition6
should be specied even if it appears to be a waste of time because it both guards against
the introduction of bugs and informs other programmers of the contract that the function
is supposed to comply with.
25.6 Tests
Tests vary from writing a program and then running it and looking casually at its behaviour
to writing a full suite of automated tests rst and then writing the program to comply.
Most of us work somewhere in between, usually nearer the rst alternative than the second.
Tests are frequently regarded as an extra cost but like quality control systems for physical
2
3
4
5
6
128
http://en.wikipedia.org/wiki/Precondition
http://en.wikipedia.org/wiki/Postcondition
http://en.wikipedia.org/wiki/Precondition
http://en.wikipedia.org/wiki/Postcondition
http://en.wikipedia.org/wiki/Postcondition
129
Eective Programming
A recommendation for Hungarian Notation is described in greater detail in the Coding
Standards7 chapter.
Class1 is a simple class with no methods and a single Public attribute. Not good programming practice for real programs but sucient for the illustration. The xProblem subroutine
simply creates two instances of Class1 (objects) and links them together. Notice that the
oObject1 and oObject2 variables are local to xProblem. This means that when the subroutine completes the to variables will be discarded. When Visual Basic does this it decrements
a counter in each object and if this counter goes to zero it executes the Class_Terminate
method (if there is one) and then recovers the memory occupied by the object. Unfortunately, in this case the reference counter can never go to zero because each object refers to
the other so even though no variable in the program refers to either of the objects they will
130
If the xProblem routine were working without a leak you would see an equal number of
Initialize and Terminate messages.
25.9.1 Exercises
Modify xProblem to ensure that both objects are disposed of when it exits (hint: setting
a variable to Nothing reduces the reference count of the object it points to).
---------| child |
|
|
----------
131
Eective Programming
In this particular case you can usually avoid the child to parent reference completely by
introducing a helper object. If you partition the parent's attributes into two sets: one
which contains attributes that only the parent accesses and another that is used by both
parent and children you can avoid the circularity by placing all those shared attributes in
the helper object. Now both parent and have reference to the helper object and no child
needs a reference to the parent.
------------------| parent | ----> | child |
|
|
|
|
------------------|
|
|
|
|
---------|
-> | common | <----------
Notice how all the arrows point away from the parent. This means that when our code
releases the last reference to the parent that the reference count will go to zero and that
the parent will be disposed of. This,in turn, releases the reference to the child. Now with
both parent and child gone there are no references left to the common object so it will be
disposed of as well. All the reference counting and disposal takes place automatically as
part of Visual Basic's internal behaviour, no code needs to be written to make it happen,
you just have to set up the structures correctly.
Note that the parent can have as many children as you like, held in a collection or array of
object references for instance.
A common use for this sort of structure occurs when the child needs to combine some
information about the parent with some of its own. For instance, if you are modelling some
complicated machine and want each part to have a property showing its position. You would
like to avoid making this a simple read write property because then you have to explicitly
update that property on each object when the machine as a whole moves. Much better to
make it a calculated property based on the parent position and some dimensional properties
then when the parent is moved all the calculated properties will be correct without running
any extra code. Another application is a property that returns a fully qualied path from
root object to the child.
Here is a code example:
'cParent
Private moChildren as Collection
Private moCommon as cCommon
Private Sub Class_Initialize()
Set moChildren = New Collection
Set moCommon = New cCommon
End Sub
Public Function NewChild as cChild
Set NewChild = New cChild
Set NewChild.oCommon = moCommon
moChildren.Add newChild
End Function
'cCommon
132
As it stands that really only works for one level of parent child relations, but often we have
an indenite number of levels, for instance in a disk directory structure.
We can generalise this by recognizing that parents and children can actually be the same
class and that the child doesn't care how the parent path is determined so long as it comes
from the common object.
'cFolder
Private moChildren as Collection
Private moCommon as cCommon
Private Sub Class_Initialize()
Set moChildren = New Collection
Set moCommon = New cCommon
End Sub
Public Function NewFolder as cFolder
Set NewFolder = New cFolder
Set NewFolder.oCommon = moCommon
moChildren.Add newFolder
End Function
Public Property Set oCommon(RHS as cCommon)
Set moCommon.oCommon = RHS
End Property
Public Property Get Path() as String
Path = moCommon.Path
End Property
Public Property Get Name() as String
Name= moCommon.Name
End Property
Public Property Let Name(RHS As String)
moCommon.Name = RHS
End Property
'cCommon
Private moCommon As cCommon
Public Name As String
Public Property Get Path() as String
Path = "/" & Name
if not moCommon is Nothing then
' has parent
Path = moCommon.Path & Path
End If
End Property
133
Eective Programming
Now we can ask any object at any level of the structure for its full path and it will return
it without needing a reference to its parent.
25.9.3 Exercises
Create a simple program using the cfolder and cCommon classes and show that it works;
that is, that it neither leaks memory nor gives the wrong answers for the Path property.
EH is a label at the end of the procedure. Following the label you place code that deals
with the error. Here is a typical error handler:
Exit Sub
EH:
If Err.Number = 5 Then
FixTheProblem
Resume
End If
Err.Raise Err.Number
End Sub
134
http://www.kellyethridge.com/vbcorlib/index.shtml
When you want a new error code you just add it to the list. The codes will be assigned in
increasing order. In fact you really never need to care what the actual number is.
You can declare the built in error codes in the just the same way except that you must
explicitly set the values:
Public Enum vbErrorCodes
InvalidProcedureCall = 5
End Enum
135
Eective Programming
arcana but has a considerable interest in clear and full description of the problem in external
terms. The error report should point the user directly at the problem in the data and oer
suggestions for xing it. Don't just say "Invalid Data", say which piece of data is invalid,
why it is invalid and what range of values can be accepted.
Contract failures usually indicate logic errors in the code. On the assumption that all
the invalid data has been weeded out by the front end the program must work unless the
program itself is faulty. See the #Assertions and Design By Contract9 for more information
on this topic.
Unexpected errors are the errors that most programmers concentrate on. In fact they are
not terribly common, they only appear so because the contract between the various parts
of the program is rarely spelled out. Unexpected errors dier from Expected errors in that
the user cannot usually be expected to assist in immediate recovery from unexpected errors.
The program then needs to present the user with all the gory details of the internal state
of the program at the time that the error was discovered in a form that can easily be
transmitted back to the maintainers of the program. A log le is ideal for this purpose.
Don't just present the user with a standard message box because there is no practical way
to capture the description so that it can be emailed to the maintainers.
136
Visual basic will halt on the assertion of n = 0 so if you run your tests in the IDE you will
be taken directly to the spot where the error is discovered.
Unexpected Errors
These are errors that are not caught by either validation of input data nor assertion of the
truth of preconditions or postconditions. Like contract errors they indicate faulty logic in
the program; of course the logic error can be in the validation of the input data or even in
one of the preconditions or postconditions.
These errors need the most thorough report because they are obviously unusual and dicult
or they would have been forseen in the validation and contract checks. Like contract failures
the report should be logged to a text le so that it can easily be sent to the maintainers.
Unlike the contract failures it won't be easy to tell what information is relevant so err on the
safe side by including descriptions of all the arguments to the subroutines in the Err.Raise
statements.
137
26 Idioms
An idiom is a sort of template or generic method of expressing an idea. In the same way
that idioms in a human language make life easier for the both speaker and listener good
idioms in computer programming make life easier for the programmer. Naturally what is
idiomatic in one language might not be in another.
It is not necessary that there exist a physical template, an idiom can exist in your head
and be so familiar that when faced with a problem that can be solved by application of an
idiom you begin to type the code almost without noticing what you are doing.
Another word often used for ideas like this is pattern. However the word pattern has become
so formalised and so strongly associated with object orientation that I feel the more generic
word idiom is better also it is the word generally associated with the rst major example
that I will show.
An idiom can be language specic, library specic, domain specic, or any combination of
the three. For instance if you are writing a program that deals with lists of things you
might borrow some of the list processing idioms of Lisp even though Visual Basic Classic
has no native list processing functions at all. By creating functions that manipulate lists
rather than inlining the code to do it you will make your code more meaningful to someone
reading it.
It is a commonplace that programmers feel ever so slightly godlike because they are in
total control of their programs. I think that programmers would be better o emulating
Shakespeare instead: when the English language of his day wasn't up to saying what he
wanted he had no hesitation in extending both its stylistic range and its vocabulary.
139
Idioms
140
with NewMouseBusy(Client)
...
End With
modMouseBusy.bas
This module provide the constructor for cMouseBusy. This gives us the ability to pass
arguments to the object when it is created.
Attribute VB_Name = "modMouseBusy"
Option Explicit
Private moReferenceCount As Dictionary
Public Function NewMouseBusy(ByRef roClient As Object) As cMouseBusy
If moReferenceCount is Nothing then
Set moReferenceCount = New Dictionary
141
Idioms
End If
Set NewMouseBusy = New cMouseBusy
NewMouseBusy.Initialize roClient
Remember that Visual Basic is single threaded and that the User Interface will not update
until all code stops executing unless you call DoEvents so do this to make sure that the
change to the mouse icon is visible.
DoEvents
End Function
This function increments the count for the given object and returns the new count.
Public Function IncrefCount(ByRef roClient As Object) As Long
Dim lRefCount As Long
IncrefCount = 1 + moReferenceCount(roClient)
moReferenceCount(roClient) = IncrefCount
End Function
This function decrements the count and returns the new count. Note that if the count goes
to zero then the entry is removed from the list; this must be done because the keys are
objects and they will not terminate if we do not release the reference.
Public Function DecRefCount(ByRef roClient As Object) As Long
DecRefCount = moReferenceCount(roClient) - 1
If DecRefCount = 0 Then
moReferenceCount.Remove roClient
Else
moReferenceCount(roClient) = DecRefCount
End If
End Function
frmTestMouseBusy
This form lets you test the mouse busy class. Just click the buttons and watch the mouse
cursor.
VERSION 5.00
Begin VB.Form frmTestMouseBusy
Caption
=
"frmTestMouseBusy"
ClientHeight
=
2175
ClientLeft
=
60
ClientTop
=
360
ClientWidth
=
4140
LinkTopic
=
"Form1"
ScaleHeight
=
2175
ScaleWidth
=
4140
StartUpPosition =
3 'Windows Default
Begin VB.CommandButton Command2
Caption
=
"Command2"
Height
=
735
Left
=
2160
TabIndex
=
1
Top
=
480
Width
=
1215
End
Begin VB.CommandButton Command1
Caption
=
"Command1"
Height
=
735
Left
=
480
TabIndex
=
0
142
prjTestMouseBusy.vbp
The project le for the test. Note that you need a reference to the Microsoft Scripting
Runtime library to provide the Dictionary class.
Type=Exe
Reference=*\G{00020430-000
0-0000-C000-000000000046}#2.0#0#..\..\..\..\..\..\WINNT\System32\stdole2.tlb#OLE
Automation
Reference=*\G{420B2830-E718-11C
F-893D-00A0C9054228}#1.0#0#..\..\..\..\..\..\WINNT\System32\scrrun.dll#Microsoft
Scripting Runtime
Class=cMouseBusy; cMouseBusy.cls
Module=modMouseBusy; modMouseBusy.bas
Form=frmtestMouseBusy.frm
Startup="frmTestMouseBusy"
HelpFile=""
Command32=""
Name="prjTestMouseBusy"
143
Idioms
HelpContextID="0"
CompatibleMode="0"
MajorVer=1
MinorVer=0
RevisionVer=0
AutoIncrementVer=0
ServerSupportFiles=0
VersionCompanyName="ABB"
CompilationType=0
OptimizationType=0
FavorPentiumPro(tm)=0
CodeViewDebugInfo=0
NoAliasing=0
BoundsCheck=0
OverflowCheck=0
FlPointCheck=0
FDIVCheck=0
UnroundedFP=0
StartMode=0
Unattended=0
Retained=0
ThreadPerObject=0
MaxNumberOfThreads=1
26.2 References
The RAII (Resource Acquisition Is Initialisation) Programming Idiom1 .
144
http://www.hackcraft.net/raii/
145
Dim I as Long
not:
Dim I as Integer
However, it is important to realize that the nal arbiter of speed is measurement of performance under real life conditions; see the tests section below.
The only reason to use Integer is when passing arguments by reference to functions dened
in components that you cannot alter. For instance most of the events raised by the VB6
GUI use Integer arguments instead of Long. This is also the case with some third party
DLLs. Most Windows API functions require Long arguments.
27.1.1 Tests
Here is a simple test that illustrates that things are not always as they seem in the world of
optimization. The test is a simple module that executes two subroutines. The two routines
are identical except that in one all the whole number variables are declared as Long and
in the other they are all declared as Integer. The routines simply increment a number
repeatedly. When executed inside the VB6 IDE on my computer the results are (three
consecutive runs in seconds):
Long
Integer
Long
Integer
Long
Integer
11.607
7.220
11.126
7.211
11.006
7.221
which appears to contradict my assertion that Longs are faster than Integers. However
when compiled and executed outside the IDE the results are:
Long
Integer
Long
Integer
Long
Integer
0.711
0.721
0.721
0.711
0.721
0.721
Notice that when running compiled the times for Long and Integer are indistinguishable.
This illustrates several important points about optimization and benchmarking.
Timing jitter
timing an algorithm on a pre-emptive multitasking operating system is always an exercise
in statistics because you cannot (not in VB6 at least) force the OS to not interrupt your
code which means that timing using the simple technique of these tests includes time spent
in other programs or at least in the System Idle loop.
146
http://xbeat.net/vbspeed/index.htm
147
27.2 Strings
If you have a lot of code that does a lot of string concatenation you should consider using
a string builder. A string builder is usually provided as a class but the principle is quite
simple and doesn't need any object oriented wrapping.
The problem that a string builder solves is the time used in repeatedly allocating and
deallocating memory. The problem arises because VB strings are implemented as pointers
to memory locations and every time you concatenate two strings what actually happens is
that you allocate a new block of memory for the resulting string. This happens even if the
new string replaces the old as in the following statement:
s = s & "Test"
The act of allocating and deallocating memory is quite expensive in terms of CPU cycles.
A string builder works by maintaining a buer that is longer than the actual string so that
the text that is to be added to the end can simply be copied to the memory location. Of
course the buer must be long enough to accommodate the resulting string so we calculate
the length of the result rst and check to see if the buer is long enough; if it is not we
allocate a new longer string.
The code for a string builder can be very simple:
Private Sub xAddToStringBuf(ByRef rsBuf As String, _
ByRef rlLength As Long, _
ByRef rsAdditional As String)
If (rlLength + Len(rsAdditional)) > Len(rsBuf) Then
rsBuf = rsBuf & Space$(rlLength + Len(rsAdditional))
End If
Mid$(rsBuf, rlLength + 1, Len(rsAdditional)) = rsAdditional
rlLength = rlLength + Len(rsAdditional)
End Sub
148
Strings
This subroutine takes the place of the string concatenation operator (&). Notice that there
is an extra argument, rlLength. This is necessary because the length of the buer is not the
same as the length of the string.
We call it like this:
lLen is the length of the string. If you look at the table of execution times below you can
see that for values of Count up to about 100 the simple method is faster but hat above
that the time for simple concatenation increases roughly exponentially whereas that for the
string builder increases roughly linearly (tested in the IDE, 1.8GHz CPU, 1GB ram). The
actual times will be dierent on your machine but the general trend should be the same.
It is important to look critically at measurements like this and try to see how, or if, they
apply to the application that you are writing. If you build long text strings in memory the
string builder is a useful tool but if you only concatenate a few strings the native operator
will be faster and simpler.
27.2.1 Tests
Concatenating the word Test repeatedly gives these results
149
Figure 8
150
Simple
0.000005
0.000037
0.001840
0.045
0.179
0.708
1.583
2.862
4.395
6.321
13.641
27.687
Builder
0.000009
0.000043
0.000351
0.002
0.004
0.008
0.011
0.016
0.019
0.023
0.033
0.035
13.4190000000017
13.137999999999
15.7870000000039
15.3669999999984
You can also see for that the dierence is slight. Be careful when interpreting coding
benchmarks, even the simplest can be misleading and it is always wise to prole the real
code if you think you have a bottle-neck.
The other claim is that ByVal is safer. This is usually explained by saying that a function
in which all arguments are declared ByVal cannot alter the values of the variables in the
caller because VB makes copies instead of using pointers. The programmer is then free to
make use of the incoming arguments in any way he or she sees t, in particular they can be
assigned new values without disturbing the original values in the caller.
The safety provided by this is generally outweighed by the fact that the compiler is
then unable to enforce type checking. In Visual Basic Classic variables are automatically
converted between string and numeric types when necessary and possible. If arguments
are declared ByVal then the caller can provide a String where a Long was expected. The
compiler will silently compile the necessary instructions to convert the String to a Long
and carry on. At run time you might, or might not, discover the problem. If the string
contained a number then it will be converted and the code will 'work', if it doesn't then
the code will fail at the point where the function is called.
Function IntByVal(ByVal z as Double) as Long
IntByVal = Int(z)
End Function
Function IntByRef(ByRef z as Double) as Long
IntByRef = Int(z)
End Function
Dim s As String
's = "0-471-30460-3"
s = "0"
Debug.Print IntByVal(s)
151
Debug.Print IntByRef(s)
If you try to compile the little snippet of code above you will see that the compiler stops
on the line:
Debug.Print IntByRef(s)
it hightlights the s and shows a message box saying 'ByRef argument type mismatch'. If
you now comment out that line and run again you will not get an error. Now uncomment
this line:
s = "0-471-30460-3"
Run the program again. Now the program fails but only at run time with 'Runtime error
13: Type mismatch'.
The moral is:
use ByRef to cause the compiler to typecheck the arguments in function calls,
never assign to the arguments of a function unless they are output parameters.
Look at the Procedure Parameters3 section in the Coding Standards4 chapter for some
suggestions about how to name the arguments so that it is always clear which are in and
which are out parameters.
27.3.1 Tests
With Empty Functions
Option Explicit
Private Const mlLOOPS As Long = 100000000
Private mnStart As Double
Private mnFinish As Double
Public Sub main()
xTestByRef 1#, 2#
Debug.Print "xTestByRef", mnFinish - mnStart
xTestByVal 1#, 2#
Debug.Print "xTestByVal", mnFinish - mnStart
End Sub
3
4
152
153
End Function
Private Function xAddByVal(ByVal a As Double, ByVal b As Double) As Double
xAddByVal = a + b
End Function
27.4 Collections
Collections are very useful objects. They allow you to write simpler code than would
otherwise be the case. For instance if you need to hold on to a list of numbers given to you
by the user you probably won't know in advance how many will be supplied. This makes
it dicult to use an array because you must either allocate an unnecessarily large array
so that you are sure that no reasonable user would supply more or you must continually
Redim5 the array as new numbers come in.
A Collection lets you avoid all that because it expands as necessary. However, this convenience comes at the cost of increased runtime under some circumstances. For many, perhaps
most, programs this price is perfectly acceptable but some programs take so long to run
that you must try to squeeze the last drop of performance out of every line.
This frequently occurs in programs doing scientic computations (don't tell me C would be
a better language for such things because the same constraints and optimizations apply to
C and all other languages as well).
One of the reasons a Collection is a convenient tool is because you can use a string as a
key and retrieve items by key instead of by index. But every time you ask for an item in
a collection the collection must rst gure out whether you have supplied an Integer (or
Byte or Long) or a String which of course takes a small but nite amount of time. If your
application makes no use of the ability to look up items by String key you will get faster
programs by using arrays instead because VB doesn't need to waste time checking to see if
you supplied a String key instead of a whole number index.
If you want a Collection to simply hold a list of items without keys then you can emulate
this behaviour with an array as follows (note that this code is not optimal, optimizing it is
left as an exercise for the student):
Public Sub Initialize(ByRef a() As Variant, _
ByRef Count As Long, _
ByRef Allocated As Long)
Allocated = 10
Redim a(1 to Allocated)
Count = 0
End Sub
Public Sub Add(ByRef NewItem as Variant, _
ByRef a() As Variant, _
ByRef Count As Long, _
ByRef Allocated As Long)
Count = Count + 1
If Allocated < Count then
154
http://en.wikibooks.org/wiki/Programming%3AVisual%20Basic%20Class%2FKeywords%23Redim
Collections
Allocated = Count
Redim Preserve a(1 to Allocated)
End If
a(Count) = NewValue
End Sub
To use the above code you must declare an array of Variants and two Longs. Call the
Initialize sub to start it all o. Now you can call the Add sub as often as you like and the
array will be extended as necessary.
There are a number of things to note about this seemingly simply subroutine:
Once the array size exceeds 10 a new array is allocated each time an item is added
(replacing the original),
The size of the array (recorded in the Allocated variable) is always the same as the Count
when Count exceeds 10,
No provision is made to delete items,
the items are stored in the same order as they are added.
all arguments are declared Byref
It is unlikely that any programmer would want to include exactly this routine in production
code. Some reasons why are:
NewItem is declared as Variant but the programmer usually knows the type,
The initial allocation is hard coded using a literal integer,
The name of the subroutine is too simple, it will probably clash with others in the same
namespace.
No provision is made for removing items.
Three separate pieces of information must be kept together but they are not bound
together.
The performance of the emulated Collection depends on the relative frequency of calls to
Add, Item and Remove (see #Exercises6 ). An optimized version must be optimized for the
use to which it will be put. If no lookups by key are performed there is no need to provide a
function for it and there is especially no need to provide data structures to make it ecient.
27.4.1 Exercises
Extend the code presented in this section to emulate the Collection class in more detail.
Implement Item and Remove methods, see the VB help les for details of the exact
method declarations but do not feel obliged to replicate them exactly.
Write a test program to exercise all the features and time them so that you can tell when
to use a Collection and when to use your emulated version.
You should be able to think of at least two distinct implementations of Remove. Think
about the consequences of dierent implementations.
155
27.5 Dictionaries
In VB a dictionary is actually supplied by the Scripting Runtime component. It is rather
like a Collection but also provides a means to retrieve the keys that were used. Also, unlike
the Collection, the keys can be any datatype including objects. The principal dierences
between Collections and Dictionaries are as follows:
Dictionaries have Keys and Items properties which return variant arrays
Keys can be any datatype not just strings
Enumeration using For Each returns the Keys not the Items
There is a built in Exists method.
156
Dictionaries
End Property
Public Function Exists(Key As String) As Boolean
Exists = IndexOf(Key)
End Function
Public Function IndexOf(Key As String) As Long
For IndexOf = 1 To mlCount
If maItems(1, IndexOf) = Key Then
Exit Function
End If
Next IndexOf
IndexOf = 0
End Function
Public Property Let Item(Key As String, RHS As String)
Dim lX As Long
lX = IndexOf(Key)
If lX Then
maItems(lX, 2) = RHS
Else
Add Key, RHS
End If
End Property
Public Property Get Item(Key As String) As String
Item = maItems(IndexOf(Key), 2)
End Property
Public Sub Remove(Key As String)
Dim lX As Long
lX = IndexOf(Key)
maItems(1, lX) = maItems(1, mlCount)
maItems(2, lX) = maItems(2, mlCount)
mlCount = mlCount - 1
End Sub
Public Sub RemoveAll()
mlCount = 0
End Sub
Public Sub Class_Initialize()
mlAllocated = 10
ReDim maItems(1 To 2, 1 To mlAllocated)
End Sub
A simple test routine can be used to demonstrate that this class is faster than the VB dictionary for certain tasks. For instance adding 32 items to the dictionary and then removing
them again is faster using cDict but if you double the number of items the VB Dictionary
is better. The moral is: choose the correct algorithm for the task at hand.
Here is the test routine:
157
Option Explicit
Public gsModuleName As String
End Sub
158
Time: 1.602
Time: 0.120
Time: 1.663
Time: 0.200
Time: 1.792
Time: 0.361
Time: 2.023
Time: 0.741
Time: 2.504
Time: 1.632
Time: 3.455
Time: 4.046
Time: 5.387
Time: 11.437
27.5.1 Exercises
Write a version of the cDict class that allows storage of other data types, using Variant
arguments for instance,
Check the performance using a similar test routine.
Write a new test routine that performs other tests. Does the relative performance of the
new classes and VB Dictionary change?
What happens if you try to retrieve or remove an item that does not exist? How does
the behaviour compare with that of the Dictionary?
159
27.6.1 Exercises
Write a short program to demonstrate that Debug.Print evaluates functions even when
compiled,
Modify it to show that Debug.Assert does not suer from this problem,
Show that Debug.Print has zero execution time in the compiled version if the arguments
are constants or variables.
160
Object Instantiation
have. If there are very few then you can create separate code for each, this can then be
very highly tuned. On the other hand if you have dozens of dierent classes (perhaps you
have extended the simulation to include competing herbivores) you will quickly nd that
you have a maintenance problem.
The solution is to create a class that describes a general purpose object pool and an Interface7 that can be implemented by each of the classes.
Before I describe the class and the interface here is a summary of the requirements:
Only one pool class needed,
The classes being 'pooled' should need small modications to only the initialization and
terminaion code to t the pooling concept,
The code that uses the pooled objects should not have to change except for calling the
pool's GetObject function instead of using the New operator.
The technique hinges on the fact that VB6 has deterministic nalization. Another piece of
jargon that simply means that VB6 destroys (nalizes) objects as soon as they become unused. Every time VB6 decides that an object is no longer in use it calls the Class_Terminate
method of that object. What we can do is add a simple piece of code to each class that
puts the terminating object on a list of available objects. VB will see that the object is
now in use and not deallocate the memory that it used. Later on, instead of using the New
operator to create new object we ask the object pool to give us one that had fallen out of
use. Even if the object takes very little time to set up this will be quicker than using New
because it avoid both the allocation and deallocation of memory.
Here is an example object pool class and a class that can use it:
'ObjectPool.cls
Private moPool as Collection
Public oTemplate As Object
Private sub Class_Initialize()
set moPool = New Collection
End Sub
Public Function GetObject() as Object
if moPool.Count then
Set GetObject = moPool(1)
moPool.Remove(1)
Else
Set GetObject = oTemplate.NewObject
End If
End Function
Public Sub ReturnToPool(Byref oObject as Object)
moPool.Add oObject
End Sub
To use this class declare a public variable in a bas module and assign a new ObjectPool
object to it:
http://en.wikibooks.org/wiki/..%2FInterfaces
161
'modPool.bas
Public goPool As ObjectPool
Public Sub Initialize()
Set goPool = New ObjectPool
Set goPool.oTemplate = New Herbivore
End Sub
Now modify your Herbivore class by adding a call to ReturnToPool to the Class_Terminate
method and adding a NewObject method:
Private Sub Class_Terminate()
goPool.ReturnToPool Me
End Sub
Public Function NewObject() as Object
Set NewObject = New Herbivore
End Function
For some simple scenarios this might even work. However, there are several aws, and at
least one is a major problem. The problem is that the object you get doesn't necessarily
look like a shiny new one. Now for some applications this doesn't matter because the
client code will reinitialise everything anyway but many programs rely on the feature of
the language that automatically sets newly allocated variables to zero. In order to satisfy
the requirement for minimum changes in the client we should extend both ObjectPool and
the objects that are pooled by adding a ReInitialize method to the Herbivore:
Public Sub ReInitialize()
' Do whatever you need to do to make the object
' look like new (reset some attributes to zero,
' empty strings, etc).
End Sub
Don't do any more work in ReInitialize than absolutely necessary. For instance if one of
the attributes of the object is a dynamically allocated array it might not be necessary to
clear it; it might be enough to set a ag or counter to indicate that no array elements are
actually in use.
Now modify the GetObject method of ObjectPool:
Public Function GetObject() as Object
if moPool.Count then
Set GetObject = moPool(1)
GetObject.ReInitialize
moPool.Remove(1)
Else
Set GetObject = oTemplate.NewObject
End If
End Function
Now everywhere that you use New Herbivore use goPool.GetObject instead. If the Herbivore object has references to other objects you might, or might not, want to release those
references by setting them to Nothing in the Class_Terminate method. It depends on the
162
Object Instantiation
behaviour of the objects and the rest of your program, generally you should put o doing
expensive operations for as long as possible.
The use of an object pool can improve the performance of certain types of program without
requiring the programmer to radically change the program design. However don't forget
that you might be able to get similar or greater improvements by using a better algorithm;
again timing tests are the key to knowledge in this area. Don't assume that you know where
the bottleneck is, demonstrate where it is by proling the program.
27.7.1 Exercises
Write a simple program that creates and destroys lots of objects and time it. Now modify
it to use ObjectPool.
Dene an interface for pooled objects and implement it. Does eliminating the use of As
Object change the performance?
Notice that the moPool collection is used simply as a FIFO stack, that is no use is made
of the ability to nd items by key. Is there an alternative that would be faster?
Is the FIFO behaviour of the stack important, that is, is it a deliberate feature or simply
irrelevant?
---Tip #1 Move as much code as possible outside of loops. Loops are always the most
important thing to optimize in your code. Always try to move as much as you can out
of a loop. That way code doesn't get repeated and saves you some cpu cycles. A simple
example:
Code: for i = 1 to 50 x = b ' Stays the same with every loop, get it outside of the loop! k
= j + i next iChange that to:
Code: x = b 'Moved outside the loop
for i = 1 to 50 k = j + 1 next i That may seem like a no-brainier but you would be surprised
about how many programmers do that. The simple rule is, if it doesn't change with every
loop iteration then move it outside of the loop as it doesn't need to be there. You only want
to include code inside a loop that MUST be there. The more instructions you can clear out
of a loop the faster we can run it.
Tip #2 Loop Unrolling Loop unrolling can eliminate some compare and jump instructions.
(Compare and jump instructions are used to create loops, you don't see them in visual basic,
its behind the scenes stu that you learn in ASM.) It also takes advantage of the ability
of modern cpus that can fetch several instructions at a time. In a nutshell you get a good
speed boast by unrolling loops.
But there is something we need to know about loop unrolling. The largest bottleneck on
modern computers is memory. So the designers of CPU's like Intel and AMD addressed
this problem by using a cache on their cpus. This is basically a memory location that is
accessed much faster by the CPU then standard memory. You want your unrolled loop
to t in that cache, if it doesn't then it could slow down your code. So you may want to
experiment with gettickcount when you unroll you're loop.
163
Next i You can get up to a 25% gain in speed depending on what you are doing, you just
have to experiment with this.
Tip #3 Avoid dividing if possible. A divide instruction is one of the most if not the most
expensive instruction you can perform on a CPU. It is faster to multiply then divide!
Code: B = 40 / 2
is slower then
Code: b = 40 * 0.5 You can also develop some interesting algorithms using subtraction to
get your result that is much faster then using division. If your using division in a loop,
it is a must to change it to speed up your code. (I was going to also recommend trying
shifting the bits for division but I forgot some versions of visual basic doesn't include the
shift operator).
Tip #4 In a nested conditional branch such as select case and nested if statements, put
the things that are most likely to be true rst in the nest, with the least likely things last.
Tip #5 Avoid use of variant variables. The variant variable is all nice when you are new
to visual basic, but its a habit you need to break. A variant variable is converted into its
proper data type at runtime which can be very expensive.
Tip #6 Be careful when you declare variables. If you don't use as something with every
variable you declare, it is a variant! For example:
Code: Dim a, b, c as string. A = A variant B = A variant C = A string I've seen some
people use the notation:
Code: dim x x = blah That is a NO NO! It may work yes, but its going to cost you speed.
Tip #7 Reduce common expressions. Sometimes you have two dierent variables that
use part of the same calculation. Instead of doing the entire calculation for both variables,
eliminate the redundant calculation.
Example:
Code: x = a * b + c y = a * b + d
is slower then
164
Object Instantiation
Code: t = a * b x = t + c y = t + d That is especially true if your using a redundant
expensive calculation in a loop.
Tip # 7 Use long or integer for calculations. A long is a 32 bit number and is more
natural on 32 bit processors. Avoid other variables such as double, single, etc
Tip #8 Use inline functions inside of loops. Instead of calling a function, stick the code in
the loop. This will make you're program larger if you repeat it in enough loops and should
only be done in critical places. The reason is due to the over head of calling a function.
Before the program calls a function, it has to push some things onto the stack. At the very
least it will push the instruction pointer (IE: Return Address). Memory access is slow so
we want to avoid that in critical places.
Tip #9 Avoid using properties in loops. Properties are accessed a lot slower then variables,
so use variables instead:
Code: for i = 1 to 50 text1.text = text1.text + b(i) next i
is slower then
Code: for i = 1 to 50 strbuer = strbuer + b(i) next i text1.text = strbuer
Tip #10 Load all the data you need from the disk. Instead of loading one le at a time,
load all of them at once. This will avoid future delay for the user.
Tip #11 Make good use of the timer control. You can do background processing while
waiting on a user. Use this time to prefetch data, calculations that are need, etc.
Tip #12 Minimize dot notation in your objects! Each dot you use in a object makes
visual basic do a call.
Code: Myobject.one.two.three
is slower then
Code: Myobject.one
Tip #13 Allocate enough memory at once. When you create a dynamic array and you want
to add elements that haven't been allocated yet, make sure you allocate enough for all of
them instead of doing it one at a time. If you don't know how many you need, times what
you have allocated by 2. Allocating memory is a expensive process.
Tip #14 Avoid built in functions in loops. If you have a algorithm that is looped that
requires the len of your string. Make sure you cache the size of your string in a buer and
not call the function len() with each iteration of the loop:
Code: for i = 1 to 100 sz = len(string) 'Do processing next i
instead
Code: sz = len(string) for i = 1 to 100 'Do Processing with sz next i
Tip #15 Hide the control when your setting its properties. Every time you update the
properties of your control, you make it repaint. So if your developing something that
displays complex graphics, may be a good idea to reduce that from happening so much.
165
28 Examples
This section includes a wide range of ready made code that can be easily adapted for use in
your own programs. Some examples are just a few lines others are complete applications,
most fall somewhere between.
Although the code is described as ready made it is not intended that you just drop it into
your programs as component parts. The idea is that these examples should provide a quick
start that you can build on and re-work.
Snippets1
Short pieces of code that illustrate techniques and solve small problems.
167
29 Snippets
29.1 TopMost Function
The code below is useful if you want to keep your application window on top or swap your
application window between staying the topmost window and behaving like a standard
window. Paste the code below into a code module and call either of the two routines as
required.
To make your application stay on top use this call :
MakeTopMost Me.hwnd
To make your application window behave like a normal window use this call :
MakeNormal Me.hwnd
' Created by E.Spencer - This code is public domain.
'
Public Const HWND_TOPMOST = -1
Public Const HWND_NOTOPMOST = -2
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Public Const SWP_NOACTIVATE = &H10
Public Const SWP_SHOWWINDOW = &H40
Public Const TOPMOST_FLAGS = SWP_NOMOVE Or SWP_NOSIZE
Public Declare Function SetWindowPos Lib "user32" _
(ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
ByVal x As Long, y, ByVal cx As Long, _
ByVal cy As Long, ByVal wFlags As Long) As Long
Public Sub MakeTopMost(Handle As Long)
SetWindowPos Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS
End Sub
Public Sub MakeNormal(Handle As Long)
SetWindowPos Handle, HWND_NOTOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS
End Sub
169
Snippets
(ByVal hwnd As Long, ByVal bRevert As Long) As Long
Private Declare Function GetMenuItemCount Lib "user32" _
(ByVal hMenu As Long) As Long
Private Declare Function RemoveMenu Lib "user32" _
(ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long)
As Long
Private Declare Function DrawMenuBar Lib "user32" _
(ByVal hwnd As Long) As Long
Private Const MF_BYPOSITION = &H400&
Private Const MF_DISABLED = &H2&
Public Sub DisableX(Frm As Form)
Dim hMenu As Long
Dim nCount As Long
hMenu = GetSystemMenu(Frm.hwnd, 0)
nCount = GetMenuItemCount(hMenu)
Call RemoveMenu(hMenu, nCount - 1, MF_DISABLED Or MF_BYPOSITION)
DrawMenuBar Frm.hwnd
End Sub
Private Sub Command1_Click()
DisableX Me
End Sub
170
171
Snippets
172
Random Numbers
173
Snippets
Combo1.AddItem "HourGlass", 1
Combo1.AddItem "Globe", 2
Combo1.ListIndex = 0
' Grab the current base cursor
mhBaseCursor = GetClassLong((hwnd), GCL_HCURSOR)
End Sub
174
Application Launching
Private
Private
Private
Private
Private
Private
Private
Private
175
Snippets
176
177
Snippets
178
179
30 The Language
Many computer languages share a core set of features. Sometimes they are very similar in
dierent languages, sometimes radically dierent. Some features regarded by users of one
language as indispensable may be completely missing from other perfectly usable languages.
30.1 Statements
A statement in Visual Basic is like a command given to the computer. Functions and
subroutines are made out of statements.
A statement generally consists of the name of a subroutine followed by a list of arguments:
Debug.Print "Hello World"
The rst word (Debug) is the name of a built in object in Visual Basic. the second word
(Print) is the name of a method of that object. "Hello World" is the argument to the
method. The result is:
Hello World
x is a subroutine which takes two string arguments. The keyword Call is a leftover from
older versions of Basic. If you use this form you must enclose the entire argument list in
a parenthesis. Sometimes the programmer wants to call a function but is not interested in
the return value only in the side-eects. In such cases he or she will call the function as
though it were a subroutine, however only the rst form above can be used.
There are other more complex statements which can span several lines of code:
If..Else..End If
Do While..Loop
These compound statements include other statements and expressions.
181
The Language
30.2 Variables
Like most programming languages, Visual Basic is able to use and process named variables
and their contents. Variables are most simply described as names by which we refer to
some location in memory - a location that holds a value with which we are working.
It often helps to think of variables as a 'pigeonhole', or a placeholder for a value. You can
think of a variable as being equivalent to its value. So, if you have a variable i that is
initialized to 4, i+1 will equal 5.
In Visual Basic variables can be declared before using them or the programmer can let the
compiler allocate space for them. For any but the simplest programs it is better to declare
all variables. This has several benets:
you get the datatype you want instead of Variant,
the compiler can sometimes check that you have used the correct type.
All variables in Visual Basic are typed, even those declared as Variant. If you don't specify
the type then the compiler will use Variant. A Variant variable is one that can hold data
of any type.
This is not a good idea because it is too easy to forget that in Visual Basic the type name applies only to the immediately precedingvariable name. The example declares twoVariantsand
one Integer
You can demonstrate what actually happens by this little program:
Option Explicit
Public Sub main()
Dim anumber, anothernumber, yetanothernumber As Integer
Dim v As Variant
Debug.Print TypeName(anumber), VarType(anumber), _
TypeName(anothernumber), VarType(anothernumber), _
TypeName(yetanothernumber), VarType(yetanothernumber)
Debug.Print TypeName(anumber), VarType(v)
Debug.Print "Assign a number to anumber"
anumber = 1
Debug.Print TypeName(anumber), VarType(anumber)
182
Variables
Debug.Print "Assign a string to anumber"
anumber = "a string"
Debug.Print TypeName(anumber), VarType(anumber)
End Sub
Empty
Integer
Notice that both VarType and TypeName return information about the variable stored
inside the Variant not the Variant itself.
To declare three integers in succession then, use:
Dim anumber As Integer, anothernumber As Integer, yetanothernumber As Integer
In Visual Basic it is not possible to combine declaration and initialization with variables
(as VB does not support constructors - see Objects and Initialise though), the following
statement is illegal:
Dim some_number As Integer = 3
In Visual Basic variables may be declared anywhere inside a subroutine, function or property, where their scope is restricted to that routine. They may also be declared in the
declarations section of a module before any subroutines, functions or properties, where they
have module level scope.
By default, variables do not have to be declared before use. Any variables created this way,
will be of Variant type. This behaviour can be overrriden by using the Option Explicit
statement at the top of a module and will force the programmer to declare all variables
before use.
After declaring variables, you can assign a value to a variable later on using a statement
like this:
Let some_number = 3
183
The Language
anumber = anothernumber
In Visual Basic you cannot assign multiple variables the same value with one statement.
the following statement does not do what a C programmer would expect:
anumber = anothernumber = yetanothernumber = 3
Instead it treats all but the rst = signs as equality test operatorsand then assigns eitherTrueorFalseto anumber depending on the values of anothernumber and yetanotherniumber.
Naming Variables
Variable names in Visual Basic are made up of letters (upper and lower case) and digits.
The underscore character, "_", is also permitted. Names must not begin with a digit.
Names can be as long as you like.
Some examples of valid (but not very descriptive) Visual Basic variable names:
foo
Bar
BAZ
foo_bar
a_foo42_
QuUx
184
Conditions
The naming rules for variables also apply to other language constructs such as function
names and module names.
It is good practice to get into the habit of using descriptive variable names. If you look at a
section of code many months after you created it and see the variables var1, var2 etc., you
will more than likely have no idea what they control. Variable names such as textinput
or time_start are much more descriptive - you know exactly what they do. If you have a
variable name comprised of more than one word, it is convention to begin the next word
with a capital letter. Remember you can't use spaces in variable names, and sensible capital
use makes it easier to distinguish between the words. When typing the variable name later
in the code, you can type all in lowercase - once you leave the line in question, if the variable
name is valid VB will capitalize it as appropriate.
30.2.2 Literals
Anytime within a program in which you specify a value explicitly instead of referring to a
variable or some other form of data, that value is referred to as a literal. In the initialization
example above, 3 is a literal. Numbers can be written as whole numbers, decimal fractions,
'scientic' notation or hex. To identify a hex number, simply prex the number with &H.
30.3 Conditions
Conditional clauses are blocks of code that will only execute if a particular expression (the
condition) is true.
30.3.1 If Statements
If statements are the most exible conditional statements: see Branching1
30.4 Unconditionals
Unconditionals3 let you change the ow of your program without a condition. These are
the Exit, End and Goto statements.
1
2
3
185
The Language
30.5 Loops
Loops4 allow you to have a set of statements repeated over and over again.
To repeat a given piece of code a set number of times or for every element in a list: see
For..Next Loops5
4
5
186
31 Coding Standards
See ../Credits and Permissions/1 for details of copyright, licence and authors of this piece.
Work in progress. Document needs substantial reformatting to t the Wikibooks style. Also
the page is much too long for convenient editing in a browser text box so it must be broken
into sections. Use the original as a guide: http://www.gui.com.au/resources/coding_
standards_print.htm
31.1 Overview
This document is a working document - it is not designed to meet the requirement that we
have "a" coding standard but instead is an acknowledgement that we can make our lives
much easier in the long term if we all agree to a common set of conventions when writing
code.
Inevitably, there are many places in this document where we have simply had to make a
choice between two or more equally valid alternatives. We have tried to actually think
about the relative merits of each alternative but inevitably some personal preferences have
come into play.
Hopefully this document is actually readable. Some standards documents are so dry as
to be about as interesting as reading the white pages. However, do not assume that this
document is any less important or should be treated any less lightly than its drier cousins.
http://en.wikibooks.org/wiki/..%2FCredits%20and%20Permissions%2F
187
Coding Standards
It is not uncommon for programmers to work with customers that have their own coding
standards. Most coding standards derive at least some of their content from the Hungarian
notation concept and in particular from a Microsoft white paper that documented a set of
suggested naming standards. For this reason many coding standards are broadly compatible
with each other. This document goes a little further than most in some areas; however it
is likely that these extensions will not conict with most other coding standards. But let's
be absolutely clear on this one folks: if there is a conict, the customer's coding standards
are to apply. Always.
31.3.2 Name body casing does not apply to prexes and suxes
Prexes and suxes are used for
module name for name with module scope
variable type
whether a variable is a function parameter
Those prexes and suxes are not subject to the same casing as the name body. Here are
a few example with the prexes or suxes in bold face.
's shows that sSomeString is of type String
sSomeString
'_IN shows that iSomeString_IN is an input argument
iSomeInteger_IN
'MYPRJ_ shows that MYPRJ_bSomeBoolean has module scope MYPRJ
MYPRJ_bSomeBoolean
188
Naming Standards
31.3.4 Acronyms
Acronym words have special handling.
Examples
Free text
Am I Happy
A DC-DC Converter
Camel Casing
AmI_Happy
Upper casing
AM_I_HAPPY
Comment
One letter words
are all upper case,
and are followed
by an underscore
inCamel casing
TheGSMPhone
THE_GSm_PHONEAcronyms are kept
all upper case in
Camel casing, and
have last letterset
lower case with
upper-casing
A_DC_DCConverterA_Dc_Dc_CONVERTER
When there are
two consecutive
acronyms, and the
rst acronym has
only twoletters,
then in Camel
casing, the rst
acronym is followed by an underscore
189
Coding Standards
Examples
Free text
A GSM LTE
phone
Camel Casing
A_GSmLTEPhone
Upper casing
Comment
A_GSm_LTe_PHONE
When there are
two consecutive
acronyms, and the
rst acronym has
three or moreletters, then in
Camel casing, the
rst acronym has
last letter in lower
case
31.4 Variables
Variable names are used very frequently in code; most statements contain the name of
at least one variable. By using a consistent naming system for variables, we are able to
minimise the amount of time spent hunting down the exact spelling of a variable's name.
Furthermore, by encoding into the variable's name some information about the variable
itself, we can make it much easier to decipher the meaning of any statement in which that
variable is used and to catch a lot of errors that are otherwise very dicult to nd.
The attributes of a variable that are useful to encode into its name are its scope and its
data type.
31.5 Scope
In Visual Basic there are three scopes at which a variable may be dened. If dened within
a procedure, the variable is local to that procedure. If dened in the general declarations
area of a form or module then that variable can be referenced from all procedures in that
form or module and is said to have module scope. Finally, if it is dened with the Global
keyword, then it is (obviously) global to the application.
A somewhat special case exists for arguments to procedures. The names themselves are
local in scope (to the procedure itself). However, any change applied to that argument
may, in some cases, aect a variable at a totally dierent scope. Now that may be exactly
what you want to happen - it is by no means always an error - but it can also be the cause
of subtle and very frustrating errors.
190
Variable Names
even worse. Subtle bugs can creep into your code because of an unintended conversion done
behind the scenes for you by VB.
By encoding the type of the variable in its name, you can visually check that any assignment
to that variable is sensible. This will help you pick up the error very quickly.
Encoding the data type into the variable name has another benet that is less often cited
as an advantage of this technique: reuse of data names. If you need to store the start date
in both a string and a double, then you can use the same root name for both variables. The
start date is always the StartDate; it just takes a dierent tag to distinguish the dierent
formats it is stored in.
The scope is encoded as a single character. The possible values for this character are :
g
m
The absence of a scope modier indicates that the variable is local in scope.
I will use text sidelined like this to try and explain why I have made certain choices. I
hope it is helpful to you.
Some coding standards require the use of another character (often 'l') to indicate a local
variable. I really don't like this. I don't think that it adds to the maintainability of the
code at all and I do think it makes the code a lot harder to read. Besides, it's ugly.
The type of the variable is encoded as one or more characters. The more common types are
encoded as single characters while the less common types are encoded using three or four
characters.
I thought long and hard about this one, folks. It is certainly possible to come up with onecharacter codes for all of the built-in data types. However, it is really hard to remember
them all, even if you made them up yourself. On balance, I think that this a better
approach.
The possible values for the type tag are :
191
Coding Standards
i
l
s
n
c
v
b
integer
long
string
numeric
currency
variant
boolean
dbl
sng
t
byte
obj
ctl
double
single
oat
byte
object
control
We do NOT use VB's type sux characters. These are redundant given our prexes and
no-one remembers more than a few of them anyway. And I don't like them.
It is important to note that dening a prex for the underlying implementation type of the
variable, while of some use, is often not the best alternative. Far more useful is dening a
prex based on the underlying aspects of the data itself. As an example, consider a date.
You can store a date in either a double or a variant. You don't (usually) care which one it
is because only dates can be logically assigned to it.
Consider this code :
dblDueDate = Date() + 14
We know that dblDueDate is stored in a double precision variable, but we are relying on
the name of the variable itself to identify that it is a date. Now suddenly we need to cope
with null dates (because we are going to process external data, for example). We need to
use a variant to store these dates so we can check whether they are null or not. We need
to change our variable name to use the new prex and nd everywhere that it is used and
make sure it is changed :
vDueDate = Date() + 14
But really, DueDate is rst and foremost a date. It should therefore be identied as a date
using a date prex :
dteDue = Date() + 14
This code is immune from changes in the underlying implementation of a date. In some
cases, you might need to know whether it is a double or a variant, in which case the
appropriate tag can also be used after the date tag :
dtevDue = Date() + 14
dtedblDue = Date() + 14
The same arguments apply for a lot of other situations. A loop index can be any numeric
type. (In VB4, it can even be a string!) You will often see code like this :
192
Variable Names
Now, what if we need to process the loop 100,000 items? We need to change the type of
the variable to a long integer, then change all occurrences of its name too.
If we had instead written the routine like this:
Dim nCounter As Integer
For nCounter = 1 to 10000
DoSomething
Next nCounter
We could have updated the routine by changing the Dim statement only.
Windows handles are an even better example. A handle is a 16-bit item in Win16 and a
32-bit item in Win32. It makes more sense to tag a handle as a handle than as an integer
or a long. Porting such code will be much easier - you change the denition of the variable
only while the rest of your code remains untouched.
Here is a list of common data types and their tags. It is not exhaustive and you will quite
likely make up several of your own during any large project.
h
dte
handle
date
16 or 32 bit handle
stored in either a double or variant
hWnd
dteDue
The body of the variable's name is comprised of one or more complete words, with each
word starting with a capital letter, as in ThingToProcess. There are a few rules to keep in
mind when forming the body of the name.
Use multiple words - but use them carefully. It can be hard to remember whether the
amount due is called AmountDue or DueAmount. Start with the fundamental and generic
thing and then add modiers as necessary. An amount is a more fundamental thing (what's
a due?), so you should choose AmountDue. Similarly:
Correct
DateDue
NameFirst
ColorRed
VolumeHigh
StatusEnabled
Incorrect
DueDate
FirstName
RedColour
HighVolume
EnabledStatus
You will generally nd that nouns are more generic than adjectives. Naming variables this
way means that related variables tend to sort together, making cross reference listings more
useful.
There are a number of qualiers that are commonly used when dealing with a set of things
(as in an array or table). Consistent use of standard modiers can signicantly aid in code
maintenance. Here is a list of common modiers and their meaning when applied to sets of
things:
193
Coding Standards
Count
Min
Max
First
Last
Cur
Next
Prev
count
minimum
maximum
rst
last
current
next
previous
the
the
the
the
the
the
the
the
SelectedCount
BalanceMin
RateHigh
CustomerFirst
InvoiceLast
ReportCur
AuthorNext
DatePrev
At rst, placing these modiers after the body of the name might take a little getting used
to; However, the benets of adopting a consistent approach are very real.
We cannot use the C conventions here as they rely on the fact that C is case-sensitive. We
need to come up with our own convention. The use of an upper case 'T' may seem at odds
with the other conventions so far presented but it is important to visually distinguish the
names of UDT denitions from the names of variables. Remember that a UDT denition
is not a variable and occupies no space.
Besides which, that's how we do it in Delphi.
31.10 Arrays
There is no need to dierentiate the names of arrays from scalar variables because you will
always be able to recognise an array when you see it, either because it has the subscripts
after it in parentheses or because it is wrapped up inside a function like UBound that only
makes sense for arrays.
Array names should be plurals. This will be especially helpful in the transition to collections
in VB4.
You should always dimension arrays with both an upper and a lower bound. Instead of:
194
Procedure
try:
Dim mCustomers(0 To 10) as TCustomer
Many times, it makes far more sense to create 1-based arrays. As a general principle, try
to create subscript ranges that allow for clean and simple processing in code.
31.11 Procedure
Procedures are named according to the following convention:
verb.noun
verb.noun.adjective
Bad:
CustomerLookup should be LookupCustomer
GetNextCustomer should be GetCustomerNext
Scoping rules for procedures are rather inconsistent in VB. Procedures are global in modules
unless they are declared Private; they are always local in forms in VB3, or default to Private
in VB4 but can be Public if explicitly declared that way. Does the term "dog's breakfast"
mean anything?
Because the event procedures cannot be renamed and do not have scope prexes, user
procedures in VB also should NOT include a scope prex. Common modules should keep
all procedures that are not callable from other modules Private.
195
Coding Standards
31.13 Parameters
Within the body of a procedure, parameter names have a very special status. The names
themselves are local to the procedure but the memory that the name relates too may not be.
This means that changing the value of a parameter may have an eect at a totally dierent
scope to the procedure itself and this can be the cause of bugs that are particularly dicult
to track down later. The best solution is to stop it from happening in the rst place.
Ada has a really neat language facility whereby all paramaters to a procedure are tagged
as being one of the types In, Out or InOut. The compiler then enforces these restrictions
on the procedure body; it is not allowed to assign from an Out parameter or assign to an
In parameter. Unfortunately, no mainstream language supports this facility so we need to
kludge it.
Always make sure you are absolutely clear about how each parameter is to be used. Is it
used to communicate a value to the procedure or to the caller or both? Where possible,
declare input parameters ByVal so that the compiler enforces this attribute. Unfortunately,
there are some data types that cannot be passed ByVal, notably arrays and UDTs.
Each parameter name is formed according to the rules for forming variable names. Of
course, parameters are always at procedure level (local) scope so there will not be a scope
character. At the end of each parameter name, add an underscore followed by one of the
words IN, OUT or INOUT as appropriate. Use upper case to really make these stand out
in your code. Here is an example:
Sub GetCustomerSurname(ByVal nCustomerCode_IN as Long, _
sCustomerSurname_OUT As String)
If you see an assignment to a variable that ends in _IN (ie if it is to the left of the '=' in
an assignment statement) then you probably have a bug. Ditto if you see a reference to the
value of a variable that ends in _OUT. Both these statements are extremely suspect:
nCustomerCode_IN = nSomeVariable
nSomeVariable = nCustomerCode_OUT
196
Constants
In the body of the function, you should always assign the required return value to Result.
You are also free to check the current value of Result. You can also read the value of the
function return variable.
This might not sound like a big deal if you have never had the ability to use it but once
you try it you will not be able to go back to working without it.
Don't forget that functions have a data type too, so their name should be prexed in the
same way as a variable.
31.15 Constants
The formatting rules for constants are going to be very dicult for us all. This is because
Microsoft has done an about-face on constant naming conventions. There are two formats
used by Microsoft for constants and unfortunately we will need to work with both formats.
While it would be possible for someone to work through the le of MS-dened constants and
convert them to the new format, that would mean that every article, book and published
code fragment would not match our conventions.
A vote failed to resolve the issue - it was pretty much evenly split between the two alternatives - so I have had to make an executive decision which I will try to explain. The decision
is to go with the old-style constant formats.
Constants are coded in ALL_UPPER_CASE with words separated by underscores. Where
possible, use the constant names dened in the Constant.Txt le - not because they are
particularly well-formed or consistent but because they are what you will see in the documentation and in books and magazine articles.
The formatting of constants may well change as the world moves to VB4 and later, where
constants are exposed by OLE objects through a type library and where the standard format
is to use InitialCapitalLetters prexed with some unique, lower-case tag (as in vbYesNoCancel).
When writing modules that are to be called from various places (especially if it will be used
in several programs), dene global constants that can be used by the client code instead
of magic numbers or characters. Use them internally too, of course. When dening such
197
Coding Standards
constants, make up a unique prex to be added to each of them. For example, if I am
developing a Widget control module, I might dene constants like this:
Global
Global
Global
Global
Global
Global
Global
Global
Const
Const
Const
Const
Const
Const
Const
Const
WDGT_STATUS_OK = 0
WDGT_STATUS_BUSY = 1
WDGT_STATUS_FAIL = 2
WDGT_STATUS_OFF = 3
WDGT_ACTION_START = 1
WDGT_ACTION_STOP = 2
WDGT_ACTION_RAISE = 3
WDGT_ACTION_LOWER = 4
198
Controls
supplied by vendors to interface to a control, the scope can be considered the control, and
the data type is one of the special types supported by a particular property or method of
that control. In other words, MB_ICONSTOP has a scope of MB (message box) and a
data type of ICON. Of course, I think it should have been MB_ICON_STOP and that
MB_YESNOCANCEL should have been MB_BUTTONS_YESNOCANCEL, but you
can't ever accuse Microsoft of consistency. I hope this sheds some light on why I decided
to go this way.
I suspect this issue will generate further discussion as we all get some experience in applying
this standard.
31.16 Controls
All controls on a form should be renamed from their default Textn names, even for the
simplest of forms.
The only exceptions to this are any labels used only as static prompts on the surface of
a form. If a label is referenced anywhere in code, even once, then it must be given a
meaningful name along with the other controls. If not, then their names are not signicant
and can be left to their default "Labeln" names. Of course, if you are really keen, you may
give them meaningful names but I really think we are all busy enough that we can safely
skip this step. If you don't like to leave those names set to their defaults (and I'll confess to
being in that group), you may nd the following technique useful. Create a control array
out of all the inert labels on a form and call the array lblPrompt( ). The easy way to do this
is to create the rst label the way you want it (with the appropriate font, alignment and so
on) and then copy and paste it as many times as is necessary to create all the labels. Using
a control array has an additional benet because a control array uses up just one name in
the form's name table.
Take the time to rename all controls before writing any code for a form. This is because
the code is attached to a control by its name. If you write some code in an event procedure
and then change the name of the control, you create an orphan event procedure.
Controls have their own set of prexes. They are used to identify the type of control so that
code can be visually checked for correctness. They also assist in making it easy to know the
name of a control without continually needing to look it up. (See "Cradle to Grave Naming
of Data Items" below.)
199
Coding Standards
Using generic prexes means that your code is less dependent on the particular control
variant that is used in an application and therefore makes code re-use simpler. Only differentiate between variants of a fundamental control if your code is totally dependent on
some unique attribute of that particular control. Otherwise, use the generic prexes where
possible. Table of Standard Control Prexes
The following table is a list of the common types of controls you will encounter together
with their prexes:
Prex
cbo
chk
cmd
dat
dir
dlg
drv
ela
l
fra
frm
gau
gra
img
lbl
lin
lst
mci
mnu
mpm
mps
ole
opt
out
pic
pnl
rpt
sbr
shp
spn
ssh
tgd
tmr
txt
Control
Combo box
Checkbox
Command button
Data control
Directory list box
Common dialog control
Drive list box
Elastic
File list box
Frame
Form
Gauge
Graph
Image
Label
Line
List box
MCI control
Menu control
MAPI Message
MAPI Session
OLE control
Option button
Outline control
Picture
Panel
Report
Scroll bar (there is no need to distinguish orientation)
Shape
Spin
Spreadsheet control
Truegrid
Timer
Textbox
200
Menu Controls
There is often a single Timer control in a project, and it is used for a number of things.
That makes it dicult to come up with a meaningful name. In this situation, it is acceptable
to call the control simply "Timer".
I think you get the idea. It's really very simple. It's also incredibly dicult to do EVERY
time, and it's only by doing it EVERY time that you get the real payos. It is just SO
tempting to code the above line like this:
sCustCode = Format$(gnCustomerCode)
201
Coding Standards
31.21 Objects
There are a number object types that are used often in VB. The most common objects (and
their type tags) are:
Prex
db
ws
rs
ds
ss
tbl
qry
tdf
qdf
rpt
idx
d
xl
wrd
Object
Database
Workspace
Recordset
Dynaset
Snapshot
Table
Query
TableDef
QueryDef
Report
Index
Field
Excel object
Word object
202
The benet of generic control variables is that they can point to any type of control. You
can test what type of control a generic control variable is currently pointing to by using the
TypeOf statement:
If TypeOf ctlText Is Textbox Then
Be careful here; this only looks like a normal If statement. You cannot combine TypeOf
with another test, nor can you use it in a Select statement.
The fact that the variable can point to any control type is also its weakness. Because
the compiler does not know what the control variable will be pointing to at any point in
time, it cannot check what you are doing for reasonableness. It must use run-time code
to check every action on that control variable so those actions are less ecient, which just
compounds VB's reputation for sluggish performance. More importantly, however, you can
end up with the variable pointing to an unexpected control type, causing your program to
explode (if you are lucky) or to perform incorrectly.
Specic control variables are also pointers. In this case however, the variable is constrained
to point only to controls of a particular type. For example:
Dim txtCustomerCode as Textbox
The problem here is that I have used the same three-letter tag to denote a control object
variable as I would have used to name an actual text control on a form. If I am following
these guidelines, then I will have a control called by the same name on a form. How am I
supposed to assign it to the variable?
I can use the form's name to qualify the reference to the actual control, but that is confusing
at best. For that reason, object control variable names should be distinct from actual
controls, so we extend the type code by using a single letter "o" at the front. The previous
denition would more correctly be:
Dim otxtCustomerCode as Textbox
This is another one I had to agonise over. I actually like the name better if the "o" follows
the tag, as in txtoCustomerName. However, it is just too hard to see that little "o" buried
in all those characters. I also considered using txtobjCustomerName, but I think that is
getting a little bit too long. I'm happy to look at any other alternatives or arguments for
or against these ones.
Actually, this is not as big a problem as you might imagine, because in most cases these
variables are used as parameters to a generic function call. In that case, we will often use
the tag alone as the name of the parameter so the issue does not arise.
A very common use of control object variables is as parameters to a procedure. Here is an
example:
Sub SelectAll(txt As TextBox)
txt.SelStart = 0
203
Coding Standards
txt.SelLength = Len(txt.Text)
End Sub
If we place a call to this procedure in the GotFocus event of a textbox, that textbox has all
its text highlighted (selected) whenever the user tabs to it.
Notice how we denote the fact that this is a generic reference to any textbox control by
omitting the body of the name. This is a common technique used by C/C++ programmers
too, so you will see these sorts of examples in the Microsoft documentation.
To show how generic object variables can be very useful, consider the case where we want
to use some masked edit controls as well as standard text controls in our application. The
previous version of the routine will not work for anything but standard textbox controls.
We could change it as follows:
Sub SelectAll(ctl As Control)
ctl.SelStart = 0
ctl.SelLength = Len(ctl.Text)
End Sub
By dening the parameter to be a generic control parameter, we are now allowed to pass
any control to this procedure. As long as that control has SelStart, SelLength and Text
properties, this code will work ne. If it does not, then it will fail with a run-time error;
the fact that it is using late binding means that the compiler cannot protect us. To take
this code to production status, either add specic tests for all the known control types you
intend to support (and only process those ones) or else add a simple error handler to exit
gracefully if the control does not support the required properties:
Sub SelectAll(ctl As Control)
On Error Goto SelectAll_Error
ctl.SelStart = 0 ctl.SelLength = Len(ctl.Text)
SelectAll_Exit:
Exit Sub
SelectAll_Error:
Resume SelectAll_Exit
End Sub
204
Source Files
for that API where the name is prexed with a unique code (the same one used to prex
its global constants).
If the mythical Widgets module needed to use SendMessage, it must declare:
Declare Function WDGT_SendMessage Lib "User" Alias "SendMessage" _
(ByVal hWnd As Integer, ByVal wMsg As Integer, _
ByVal wParam As Integer, lParam As Any) As Long
This eectively creates a private declaration of SendMessage that can be included with any
other project without naming conicts.
where SSS is a three-character system or sub-system name, CCCC is the four-character form
name code and V is an (optional) single digit that can be used to denote dierent versions
of the same le. EXT is the standard le extension assigned by VB, either FRM/FRX,
BAS or CLS.
@TODO: rewrite the short name specic parts, update to win32.
The importance of starting with a system or sub-system name is that it is just too easy
in VB to save a le in the wrong directory. Using the unique code allows us to nd the
le using directory search (sweeper) programs and also minimises the chance of clobbering
another le that belongs to another program, especially for common form names like MAIN
or SEARCH.
Reserving the last character for a version number allows us to take a snapshot of a particular
le before making radical changes that we know we might want to back out. While tools
like SourceSafe should be used where possible, there will inevitably be times when you will
need to work without such tools. To snapshot a le, you just remove it from the project,
swap to a Dos box or Explorer (or File Manager or whatever) to make a new copy of the
le with the next version number and then add the new one back in. Don't forget to copy
the FRX le as well when taking a snapshot of a form le. Coding Standards
The rest of this document addresses issues relating to coding practices. We all know that
there is no set of rules that can always be applied blindly and that will result in good code.
Programming is not an art form but neither is it engineering. It is more like a craft: there
are accepted norms and standards and a body of knowledge that is slowly being codied
and formalised. Programmers become better by learning from their previous experience and
by looking at good and bad code written by others. Especially by maintaining bad code.
205
Coding Standards
Rather than creating a set of rules that must be slavishly followed, I have tried to create
a set of principles and guidelines that will identify the issues you need to think about and
where possible indicate the good, the bad and the ugly alternatives.
Each programmer should take responsibility for creating good code, not simply code that
adheres to some rigid standard. That is a far nobler goal - one that is both more fullling
to the programmer and more useful to the organisation.
The underlying principle is to keep it simple.
Eschew obfuscation.
31.26 IF
Write the nominal path through the code rst, then write the exceptions. Write your code so
that the normal path through the code is clear. Make sure that the exceptions don't obscure
the normal path of execution. This is important for both maintainability and eciency.
Make sure that you branch correctly on equality. A very common mistake is to use >
instead of >= or vice versa.
206
SELECT CASE
Put the normal case after the If rather than after the Else. Contrary to popular thought,
programmers really do not have a problem with negated conditions. Create the condition
so that the Then clause corresponds to normal processing.
Follow the If with a meaningful statement. This is somewhat related to the previous point.
Don't code null Then clauses just for the sake of avoiding a Not. Which one is easier to
read:
If EOF(nFile) Then
' do nothing
Else
ProcessRecord
End IF
If Not EOF(nFile) Then
ProcessRecord
End If
Always at least consider using the Else clause. A study of code by GM showed that only
17% of If statements had an Else clause. Later research showed that 50 to 80% should have
had one. Admittedly, this was PL/1 code and 1976, but the message is ominous. Are you
absolutely sure you don't need that Else clause?
Simplify complicated conditions with boolean function calls. Rather than test twelve things
in an If statement, create a function that returns True or False. If you give it a meaningful
name, it can make the If statement very readable and signicantly improve the program.
Don't use chains of If statements if a Select Case statement will do. The Select Case
statement is often a better choice than a whole series of If statements. The one exception
is when using the TypeOf condition which does not work with Select Case statements.
207
Coding Standards
sGrade = "C"
Case 80 To 89:
sGrade = "B"
Case 60 To 69:
sGrade = "D"
sGrade = "F"
Case 90 To 100:
sGrade = "A"
Case 50 To 59:
sGrade = "E"
Case Else:
End Select
However, the natural way to code this would be do follow the natural order of the scores:
Select Case iScore
Case Is < 50:
sGrade = "F"
sGrade = "E"
sGrade = "D"
sGrade = "C"
sGrade = "B"
End Select
Not only is this easier to understand, it has the added advantage of being more robust if the scores are later changed from integers to allow for fractional points, then the rst
version would allow 89.1 as an A which is probably not what was expected.
On the other hand, if this statement was identied as being the bottleneck in a program
that was not performing quickly enough, then it would be quite appropriate to order the
cases by their statistical probability of occurring, in which case you would document why
you did it that way in comments.
This discussion was included to reinforce the fact that we are not seeking to blindly obey
rules - we are trying to write good code. The rules must be followed unless they result in
bad code, in which case they must not be followed.
31.28 DO
Keep the body of a loop visible on the screen at once. If it is too long to see, chances are it
is too long to understand buried inside that loop and should be taken out as a procedure.
Limit nesting to three levels. Studies have shown that the ability of programmers to understand a loop deteriorates signicantly beyond three levels of nesting.
208
FOR
31.29 FOR
See DO above.
Never omit the loop variable from the Next statement. It is very hard to unravel loops if
their end points do not identify themselves properly.
Try not to use i, j and k as the loop variables. Surely you can come up with a more
meaningful name. Even if it's something generic like iLoopCounter it is better than i.
This is a suggestion only. I know how convenient single-character loop variable names are
when they are used for a number of things inside the loop, but do think about what you
are doing and the poor programmer who has to gure out what i and j actually equate to.
31.31 GOTO
Do not use Goto statements unless they make the code simpler. The general consensus of
opinion is that Goto statements tend to make code dicult to follow but that in some cases
the exact opposite is true.
In VB, you have to use Goto statements as an integral part of error handling, so there is
really no choice about that.
You may also want to use Goto to exit from a very complex nested control structure. Be
careful here; if you really feel that a Goto is warranted, perhaps the control structure is just
too complex and you should decompose the code into smaller routines.
That is not to say that there are no cases where the best approach is to use a Goto. If you
really feel that it is necessary then go ahead and use one. Just make sure that you have
thought it through and are convinced that it really is a good thing and not a hack.
209
Coding Standards
End If
End Sub
The one that seems to be the clearest in these simple, skeletal examples is the rst one
and it is in general a good approach to coding simple procedures. This structure becomes
unwieldy when the main body of the procedure (denoted by the '' above) is nested deep
inside a series of control structures required to determine whether that body should be
executed. It is not at all dicult to end up with the main body indented half way across
the screen. If that main body is itself complex, the code looks quite messy, not to mention
the fact that you then need to unwind all those nested control structures after the main
body of the code.
When you nd this happening in your code, adopt a dierent approach: determine whether
you should proceed and, if not, exit prematurely. Both of the other techniques shown work.
Although I think that the Exit statement is more 'elegant', I am forced to mandate the use
of the Goto ExitLabel as the standard. The reason that this was chosen is that sometimes
you need to clean up before exiting a procedure. Using the Goto ExitLabel construct means
that you can code that cleanup code just once (after the label) instead of many times (before
each Exit statement).
If you need to prematurely exit a procedure, prefer the Goto ExitLabel construct to the
Exit Sub or Exit Function statements unless there is no chance that any cleanup will be
needed before those statements.
210
EXIT DO/FOR
One case where the Exit statement is very much OK is in conjunction with gatekeeper
variables to avoid unwanted recursion. You know:
Sub txtSomething_Change()
Dim bBusy As Integer
If bBusy Then Exit Sub
bBusy = True
. . . ' some code that may re-trigger the Change() event
bBusy = False
End Sub
This will always loop through all elements of the array, even if the item is found in the rst
element. Placing an Exit For statement inside the If block would improve performance with
no loss of clarity in the code.
Do avoid the need to use these statements in deeply nested loops. (Indeed, avoid deeply
nested loops in the rst place.) Sometimes there really is no option, so this is not a hard
and fast rule, but in general it is dicult to determine where an Exit For or Exit Do will
branch to in a deeply nested loop.
31.34 GOSUB
The Gosub statement is not often used in VB and with good reason. In most cases it does
not oer any advantages over the use of standard Sub procedures. Indeed, the fact that
the routine that is executed by a Gosub is actually in the same scope as the calling code is
generally a dangerous situation and should be avoided.
However, this very same attribute is sometimes very useful. If you are writing a complex
Sub or Function procedure you would generally try to identify discrete units of processing
that can be implemented as separate procedures that this one can call as appropriate. In
some particular cases, however, you will nd that the amount of data that needs to be
passed around between these related procedures becomes quite absurd. In those cases,
211
Coding Standards
implementing those subordinate routines as subroutines inside the main procedure can
signicantly simplify the logic and improve the clarity and maintainability of the code.
The particular situation where I have found this technique useful is in creating multilevel reports. You end up with procedures like ProcessDetail, ProcessGroup, PrintTotals,
PrintSubTotals, PrintHeadings and so forth, all of which need to access and modify a
common pool of data items like the current page and line number, the current subtotals
and grand totals, the current and previous key values (and a whole lot more I can't think
of right now).
For situations like this, this standard does permit the use of Gosub statements when appropriate. However, be careful when you decide to use this approach. Treat the enclosing Sub
or Function as if it was a single COBOL program; that is essentially what it is from a control and scope point of view. In particular, ensure that there is only one Return statement
at the end of every subroutine. Also be sure that there is an Exit Sub or Exit Function
statement before all the subroutine code to avoid the processing falling through into that
code (which is highly embarrassing).
212
Error Handling
values in any convenient order by "setting" them in the UDT. Here is a rather trivial
example that demonstrates the technique:
Type TUserMessage
sText
As
sTitle
As
nButtons
As
nIcon
As
nDefaultButton As
nResponse
As
End Type
String
String
Integer
Integer
Integer
Integer
213
Coding Standards
HandleError:
Resume Done
End Sub
Within the body of the procedure, you may create regions where dierent error handling is
required. To put it bluntly, VB stinks in this area. The technique I prefer is to temporarily
disable error trapping and to test for errors using in-line code, something like this:
DoThingOne
DoThingTwo
On Error Resume Next
DoSomethingSpecial
If Err Then
HandleTheLocalError
End If
On Error Goto HandleError
Another technique is to set ag variables to indicate where you are up to in the body of
the procedure and then to write logic in the error handler to take dierent action based on
where the error occurred. I do not generally like this approach but confess that I have used
it on occasion because I just could not gure out a way I liked better.
If you get the feeling that I am not too impressed with VB's error handling mechanisms,
you are absolutely correct. For that reason, I am not going to mandate any particular style
of error handling other than to say that you should implement sucient error handling in
your application to ensure that it works correctly and reliably. If you implement an error
handler in a procedure, use the naming convention as shown previously for the line labels.
214
Error Handling
Or ERR_DISPLAY_MESSAGE
End Select
Resume DoSomething_Exit
End Sub
Note the Select Case statement used to trap errors that will be handled locally. All expected
errors that need to be handled locally will have a corresponding Case statement. The
individual cases may choose to use a dierent form of the Resume statement to control the
ow of the program. Any handled errors that do not do a Resume will fall through to the
nal statement that will cause the procedure to exit gracefully.
Any errors that have not been handled will be passed on to DefErrorHandler. Individual
cases may also call upon DefErrorHandler to log the message and display a user alert.
The rst parameter to DefErrorHandler is the error number that has been trapped. The
second parameter is a string to denote where the error occurred. This string is used in
message boxes and in the log le. The third parameter is a message that is to be written to
the log le and displayed to the user. The fourth parameter is a set of ags indicating what
the procedure should do. See the constants dened in ERRORGEN for more information.
The default error handler can also provide exits in which you can further customise the
way the generic error handler works without modifying the code. These would take the
form of global procedures that are called at convenient places in the processing cycle. By
customising how these procedures operate you can change the appearance and function of
the error handling process.
Here are some examples of constants and procedures you may want to implement:
gsERROR_LogFileName
a constant that denes the root le name for the log le
gsERROR_LogFilePath
a constant that denes the path for the log le can be left empty to use App.Path or can
be changed to a variable and set during program initialisation to a valid path
gsERROR_MSG_???
a series of constants that dene some strings, buttons and icons for default message boxes.
gsERROR_LOGFORMAT_???
a series of constants that dene the delimiters in and the format of the lines in the log le
sERROR_GetLogLine
a function that can totally redene the format of the log line
sERROR_GetLogFileName
a function that returns the complete path to the log le; this function can be customised to
allow non-xed locations for the log le to be handled (see the comments in the function)
ERROR_BeforeAbort
215
Coding Standards
a sub that is called just before the program is terminated with an End statement; this can
be used to do any last-minute clean-up in an error situation
bERROR_FormatMessageBox
a function that can do custom formatting of the error message box or can replace the
message box altogether with custom processing (eg. using a form to gather additional
information from the user before continuing or ending)
Unfortunately, the other two prime reasons to indent (the For and Select statements) do
not t neatly into this scheme.
When writing a For statement, the natural inclination is to indent four characters:
For nValueCur = nValueMin To nValueMax
MsgBox . . .
Next nValueCur
I'm not too fussed about this as long as the For is not too long and does not encase further
indentation. If it is long and if it does contain more indentation, it is hard to keep the closing
216
In the second technique, the Case statements themselves are indented by a small amount
(two or three spaces) and the statements they control are super-indented, essentially suspending the rules if indentation:
Select
Case
Case
Case
Case nCurrentMonth
1,3,5,7,8,10,12: nDaysInMonth = 31
4,6,9,11:
nDaysInMonth = 30
2
If IsLeapYear(nCurrentYear) Then
nDaysInMonth = 29
Else
nDaysInMonth = 28
End If
Case Else:
DisplayError "Invalid Month"
End Select
Notice how the colon is used to allow the statements to appear right beside the conditions.
Notice also how you cannot do this for If statements.
Both techniques are valid and acceptable. In some sections of a program, one or the other
will be clearer and more maintainable, so use common sense when deciding.
Let's not get too hung up on indentation, folks. Most of us understand what is acceptable
and what is not when it comes to indenting code.
217
Coding Standards
who wrote it
what it is supposed to do
what the parameters mean (both input and output)
what it returns (if it's a function)
what assumptions it makes about the state of the program or environment
any known limitations
an amendment history
Looks like a lot, doesn't it? The truth of the matter is you will not write one of these for
every procedure. For many procedures, a few lines of comments at the top is all you need to
convey all the information required. For event procedures you generally do not need such
a comment block at all unless it contains signicant amounts of code.
218
Commenting Code
For signicant procedures, however, until you can write one of these you haven't thought
the processing through enough to start coding. If you don't know what's in that header,
you can't start maintaining that module. Nor can you hope to reuse it. So you may as well
type it into the editor before you write the code and pass on your knowledge rather than
require someone else to read through the code to decipher what is going on.
Notice that the header does not describe how the function does its job. That's what the
source code is for. You don't want to have to maintain the comments when you later change
the implementation of a routine. Notice also that there are no closing characters at the end
of each comment line. DO NOT DO THIS:
'****************************************************
'* MY COMMENT BLOCK
*
'*
*
'* This is an example of a comment block that is
*
'* almost impossible to maintain. Don't do it !!!
*
'****************************************************
This might look 'nice' (although that really is debatable) but it is very hard to maintain
such formatting. If you've got time to do this sort of thing, you obviously aren't busy
enough.
In general terms, naming variables, controls, forms and procedures sensibly, combined with
a well-thought-out comment block, will be sucient commenting for most procedures. Remember, you are not trying to explain your code to a non-programmer; assume that the
person looking at the code knows VB pretty well.
In the body of the procedure, there are two types of comments that you will use as required:
In-line comments and End of Line Comments.
219
Coding Standards
Why we chose a particular option:
'
'
'
'
'
'
'
Notice that we are not documenting what is self-evident from the code. Here are some
examples of inappropriate In-line comments:
' Declare local variables
Dim nEmployeeCur As Integer
' Increment the array index
nEmployeeCur = nEmployeeCur + 1
' Call the update routine
UpdateRecord
Comment what is not readily discernible from the code. Do not re-write the code in English,
otherwise you will almost certainly not keep the code and the comments synchronised and
that is very dangerous. The reverse side of the same coin is that when you are looking at
someone else's code you should totally ignore any comments that relate directly to the code
statements. In fact, do everyone a favour and remove them.
mnEmployeeCur = nEmployeeCur
UpdateProgress
Notice that EOL comments may be continued onto additional lines if necessary as shown
in the rst example.
Here are some examples of inappropriate EOL comments:
220
Commenting Code
221
32.1.2 Chr
Returns a string character that corresponds to the ASCII code in the range 0-255. Reverse
of the Asc function.
Usage
Chr(code)
code = ASCII code.
Example
char = Chr(97)
Here char gets value "a".
32.1.3 Len
Returns the length of a given string or 0 for Empty.
Usage
223
32.1.4 Left
Returns a given number of characters from the left hand side of a string
Usage
Left(string,x)
string = string to use
x = number of characters
Example
mystring = InputBox("Enter a string")
mystring = Left(mystring, 4)
MsgBox "First four characters of your input are " + mystring
e.g. where the input mystring is "Hello", the output mystring will be "Hell"
32.1.5 Right
Returns a given number of characters from the right hand side of a string
Usage
Right(string, x)
string = string to use
x = number of characters
Example
224
String Manipulation
mystring = InputBox("Enter a string")
mystring = Right(mystring, 4)
MsgBox "Last four characters of your input are " + mystring
e.g. where the input mystring is "Hello", the output mystring will be "ello"
225
32.1.8 Trim
Removes leading and trailing spaces from a string
Usage
Trim(string)
string = string to use
Example
mystring = Trim(mystring)
e.g. where the original value of mystring was " Hello ", the new value of mystring will be
"Hello".
32.1.9 LCase
Converts a string to lowercase
Usage
LCase(string)
string = string to use
Example
mystring = LCase(mystring)
e.g. where the original value of mystring was "HELLO", the new value of mystring will be
"hello".
32.1.10 UCase
Converts a string to uppercase
Usage
UCase(string)
string = string to use OR character
226
String Manipulation
Example
mystring = UCase(mystring)
e.g. where the original value of mystring was "Hello", the new value of mystring will be
"HELLO".
32.1.11 String
Creates a string with the specied length of the specied character
Usage
String(length, character)
length = length of string
character = character to ll string with
Example
mystring = String(5,"a")
e.g. the new value of mystring will be "aaaaa".
32.1.12 Space
Creates a string with the specied length of space
Usage
Space(length)
length = length of string
Example
mystring = Space(5)
e.g. the new value of mystring will be "
".
32.1.13 StrConv
Returns a converted string as specied.
Usage
StrConv(string, conversion,LCID)
string = string to use
227
32.2.2 Cos
Returns the cosine of a number
Usage
Cos(integer)
Example
msgbox "The cosine of 4 is " & cos(4)
Important note!
The input angle for all VB trigometric functions should be in radians1 . To convert degrees
to radians, multiply degrees by pi / 180.
32.2.3 Sin
Returns the sine of a number
Usage
1
228
http://en.wikipedia.org/wiki/radian
Mathematical Functions
Sin(integer)
Example
msgbox "The sine of 4 is " & sin(4)
Important note!
The input angle for all VB trigometric functions should be in radians. To convert degrees
to radians, multiply degrees by pi / 180.
32.2.4 Tan
Returns the tangent of an angle
Usage
Tan(integer)
Example
msgbox "The tangent of 4 is " & tan(4)
Important note!
The input angle for all VB trigometric functions should be in radians. To convert degrees
to radians, multiply degrees by pi / 180.
32.2.5 Val
Returns the numeric value of a string
Uses "." as decimal separator. ( It does not depend on the Regional Settings )
Usage
Val(string)
Example
Val("5")
The return value will be 5
Val("10.6")
The return value will be 10.6
Val("Hello World")
The return value will be 0 (zero)
txtOutput.Text = Val(txtNumber1.Text) * Val(txtNumber2.Text)
The text box txtOutput will contain the product of the numbers in the txtNumber1 and
txtNumber2 text boxes. If either of the text boxes contains a value that cannot be evaluated
to a number then the output will be 0 (zero).
229
32.2.6 Rnd
Returns a oating point number less than 1 but greater than or equal to 0.
Usage
Rnd[(number)]
You can change the seed the random number generator uses by providing a value as a
parameter e.g. Rnd(500) will use the number 500 as the seed in generating random numbers.
If you provide a value less than zero then the same number will be returned by the Rnd()
function
A value of zero will return the most recently generated random number.
Providing no value will generate the next random number in the sequence.
Providing a positive number will generate the next random number in the sequence using
number as a seed.
Example
myvar = Rnd() * 10
myvar will become a random number between 0 and 10.
myvar = (Rnd() * 15) + 25
myvar will become a random number between 25 and 40.
myvar = (Rnd(12345) * 50) - 25
myvar will become a random number between -25 and 25 and 12345 will be used as the
seed to generate this number.
32.2.7 Int
Returns the integer portion of a number.
Usage
Int(n)
Where n is the number to return
Example
Int(10)
Returns 10
Int(5.1)
Returns 5
Int(5.86)
Returns 5
MsgBox "The integer portion of " & myvar & " is " & Int(myvar) & "."
230
231
32.3.2 InputBox
Displays a simple input box for user to enter data
Usage
Variable=InputBox(Prompt,Title)
Variable = variable to which the input value will be stored Prompt = text displayed to the
user in the input box Title = title of the input box
Examples
myint=InputBox("Enter a number","Enter a number")
32.4.2 Time
Returns or sets the current system time
Example
MsgBox "The time now is " & Time & "."
Sidenote: be wary of changing date and time in your program. Many other programs will
depend upon the system's date and time being set correctly, so altering it could have a knock
on eect. Use date and time changes sparingly, and notify the user if this is being done.
32.4.3 Timer
Returns the number of seconds since midnight
Example
NumHrs=Int(Timer / 3600) MsgBox "Currently in hour " & NumHrs
32.4.4 WeekdayName
Returns the name of a day of the week
Usage
232
Miscellaneous
WeekdayName(x,FirstDayOfWeek)
x = integer between 1 and 7 FirstDayOfWeek (OPTIONAL) = the day counted as the rst
in the week. The default is Sunday.
Example
DayName=WeekdayName(i,vbMonday)
32.5 Miscellaneous
32.5.1 IsNull
Returns true if an expression is null and false otherwise.
Example:
If IsNull(vntMyVariantVar) Then
Debug.Print "vntMyVariantVar is null"
Else
Debug.Print "vntMyVariantVar is NOT null"
End If
32.7 Selection
If... Then... Else... Performs selection based upon criteria. A cornerstone of any VB
program.
'Usage'
Single Line Selection
If condition Then action
Note that only one line can be used with this style of statement
Multi Line Selection
If condition Then
[code to execute if condition is true]
EndIf
233
Condition is a condition which must be met for the code within the statement to be executed.
It can make use of the modiers AND, NOT, OR and XOR (e.g. x=4 AND y > 6). See
also the Examples section.
32.7.1 Examples
Simple one-line selection:
If x < 10 then MsgBox "The number is below 10"
Slightly more complex one-line selection, using the AND modier to check two conditions
at one time.
If x > 10 AND x < 20 then MsgBox "The number is between 10 and 20"
Simple multi-line selection. The code between the If... and EndIf statements will be executed
only if the condition is met.
If Date = 25/12/2006 Then
MsgBox "The date is Christmas Day"
Call ChristmasDay
EndIf
Using the Else statement. If the variable i is over 4, the program will exit the subroutine.
Otherwise, it will go to the label SaveFile.
If i > 4 Then
Exit Sub
Else
Goto SaveFile
EndIf
Two If... Endif statements are used, one nested within the other. If i is 6 or 7, the code
between the inside If...Endif statements is executed. Then if i=7 the rst message box will be
shown; otherwise (i.e. if i=6) the other will be displayed. For both cases, the nal message
box will be shown. When nesting selection statements, it's often a good idea to indent them
as shown here.
If i = 6 OR i = 7 Then
If i = 7 Then
MsgBox "Today is Sunday"
Else
MsgBox "Today is Saturday"
EndIf
MsgBox "Today is the Weekend"
EndIf
234
33 Glossary
An alphabetical list of denitions crossreferenced to more thorough explanations in the
main text or in external documents.
33.1 A
argument
In computing jargon an argument is one of the pieces of data passed to a procedure1 .
Another name is parameter.
assign, assigning, assignment
Assignment is one of the fundamental operations of computing. All it means is copying a
value into the memory location pointed at by a variable. The value can be a literal or the
value of some other variable, q.v. Assignment2
33.2 B
ByRef
Declares an argument to a procedure3 as a pointer4 to the argument5 instead of as a copy of
the value of the argument. This allows the procedure to permanently change the variable.
If neither ByRef or ByVal is stated, ByRef is assumed by the compiler.
ByVal
Declares an argument to a procedure6 as a copy of the value of the argument. The value
of the original variable cannot be changed by the procedure. The value of the newly
created variable can be changed within the procedure, but this does not aect the variable
it was copied from. Programs are more robust if variables are declared ByVal whenever
possible since this means that an argument will not be unexpectedly changed by calling a
function. This also results in a faster program (see pg 758 of the Microsoft Visual Basic 6
Programmer's Guide).
1
2
3
4
5
6
235
Glossary
33.3 C
compiler directives7
These are instructions included in the text of the program that aect the way the compiler
behaves. For instance it might be directed to include one or another version of a piece of
code depending on whether the target operating system is Windows 95 or Windows XP.
33.4 I
Immediate Windows
This is the window in the IDE which receives output from Debug.Print. See ../IDE/8
33.5 J
JavaScript
JavaScript is an interpreted language mostly used to provide scripts inside web pages.
It is, however, not conned to such uses. See Jarithmetic9 , a case study in multilingual
programming and Crockford on JavaScript10 , the world's most misundertood language.
JScript
Microsoft's implementation of JavaScript as provided by the Windows Script Host. See
Jarithmetic11 for an example of its use.
33.6 O
Operands and Operators
An operand is operated on by an operator. Expressions are built of operands and operators.
For instance in this expression:
a = b + c
there are three operands (a, b, and c) and two operators (= and +).
Operands in Visual Basic Classic are either variables12 or literals13
7
8
9
10
11
12
13
236
http://en.wikibooks.org/wiki/..%2FCompiler%20Directives
http://en.wikibooks.org/wiki/..%2FIDE%2F
http://en.wikibooks.org/wiki/..%2FJArithmetic
http://www.crockford.com/javascript/
http://en.wikibooks.org/wiki/..%2FJArithmetic
Chapter 33.9 on page 237
Chapter 33.9 on page 237
33.7 P
procedure
A subroutine14 , function15 , method16 , or property17
33.8 R
ragged array
An array having rows of diering lengths. Such arrays are natural in languages such as C
and JavaScript but rather unusual in Visual Basic although there is nothing that prevents
their creation. In Visual Basic you can create such an array using the Array function:
aRagged = Array(Array(1, 2, 3), Array(1, 2))
Such arrays are inecient in Visual Basic because they are implemented as Variants but
functionaly identical arrays can be created as instances of a class and can be made much
more ecient both in storage space and execution time, although not quite as ecient as
C arrays.
real number
A variable that can hold a real number is one that can hold a number that can have any
value including fractional value. In computer languages variables only approximate real
numbers because the irrational numbers are also real, see Real number18 . In Visual Basic
Classic there are two real number types: Single and Double, see ../Data Types/19
reference
A variable that holds a pointer to a value rather than holding the the value itself. In strict
Visual Basic usage only object references work like this.
reference argument
A declaration of an argument using the ByRef20 keyword. Reference arguments allow
the subroutine or function to make changes to the variable in the calling routine. See
../Procedures and Functions/21 .
33.9 S
subroutine
14
15
16
17
18
19
20
21
237
Glossary
A procedure similar to a function but it does not return a value.
238
34 Work in Progress
This is a work area. Short pieces of text, reminders and links that don't warrant a page to
themselves but need a temporary home lest they be forgotten start life here.
34.1 Miscellaneous
http://www.elitevb.com/content/01,0057,01/01.aspx
Split Eective Programming into separate pages as it is now over 36K
Explain le format of VBP, frm, cls, bas, etc.
Registry entries that refer to VB components, how they are structured, what you can do
with them.
Side by side installation.
Windows standard dialogs http://builder.com.com/5100-6371_14-5800282.html,
http://www.vbaccelerator.com/home/VB/Code/Libraries/Common_Dialogs/
Folder_Browser/article.asp,
http://msdn.microsoft.com/library/default.
asp?url=/library/en-us/dnima01/html/ima0201.asp,
http://www.devx.com/
vb2themax/Tip/19255,
34.2 Graphics
http://www.a1vbcode.com/app-2571.asp
34.4 Networks
inet control winsock control comm control
239
Work in Progress
http://www.kellyethridge.com/
34.9 CGI
See:
http://www.vbcity.com/forums/faq.asp?fid=10&cat=Web+Development&
34.11 Sound
http://www.unh.edu/music/Visual_Basic_Programs/Visual_Basic.htm
http://www.devx.com/vb2themax/Tip/18323
http://www.ostrosoft.com/vb/projects/mci_play/index.asp
http://vbcity.com/forums/topic.asp?tid=103678
http://www.xtremevbtalk.com/archive/index.php/t-237143.html
http://www.vbforums.com/showthread.php?t=359612
http://www.vbaccelerator.com/home/VB/Code/vbMedia/Audio/Lossless_WAV_
Compression/article.asp
240
35 Links
35.1 External Links
Link
Visual Basic Tutorial1
Visual Basic 6 (VB6) tutorials and
source code2
VBCorLib3
Visual Basic Tutorials4
WinsockVB5
VBSpeed6
GPWiki7
XtremeVBTalk8
AllAPI9
Hardcore Visual Basic10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Comment
37 Lessons on Visual Basic 6 and sample
source code
Example Visual Basic 6.0 tutorials and
source code
Do exceptions in Visual Basic 6.0
Everything there is to know about sockets and Visual Basic 6.0. Still has active
forums. OFF LINE
Objective evidence you can use to help
you write faster code
Contains more game related Visual Basic
tutorials
A VB forum
Examples for Windows APIs
Bruce McKinney's masterpiece; shows
that you can write almost anything in
Visual Basic 6.0.
Sample Source Code
Free Source Code for VB
Another Free Source Code site for VB
Free examples(source code), Tutorials for
Visual Basic 6.0
http://www.vbtutor.net
http://www.vb6.us
http://www.kellyethridge.com/vbcorlib/index.shtml
http://www.pickatutorial.com/tutorials/vb_1.htm
http://www.winsockvb.com/
http://xbeat.net/vbspeed
http://gpwiki.org
http://www.visualbasicforum.com/
http://www.mentalis.org/vbexamples/list.php?category=MISC
http://vb.mvps.org/hardcore/
http://www.planet-source-code.com
http://www.vbcode.com/
http://www.a1vbcode.com/
http://www.vbexemple.com/
241
Links
Link
Visual Basic Beginners Tutorials15
15
242
http://www.visualbasictutorial.net/
Comment
Tutorials for Visual Basic.NET
36 Contributors
36.1 Authors and Contributors
Details of permissions given to include content from other sites and documents is on the
Credits and Permissions1 page.
Name
Batjew2
EugeneH3
Kwhitefoot5
Elliot Spencer
Aadnk6
1
2
3
4
5
6
7
8
9
10
11
12
Comment
Wrote almost everything to start, getting
help now.
Corrected mistakes and wrote the rest.
User name is now Orage4
Added: distinction between Type and
Class, optimization of programs. Split
into separate chapters. Added coding
standards, case studies, optimization.
Contributed many examples from his
web site: http://www.ilook.fsnet.co.
uk/index.htm
Some sections translated from the Norwegian Visual Basic7 WikiBook (by
Kwhitefoot8 ). Also wrote the Windows
Dialogs chapter.
Mark Trescowthick kindly gave permission for the http://www.gui.com.
au/resources/coding_standards.htm
GUI Computing coding standards10 and
http://www.avdf.com/ Australian Visual Developers Forum11 to be used, see
Credits and Permissions12 .
http://en.wikibooks.org/wiki/..%2FCredits%20and%20Permissions
http://en.wikibooks.org/wiki/User%3ABatjew
http://en.wikibooks.org/wiki/User%3AEugeneH
http://en.wikibooks.org/wiki/user%3Aorage
http://en.wikibooks.org/wiki/User%3AKwhitefoot
http://no.wikibooks.org/wiki/User:Aadnk
http://no.wikibooks.org/wiki/Visual_Basic
http://en.wikibooks.org/wiki/User%3AKwhitefoot
http://www.gui.com.au
http://en.wikibooks.org/wiki/%20GUI%20Computing%20coding%20standards
http://en.wikibooks.org/wiki/%20Australian%20Visual%20Developers%20Forum
http://en.wikibooks.org/wiki/..%2FCredits%20and%20Permissions
243
Contributors
244
37 Contributors
Edits
2
71
2
2
1
3
1
3
1
55
2
1
3
4
1
1
18
4
1
1
2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
User
A R King1
Adrignola2
Albmont3
Alexius084
Avicennasis5
Az15686
Bevo7
Bijee8
Carl Turner9
Dan Polansky10
Darklama11
Derbeth12
Gang6513
Hagindaz14
HethrirBot15
JackPotte16
Jguk17
Jomegat18
Jorgenev19
King jakob c 220
Krischik21
http://en.wikibooks.org/wiki/User:A_R_King
http://en.wikibooks.org/wiki/User:Adrignola
http://en.wikibooks.org/wiki/User:Albmont
http://en.wikibooks.org/wiki/User:Alexius08
http://en.wikibooks.org/wiki/User:Avicennasis
http://en.wikibooks.org/wiki/User:Az1568
http://en.wikibooks.org/wiki/User:Bevo
http://en.wikibooks.org/wiki/User:Bijee
http://en.wikibooks.org/wiki/User:Carl_Turner
http://en.wikibooks.org/wiki/User:Dan_Polansky
http://en.wikibooks.org/wiki/User:Darklama
http://en.wikibooks.org/wiki/User:Derbeth
http://en.wikibooks.org/wiki/User:Gang65
http://en.wikibooks.org/wiki/User:Hagindaz
http://en.wikibooks.org/wiki/User:HethrirBot
http://en.wikibooks.org/wiki/User:JackPotte
http://en.wikibooks.org/wiki/User:Jguk
http://en.wikibooks.org/wiki/User:Jomegat
http://en.wikibooks.org/wiki/User:Jorgenev
http://en.wikibooks.org/wiki/User:King_jakob_c_2
http://en.wikibooks.org/wiki/User:Krischik
245
Contributors
361
2
3
1
13
6
1
5
7
1
6
1
31
4
1
1
1
2
1
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
246
Kwhitefoot22
LlamaAl23
Mike.lifeguard24
Mts1525
Orage26
Panic2k427
Pmlineditor28
Psoup29
QuiteUnusual30
Ramac31
Recent Runes32
Red4tribe33
Redrocketboy34
Reece35
Sigma 736
Taxman37
Thenub31438
Webaware39
Xania40
http://en.wikibooks.org/wiki/User:Kwhitefoot
http://en.wikibooks.org/wiki/User:LlamaAl
http://en.wikibooks.org/wiki/User:Mike.lifeguard
http://en.wikibooks.org/wiki/User:Mts15
http://en.wikibooks.org/wiki/User:Orage
http://en.wikibooks.org/wiki/User:Panic2k4
http://en.wikibooks.org/wiki/User:Pmlineditor
http://en.wikibooks.org/wiki/User:Psoup
http://en.wikibooks.org/wiki/User:QuiteUnusual
http://en.wikibooks.org/wiki/User:Ramac
http://en.wikibooks.org/wiki/User:Recent_Runes
http://en.wikibooks.org/wiki/User:Red4tribe
http://en.wikibooks.org/wiki/User:Redrocketboy
http://en.wikibooks.org/wiki/User:Reece
http://en.wikibooks.org/wiki/User:Sigma_7
http://en.wikibooks.org/wiki/User:Taxman
http://en.wikibooks.org/wiki/User:Thenub314
http://en.wikibooks.org/wiki/User:Webaware
http://en.wikibooks.org/wiki/User:Xania
List of Figures
GFDL: Gnu Free Documentation License.
html
http://www.gnu.org/licenses/fdl.
http://
http://
http://
http://
247
List of Figures
EPL: Eclipse Public License. http://www.eclipse.org/org/documents/epl-v10.
php
Copies of the GPL, the LGPL as well as a GFDL are included in chapter Licenses41 . Please
note that images in the public domain do not require attribution. You may click on the
image numbers in the following table to open the webpage of the images in your webbrower.
41
248
List of Figures
1
2
3
4
5
6
7
8
249
38 Licenses
38.1 GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed. Preamble
The GNU General Public License is a free, copyleft license for software
and other kinds of works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your
freedom to share and change all versions of a program--to make sure
it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software;
it applies also to any other work released this way by its authors. You
can apply it to your programs, too.
When we speak of free software, we are referring to freedom, not price.
Our General Public Licenses are designed to make sure that you have
the freedom to distribute copies of free software (and charge for them
if you wish), that you receive source code or can get it if you want
it, that you can change the software or use pieces of it in new free
programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you have
certain responsibilities if you distribute copies of the software, or if you
modify it: responsibilities to respect the freedom of others.
For example, if you distribute copies of such a program, whether gratis
or for a fee, you must pass on to the recipients the same freedoms that
you received. You must make sure that they, too, receive or can get
the source code. And you must show them these terms so they know
their rights.
Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) oer you this License
giving you legal permission to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modied versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
Some devices are designed to deny users access to install or run modied versions of the software inside them, although the manufacturer
can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of
such abuse occurs in the area of products for individuals to use, which
is precisely where it is most unacceptable. Therefore, we have designed
this version of the GPL to prohibit the practice for those products. If
such problems arise substantially in other domains, we stand ready to
extend this provision to those domains in future versions of the GPL,
as needed to protect the freedom of users.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it eectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and modication follow. TERMS AND CONDITIONS 0. Denitions.
This License refers to version 3 of the GNU General Public License.
Copyright also means copyright-like laws that apply to other kinds
of works, such as semiconductor masks.
The Program refers to any copyrightable work licensed under this License. Each licensee is addressed as you. Licensees and recipients
may be individuals or organizations.
To modify a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of
an exact copy. The resulting work is called a modied version of the
earlier work or a work based on the earlier work.
A covered work means either the unmodied Program or a work
based on the Program.
To propagate a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement
under applicable copyright law, except executing it on a computer or
modifying a private copy. Propagation includes copying, distribution
(with or without modication), making available to the public, and in
some countries other activities as well.
To convey a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays Appropriate Legal Notices to
the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the
user that there is no warranty for the work (except to the extent that
warranties are provided), that licensees may convey the work under
this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a
prominent item in the list meets this criterion. 1. Source Code.
The source code for a work means the preferred form of the work for
making modications to it. Object code means any non-source form
of a work.
A Standard Interface means an interface that either is an ocial
standard dened by a recognized standards body, or, in the case of
interfaces specied for a particular programming language, one that is
widely used among developers working in that language.
The System Libraries of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A Major Component, in this context, means a major essential component
(kernel, window system, and so on) of the specic operating system (if
any) on which the executable work runs, or a compiler used to produce
the work, or an object code interpreter used to run it.
The Corresponding Source for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts
to control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodied in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface denition les associated with source les for the
work, and the source code for shared libraries and dynamically linked
subprograms that the work is specically designed to require, such as
by intimate data communication or control ow between those subprograms and other parts of the work.
The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source.
The Corresponding Source for a work in source code form is that same
work. 2. Basic Permissions.
All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly arms your unlimited permission to run the unmodied Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in
force. You may convey covered works to others for the sole purpose
of having them make modications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do not
control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary. 3. Protecting Users' Legal Rights From AntiCircumvention Law.
No covered work shall be deemed part of an eective technological
measure under any applicable law fullling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention is eected by exercising rights under this License with respect
to the covered work, and you disclaim any intention to limit operation or modication of the work as a means of enforcing, against the
work's users, your or third parties' legal rights to forbid circumvention
of technological measures. 4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms
added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of
this License along with the Program.
You may charge any price or no price for each copy that you convey, and you may oer support or warranty protection for a fee. 5.
Conveying Modied Source Versions.
You may convey a work based on the Program, or the modications
to produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
* a) The work must carry prominent notices stating that you modied
it, and giving a relevant date. * b) The work must carry prominent
notices stating that it is released under this License and any conditions
added under section 7. This requirement modies the requirement in
section 4 to keep intact all notices. * c) You must license the entire
work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any
applicable section 7 additional terms, to the whole of the work, and
all its parts, regardless of how they are packaged. This License gives
no permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it. * d) If
the work has interactive user interfaces, each must display Appropriate
Legal Notices; however, if the Program has interactive interfaces that
do not display Appropriate Legal Notices, your work need not make
them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
aggregate if the compilation and its resulting copyright are not used
to limit the access or legal rights of the compilation's users beyond
what the individual works permit. Inclusion of a covered work in an
aggregate does not cause this License to apply to the other parts of
the aggregate. 6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms of
sections 4 and 5, provided that you also convey the machine-readable
Corresponding Source under the terms of this License, in one of these
ways:
* a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source xed on a durable physical medium customarily used
for software interchange. * b) Convey the object code in, or embodied
in, a physical product (including a physical distribution medium), accompanied by a written oer, valid for at least three years and valid
for as long as you oer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the product
that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your
reasonable cost of physically performing this conveying of source, or
(2) access to copy the Corresponding Source from a network server at
no charge. * c) Convey individual copies of the object code with a
copy of the written oer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and only
if you received the object code with such an oer, in accord with subsection 6b. * d) Convey the object code by oering access from a
designated place (gratis or for a charge), and oer equivalent access to
the Corresponding Source in the same way through the same place at
no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the
object code is a network server, the Corresponding Source may be on a
dierent server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to
the object code saying where to nd the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain
obligated to ensure that it is available for as long as needed to satisfy
these requirements. * e) Convey the object code using peer-to-peer
transmission, provided you inform other peers where the object code
and Corresponding Source of the work are being oered to the general
public at no charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be included in conveying the object code work.
A User Product is either (1) a consumer product, which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer
product, doubtful cases shall be resolved in favor of coverage. For a
particular product received by a particular user, normally used refers
to a typical or common use of that class of product, regardless of the
status of the particular user or of the way in which the particular
user actually uses, or expects or is expected to use, the product. A
product is a consumer product regardless of whether the product has
substantial commercial, industrial or non-consumer uses, unless such
uses represent the only signicant mode of use of the product.
Installation Information for a User Product means any methods, procedures, authorization keys, or other information required to install
and execute modied versions of a covered work in that User Product
from a modied version of its Corresponding Source. The information
must suce to ensure that the continued functioning of the modied
object code is in no case prevented or interfered with solely because
modication has been made.
If you convey an object code work under this section in, or with, or
specically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a xed
term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by
the Installation Information. But this requirement does not apply if
neither you nor any third party retains the ability to install modied object code on the User Product (for example, the work has been
installed in ROM).
The requirement to provide Installation Information does not include
a requirement to continue to provide support service, warranty, or updates for a work that has been modied or installed by the recipient,
or for the User Product in which it has been modied or installed.
Access to a network may be denied when the modication itself materially and adversely aects the operation of the network or violates
the rules and protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying. 7. Additional Terms.
Additional permissions are terms that supplement the terms of this
License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be
treated as though they were included in this License, to the extent that
they are valid under applicable law. If additional permissions apply
only to part of the Program, that part may be used separately under
those permissions, but the entire Program remains governed by this
License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part
of it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders
of that material) supplement the terms of this License with terms:
* a) Disclaiming warranty or limiting liability dierently from the
terms of sections 15 and 16 of this License; or * b) Requiring preservation of specied reasonable legal notices or author attributions in
that material or in the Appropriate Legal Notices displayed by works
containing it; or * c) Prohibiting misrepresentation of the origin of
that material, or requiring that modied versions of such material be
marked in reasonable ways as dierent from the original version; or *
d) Limiting the use for publicity purposes of names of licensors or authors of the material; or * e) Declining to grant rights under trademark
law for use of some trade names, trademarks, or service marks; or *
f) Requiring indemnication of licensors and authors of that material
by anyone who conveys the material (or modied versions of it) with
contractual assumptions of liability to the recipient, for any liability
that these contractual assumptions directly impose on those licensors
and authors.
All other non-permissive additional terms are considered further restrictions within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction,
you may remove that term. If a license document contains a further
restriction but permits relicensing or conveying under this License, you
may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive
such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source les, a statement of the additional
terms that apply to those les, or a notice indicating where to nd the
applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions; the above
requirements apply either way. 8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your license
from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and nally terminates
your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days
after the cessation.
Moreover, your license from a particular copyright holder is reinstated
permanently if the copyright holder noties you of the violation by
some reasonable means, this is the rst time you have received notice
of violation of this License (for any work) from that copyright holder,
and you cure the violation prior to 30 days after your receipt of the
notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10. 9. Acceptance Not Required for Having
Copies.
You are not required to accept this License in order to receive or run
a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to
receive a copy likewise does not require acceptance. However, nothing
other than this License grants you permission to propagate or modify
any covered work. These actions infringe copyright if you do not accept
this License. Therefore, by modifying or propagating a covered work,
you indicate your acceptance of this License to do so. 10. Automatic
Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for
enforcing compliance by third parties with this License.
An entity transaction is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work
results from an entity transaction, each party to that transaction who
receives a copy of the work also receives whatever licenses to the work
the party's predecessor in interest had or could give under the previous
paragraph, plus a right to possession of the Corresponding Source of
the work from the predecessor in interest, if the predecessor has it or
can get it with reasonable eorts.
You may not impose any further restrictions on the exercise of the
rights granted or armed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of rights
granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any
patent claim is infringed by making, using, selling, oering for sale, or
importing the Program or any portion of it. 11. Patents.
A contributor is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's contributor version.
A contributor's essential patent claims are all patent claims owned
or controlled by the contributor, whether already acquired or hereafter
acquired, that would be infringed by some manner, permitted by this
License, of making, using, or selling its contributor version, but do
not include claims that would be infringed only as a consequence of
further modication of the contributor version. For purposes of this
denition, control includes the right to grant patent sublicenses in a
manner consistent with the requirements of this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to make,
use, sell, oer for sale, import and otherwise run, modify and propagate the contents of its contributor version.
In the following three paragraphs, a patent license is any express
agreement or commitment, however denominated, not to enforce a
patent (such as an express permission to practice a patent or covenant
not to sue for patent infringement). To grant such a patent license
to a party means to make such an agreement or commitment not to
enforce a patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through
a publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benet of the patent
license for this particular work, or (3) arrange, in a manner consistent
with the requirements of this License, to extend the patent license to
downstream recipients. Knowingly relying means you have actual
knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered
work, and grant a patent license to some of the parties receiving the
covered work authorizing them to use, propagate, modify or convey a
specic copy of the covered work, then the patent license you grant is
automatically extended to all recipients of the covered work and works
based on it.
A patent license is discriminatory if it does not include within the
scope of its coverage, prohibits the exercise of, or is conditioned on the
non-exercise of one or more of the rights that are specically granted
under this License. You may not convey a covered work if you are
a party to an arrangement with a third party that is in the business
of distributing software, under which you make payment to the third
party based on the extent of your activity of conveying the work, and
under which the third party grants, to any of the parties who would
receive the covered work from you, a discriminatory patent license (a)
in connection with copies of the covered work conveyed by you (or
copies made from those copies), or (b) primarily for and in connection
with specic products or compilations that contain the covered work,
unless you entered into that arrangement, or that patent license was
granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting any
implied license or other defenses to infringement that may otherwise
be available to you under applicable patent law. 12. No Surrender of
Others' Freedom.
If conditions are imposed on you (whether by court order, agreement
or otherwise) that contradict the conditions of this License, they do
not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations
under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to
terms that obligate you to collect a royalty for further conveying from
those to whom you convey the Program, the only way you could satisfy
both those terms and this License would be to refrain entirely from
conveying the Program. 13. Use with the GNU Aero General Public
License.
Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under
version 3 of the GNU Aero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but
the special requirements of the GNU Aero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such. 14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may dier
in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the Program
species that a certain numbered version of the GNU General Public License or any later version applies to it, you have the option of
following the terms and conditions either of that numbered version or
of any later version published by the Free Software Foundation. If
the Program does not specify a version number of the GNU General
Public License, you may choose any version ever published by the Free
Software Foundation.
If the Program species that a proxy can decide which future versions
of the GNU General Public License can be used, that proxy's public
statement of acceptance of a version permanently authorizes you to
choose that version for the Program.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM
AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK
AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. Limitation of Liability.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
The hypothetical commands `show w' and `show c' should show the
appropriate parts of the General Public License. Of course, your program's commands might be dierent; for a GUI interface, you would
use an about box.
You should also get your employer (if you work as a programmer) or
school, if any, to sign a copyright disclaimer for the program, if necessary. For more information on this, and how to apply and follow the
GNU GPL, see <http://www.gnu.org/licenses/>.
The GNU General Public License does not permit incorporating your
program into proprietary programs. If your program is a subroutine
library, you may consider it more useful to permit linking proprietary
applications with the library. If this is what you want to do, use the
GNU Lesser General Public License instead of this License. But rst,
please read <http://www.gnu.org/philosophy/why-not-lgpl.html>.
If you publish printed copies (or copies in media that commonly have
printed covers) of the Document, numbering more than 100, and the
Document's license notice requires Cover Texts, you must enclose the
copies in covers that carry, clearly and legibly, all these Cover Texts:
Front-Cover Texts on the front cover, and Back-Cover Texts on the
back cover. Both covers must also clearly and legibly identify you as
the publisher of these copies. The front cover must present the full title
with all words of the title equally prominent and visible. You may add
other material on the covers in addition. Copying with changes limited
to the covers, as long as they preserve the title of the Document and
satisfy these conditions, can be treated as verbatim copying in other
respects.
If the required texts for either cover are too voluminous to t legibly,
you should put the rst ones listed (as many as t reasonably) on the
actual cover, and continue the rest onto adjacent pages.
A "Secondary Section" is a named appendix or a front-matter section of the Document that deals exclusively with the relationship of
the publishers or authors of the Document to the Document's overall
subject (or to related matters) and contains nothing that could fall
directly within that overall subject. (Thus, if the Document is in part
a textbook of mathematics, a Secondary Section may not explain any
mathematics.) The relationship could be a matter of historical connection with the subject or with related matters, or of legal, commercial,
philosophical, ethical or political position regarding them.
It is requested, but not required, that you contact the authors of the
Document well before redistributing any large number of copies, to
give them a chance to provide you with an updated version of the
Document. 4. MODIFICATIONS
You may copy and distribute a Modied Version of the Document under the conditions of sections 2 and 3 above, provided that you release
the Modied Version under precisely this License, with the Modied
Version lling the role of the Document, thus licensing distribution
and modication of the Modied Version to whoever possesses a copy
of it. In addition, you must do these things in the Modied Version:
* A. Use in the Title Page (and on the covers, if any) a title distinct
from that of the Document, and from those of previous versions (which
should, if there were any, be listed in the History section of the Document). You may use the same title as a previous version if the original
publisher of that version gives permission. * B. List on the Title Page,
as authors, one or more persons or entities responsible for authorship
of the modications in the Modied Version, together with at least ve
of the principal authors of the Document (all of its principal authors,
if it has fewer than ve), unless they release you from this requirement. * C. State on the Title page the name of the publisher of the
Modied Version, as the publisher. * D. Preserve all the copyright
notices of the Document. * E. Add an appropriate copyright notice
for your modications adjacent to the other copyright notices. * F. Include, immediately after the copyright notices, a license notice giving
the public permission to use the Modied Version under the terms of
this License, in the form shown in the Addendum below. * G. Preserve
in that license notice the full lists of Invariant Sections and required
Cover Texts given in the Document's license notice. * H. Include an
unaltered copy of this License. * I. Preserve the section Entitled "History", Preserve its Title, and add to it an item stating at least the title,
year, new authors, and publisher of the Modied Version as given on
the Title Page. If there is no section Entitled "History" in the Document, create one stating the title, year, authors, and publisher of the
Document as given on its Title Page, then add an item describing the
Modied Version as stated in the previous sentence. * J. Preserve the
network location, if any, given in the Document for public access to a
Transparent copy of the Document, and likewise the network locations
given in the Document for previous versions it was based on. These
may be placed in the "History" section. You may omit a network
location for a work that was published at least four years before the
Document itself, or if the original publisher of the version it refers to
gives permission. * K. For any section Entitled "Acknowledgements"
or "Dedications", Preserve the Title of the section, and preserve in
the section all the substance and tone of each of the contributor acknowledgements and/or dedications given therein. * L. Preserve all
If the Modied Version includes new front-matter sections or appendices that qualify as Secondary Sections and contain no material copied
from the Document, you may at your option designate some or all of
these sections as invariant. To do this, add their titles to the list of
Invariant Sections in the Modied Version's license notice. These titles
must be distinct from any other section titles.
However, if you cease all violation of this License, then your license
from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and nally terminates
your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days
after the cessation.
You may add a section Entitled "Endorsements", provided it contains nothing but endorsements of your Modied Version by various
partiesfor example, statements of peer review or that the text has
been approved by an organization as the authoritative denition of a
standard.
You may add a passage of up to ve words as a Front-Cover Text,
and a passage of up to 25 words as a Back-Cover Text, to the end
of the list of Cover Texts in the Modied Version. Only one passage
of Front-Cover Text and one of Back-Cover Text may be added by
(or through arrangements made by) any one entity. If the Document
already includes a cover text for the same cover, previously added by
you or by arrangement made by the same entity you are acting on
behalf of, you may not add another; but you may replace the old one,
on explicit permission from the previous publisher that added the old
one.
The author(s) and publisher(s) of the Document do not by this License give permission to use their names for publicity for or to assert or imply endorsement of any Modied Version. 5. COMBINING
DOCUMENTS
You may combine the Document with other documents released under
this License, under the terms dened in section 4 above for modied
versions, provided that you include in the combination all of the Invariant Sections of all of the original documents, unmodied, and list
them all as Invariant Sections of your combined work in its license
notice, and that you preserve all their Warranty Disclaimers.
The combined work need only contain one copy of this License, and
multiple identical Invariant Sections may be replaced with a single
copy. If there are multiple Invariant Sections with the same name
but dierent contents, make the title of each such section unique by
adding at the end of it, in parentheses, the name of the original author or publisher of that section if known, or else a unique number.
Make the same adjustment to the section titles in the list of Invariant
Sections in the license notice of the combined work.
In the combination, you must combine any sections Entitled "History"
in the various original documents, forming one section Entitled "History"; likewise combine any sections Entitled "Acknowledgements",
and any sections Entitled "Dedications". You must delete all sections
Entitled "Endorsements". 6. COLLECTIONS OF DOCUMENTS
You may make a collection consisting of the Document and other documents released under this License, and replace the individual copies
of this License in the various documents with a single copy that is
included in the collection, provided that you follow the rules of this
License for verbatim copying of each of the documents in all other
respects.
You may extract a single document from such a collection, and distribute it individually under this License, provided you insert a copy
of this License into the extracted document, and follow this License
in all other respects regarding verbatim copying of that document. 7.
AGGREGATION WITH INDEPENDENT WORKS
A compilation of the Document or its derivatives with other separate
and independent documents or works, in or on a volume of a storage or
distribution medium, is called an "aggregate" if the copyright resulting
from the compilation is not used to limit the legal rights of the compilation's users beyond what the individual works permit. When the
Document is included in an aggregate, this License does not apply to
the other works in the aggregate which are not themselves derivative
works of the Document.
If the Cover Text requirement of section 3 is applicable to these copies
of the Document, then if the Document is less than one half of the
entire aggregate, the Document's Cover Texts may be placed on covers that bracket the Document within the aggregate, or the electronic
equivalent of covers if the Document is in electronic form. Otherwise
they must appear on printed covers that bracket the whole aggregate.
8. TRANSLATION
Translation is considered a kind of modication, so you may distribute
translations of the Document under the terms of section 4. Replacing
Invariant Sections with translations requires special permission from
their copyright holders, but you may include translations of some or all
Invariant Sections in addition to the original versions of these Invariant Sections. You may include a translation of this License, and all the
license notices in the Document, and any Warranty Disclaimers, provided that you also include the original English version of this License
and the original versions of those notices and disclaimers. In case of a
disagreement between the translation and the original version of this
License or a notice or disclaimer, the original version will prevail.
If a section in the Document is Entitled "Acknowledgements", "Dedications", or "History", the requirement (section 4) to Preserve its
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL. 2. Conveying
Modied Versions.
If you modify a copy of the Library, and, in your modications, a facility refers to a function or data to be supplied by an Application that
uses the facility (other than as an argument passed when the facility
is invoked), then you may convey a copy of the modied version:
* a) under this License, provided that you make a good faith eort to
ensure that, in the event an Application does not supply the function
or data, the facility still operates, and performs whatever part of its
purpose remains meaningful, or * b) under the GNU GPL, with none
of the additional permissions of this License applicable to that copy.
You may convey a Combined Work under terms of your choice that,
taken together, eectively do not restrict modication of the portions
of the Library contained in the Combined Work and reverse engineering for debugging such modications, if you also do each of the following:
You may place library facilities that are a work based on the Library
side by side in a single library together with other library facilities that
are not Applications and are not covered by this License, and convey
such a combined library under terms of your choice, if you do both of
the following:
5. Combined Libraries.