Documente Academic
Documente Profesional
Documente Cultură
PD PD
1/5/2010 Visual Basic 6 Code Editor and control s…
!
W
W
O
O
N
N
y
y
bu
bu
to
to
k
k
lic
lic
C
C
w
w
m
m
w w
w
w
o
o
.d o .c .d o .c
c u -tr a c k c u -tr a c k
Getting help
There is a lot of doc umentation available on VB. There is so
much, in fact, that it's easy to get lost in it. However, the on-
line Help available from the Menu should be used regularly. Very
often just doing a searc h on a word in partic ular will be
sufficient to get you out of a jam. If you want to go into more
detail c hec k out the Contents part of MSDN (Microsoft
Developers' Network) and surf through it.
Writing code
VB is not very particular about presentation - spac es, indents,
lower case or upper c ase, it doesn't make too muc h differenc e
to the c ompiler. But it may make a whole lot of differenc e to
the programmer who has to maintain your code in 2 years, after
you've moved up to President.
http://www.profsr.com/vb/vbless02.htm 1/9
F -X C h a n ge F -X C h a n ge
PD PD
1/5/2010 Visual Basic 6 Code Editor and control s…
!
W
W
O
O
N
N
y
y
bu
bu
to
to
k
k
lic
lic
Apply "Best Programming
C
C
w
w
m
m
w w
w
w
o
o
.d o .c .d o .c
c u -tr a c k c u -tr a c k
Practices"
When you work with RAD
(Rapid Application
Development) tools like VB in
a graphical interface
environment, you become
more than just a programmer,
a writer of c ode. You are a
developer. We will cover that
in the next lesson.
If you are just starting out with the language, why not pick up a few good
habits right now and it may make your life a lot easier down the road.
http://www.profsr.com/vb/vbless02.htm 2/9
F -X C h a n ge F -X C h a n ge
PD PD
1/5/2010 Visual Basic 6 Code Editor and control s…
!
W
W
O
O
1. Use comments when appropriate but not so many as to overwhelm the
N
N
y
y
bu
bu
c ode; the apostrophe ' is the c omment identifier; it c an be at the
to
to
k
k
lic
lic
beginning of a line or after the code.
C
C
w
w
m
m
w w
w
w
o
o
.d o .c .d o .c
c u -tr a c k c u -tr a c k
2. Use indents - c ode must be indented under c ont rol struc tures suc h as If
... Then or Sub - it makes it so much easier to follow the logic.
FOR i = 1 TO 5
value(i) = 0 ' Indent used in control
structures
NEXT i
3. Use standard capitalization - keywords like If, Dim, Option, Private start
with a capital letter with the rest in lower case; variable names, control
names, etc . are usually mixed c ase: ClientName, StudentId, etc .
Data1.RecordSource = _
"Select * From Titles" ' One statement on 2
lines is OK
Naming conventions
These are the rules to follow when naming elements in VB - variables,
constants, controls, procedures, and so on:
May be as muc h as 255 charac ters long (but don't forget that somedy
has to type the stuff!).
Must not be a reserved word (that is part of the code, like Option, for
example).
The dash, alt hough legal, should be avoided bec ause it may be c onfused
with the minus sign. Instead of Family-name use Family_name or
FamilyName.
Data types
!
W
W
O
O
N
N
y Long (long
y
bu
bu
4 bytes -2,147,483,648 to 2,147,483,647
to
to
integer)
k
k
lic
lic
C
C
w
w
m
m
w w
w
w
o
o
.d o .c .d o .c
c u -tr a c k c u -tr a c k
Single 4 bytes
(single- -3.402823E38 to -1.401298E-45 for
precision negative values; 1.401298E-45 to
floating- 3.402823E38 for positive values
point)
Double 8 bytes -1.79769313486232E308 to -
(double- 4.94065645841247E-324 for negative
precision values; 4.94065645841247E-324 to
floating- 1.79769313486232E308 for positive
point) values
Currency
-922,337,203,685,477.5808 to
(scaled 8 bytes
922,337,203,685,477.5807
integer)
Decimal 14 bytes +/-
79,228,162,514,264,337,593,543,950,335
with no decimal point; +/-
7.9228162514264337593543950335 with
28 places to the right of the decimal;
smallest non-zero number is +/-
0.0000000000000000000000000001
Date 8 bytes January 1, 100 to December 31, 9999
Object 4 bytes Any Object reference
String 10 bytes
(variable- + string 0 to approximately 2 billion
length) length
String
Length
(fixed- 1 to approximately 65,400
of string
length)
Variant
Any numeric value up to the range of a
(with 16 bytes
Double
numbers)
Variant 22 bytes
(with + string Same range as for variable-length String
characters) length
User- Number
defined required The range of each element is the same
(using by as the range of its data type.
Type) elements
In all probability, in 90% of your applic ations you will use at most six types:
String, Integer, Long, Single, Boolean and Date. The Variant type is often used
automatically when type is not important. A Variant-type field can contain text
or numbers, depending on the data that is ac tually entered. It is flexible but it
is not very efficient in terms of storage.
Top
Declaring variables
Dec laring a variable means giving it a name, a data type and sometimes an
initial value. The declaration can be explicit or implicit.
http://www.profsr.com/vb/vbless02.htm 4/9
F -X C h a n ge F -X C h a n ge
PD PD
1/5/2010 Visual Basic 6 Code Editor and control s…
!
W
W
O
O
An explicit declaration: variable is declared in the Declarations Section or at
N
N
y
y
bu
bu
the beginning of a Procedure. An explicit declaration looks like:
to
to
k
k
lic
lic
Dim MyNumber As Integer
C
C
w
w
m
m
w w
w
w
o
o
.d o .c .d o .c
c u -tr a c k c u -tr a c k
Now the variable MyNumber exists and a 2-byte space has been reserved for
it.
An implicit declaration: the variable is dec lared "on the fly", its data type is
deduced from other variables. For example:
Dim Total1 As Integer 'Explicit declaration
Dim Total2 As Integer 'Explicit declaration
Total3 = Total1 + Total2 'Implicit declaration
Total3 is not formally declared but is implied, it is "arrived at" from the other
dec larations.
It is never a good idea to have implic it dec larations. It goes against the rules
for clarity, readability and ease of use of the code.
To make sure that t his rule is followed, start the Dec larations with the Option
Explicit clause. This tells the compiler to consider implicit declarations as errors
and forc es the programmer to declare everything explic itly.
In the last example the type assigned to each variable will be: Variant. It is the
default type when none is specified.
In this final example, what are the types assigned to the three variables:
Dim Amount1, Amount2, Amount3 As Single
All Single-prec ision floating point, you say. Wrong! Only Amount3 is Single.
Amount1 and Amount2 are considered Variant because VB specifies that each
variable in a statement must be explicitly declared. Thus Amount1 and Amount2
take the default data type. This is different from what most other languages
do.
Constants
A constant is a value that does not change during the exec ution of a
procedure. The constant is defined with:
Const ValuePi = 3.1416
!
W
W
O
O
N
N
y
y
bu
bu
Private Sub Command1_Click ()
to
to
k
k
lic
lic
Dim Total1 As Integer
C
C
w
w
m
m
w w
w
w
o
o
.d o .c .d o .c
c u -tr a c k c u -tr a c k
Static Total3 As Integer
Total1 = Total1 + 1
Total2 = Total2 + 1
Total3 = Total3 + 1
End Sub
Every time Button1 is clicked, Total1 is declared as a new variable during the
execution of that c licked event. It is a procedure-level variable. It will always
stay at 1. The same for the Button2 event: Total1 is a new variable in that
procedure. When the procedure ends, Total1 disappears.
Total2 is dec lared in the Dec larations section. It is a module-level variable,
meaning it is available to every control in this Form. When Button1 is clicked, it
inc rements by 1 and it retains that value. When Button2 is c lic ked, Total2 is
incremented from its previous value, even if it came from the Button1 event.
Total3 shows another way of retaining the value of a loc al variable. By
declaring it with Static instead of Dim, the variable ac ts like a module-level
variable, although it is declared in a procedure.
Another scope indicator that you will see when you study examples of code is
Private and Public. This determines whether a proc edure is available only in
this Form (module) or if it is available to any module in the application. For now,
we will work only with Private procedures.
Top
Operators
http://www.profsr.com/vb/vbless02.htm 6/9
F -X C h a n ge F -X C h a n ge
PD PD
1/5/2010 Visual Basic 6 Code Editor and control s…
!
W
W
O
O
Logical operators
N
N
y
y
bu
bu
to
to
k
k
lic
lic
C
C
w
w
m
m
Operator Definition Example Result
w w
w
w
o
o
.d o .c .d o .c
c u -tr a c k c u -tr a c k
= Equal to 9 = 11 False
> Greater than 11 > 9 True
< Less than 11 < 9 False
>= Greater or equal 15 >= 15 True
<= Less or equal 9 <= 15 True
<> Not equal 9 <> 9 False
AND Logical AND (9 = 9) AND (7 = 6) False
OR Logical OR (9 = 9) OR (7 = 6) True
Top
Control Structures
If...Then...Else
If condition1 Then
statements1
Else
statements2
End If
If condition1 Then
statements1
End If
Select Case
!
W
W
O
O
Case Else
N
N
y
y
bu
bu
SizeName.Caption = "Unknown size"
to
to
k
k
lic
lic
End Select
C
C
w
w
m
m
w w
w
w
o
o
.d o .c .d o .c
c u -tr a c k c u -tr a c k
Do...Loop
Do While condition
statements
Loop
For...Next
For additional resources you might find this site useful: Free Visual Basic 6
tutorials and source code examples .
Assignment
To practise your coding and editing skills, try modifying the Football example by
adapting it for different sports. For example, in American football, which is
similar to rugby, there are 4 different ways to score, as shown here:
Touchdown 6 points
Field goal 3 points
2-point Convert or Safety 2 points
http://www.profsr.com/vb/vbless02.htm 8/9
F -X C h a n ge F -X C h a n ge
PD PD
1/5/2010 Visual Basic 6 Code Editor and control s…
!
W
W
O
O
N
Convert 1 point
N
y
y
bu
bu
to
to
k
k
lic
lic
C
C
w
w
m
m
w w
w
w
o
o
.d o .c .d o .c
c u -tr a c k c u -tr a c k
If you haven't found the Visual Basic resource you're looking for,
use our Google Search box for more information.
Search
Top
http://www.profsr.com/vb/vbless02.htm 9/9