Sie sind auf Seite 1von 31

Visual Basic 6

Programming.
Lecture 1 : January 2005
Dr. Andrew Paul Myers

10/04/2019 1
Course Methods.
 Lectures.
 Handout notes.
 Supervised practical work.
 Weekly submission of exercises.
 Weekly feedback on exercises.
 Model Answers.
 Final project : 12.00 Wed. 19th May 2004.
 Clinics for final Project.
10/04/2019 2
Introduction.
 A course in Visual Basic 6.
 Why Visual Basic 6 (VB6)?
 Objectives :
• General education in computing.
• Introduction to programming.
• First write a simple program, then…
• Write a more complex program.
 Course Benefits :
• I.T. is a part of all our lives and getting more so!
• VB6 and computing skills for other courses.
• Future employment.

10/04/2019 3
Tools For The Job.
 Programming Environment :

 CFS client PC.


 Windows 2000.
 Microsoft Visual Studio 6 SP5.
 Computers in G73 only at present!

10/04/2019 4
Programming
Languages.
 You are users!
 How computers work.
 Machine code.
 High level languages.
 BASIC : Beginners All purpose Symbolic
Instruction Code.
 Other languages :
• FORTRAN, C/C++ & Java

10/04/2019 5
Programming Cycle.
 Analyse the task.
 Plan the program, a structured approach!
 Flowcharts & Pseudo-code (Week 2).
 Edit your source code.
 Execute and debug program (Week 2)
 Edit source code as necessary.

10/04/2019 6
Setting up VB6 on CFS.
VB6 is part of MS Visual Studio 6.
Select :
Start Menu
Programs
Installable Software
Then navigate to :
Central Software
Programming Languages
Double click on :
Install MS Visual Studio V6 SP5

10/04/2019 7
VB6 Help : MSDN.
 Visual Studio 6 has an extensive, inbuilt
help system call MSDN. MicroSoft
Developer Network.

 Before this can be used select Setup Help


for this session in the MS VS6 folder on
the Programs menu.

Only run this once per login session.

10/04/2019 8
Starting VB6.
 After setup a new folder is added to your
programs menu.

 Select Microsoft Visual Basic 6 from the MS


Visual Studio V6 SP5 folder.

 Tip : Create a short cut on your desktop.

 This will start the VB6 IDE.


Integrated Programming Environment.
Select Standard EXE as project type.
10/04/2019 9
The VB IDE.
Has Standard MicroSoft “feel”.
 Title Bar. Status : Design, run, debug.
 Menu Bar. Pull down menus.
 Tool Bar.
 Tool Box. For adding controls.
 Project Explorer. List project modules.
 Properties Window.
 Form Layout.
10/04/2019 10
On we go…

10/04/2019 11
Structure of VB
Program.
VB programs are made up of different
subroutines (or procedures) of the form:

Private Sub <name>()


Comment statement(s)
Declaration statement(s)
BASIC statement(s)
End Sub
10/04/2019 12
VB Statements.
 Comments : Used to document programs
and make them more readable. USE
THEM !!!

‘ A Comment!

 Terminate a program.

End
10/04/2019 13
Computers store and add
numbers (very fast).
Computer memory is divided into separate sections.
Each is identified by a number (its address).

Alan Bruce Claire Dan Ethel ……..


A computer language allows us to identify these by names.
Then to manipulate the numbers in them.

Alan = Bruce + Claire


An assignment statement.
10/04/2019 14
Bits, bytes and nibbles (its half a byte!).
8 bits make a byte

1 1 1 0 1 0 1 1 1 1 1 0 1 0 1 1

Each bit can be set to 1 or 0, so within one byte (8 bits) we can


represent numbers in the range 0 to 255 i.e. 2^8.
Combining multiple bytes, we can store numbers:

16 bits = 2 bytes stores 2^16 = 0 to 65535


32 bits = 4 bytes stores 2^32 = 0 to 4294967295

These are whole numbers (integers), no decimals allowed.


We are restricted to numbers +/- 2 billion.
10/04/2019 15
Different Forms of Storage.
 A large number on a calculator is represented in exponential (also
called scientific) format e.g. 1.23456E+23.
 If we take 4 bytes of memory, and store the mantissa (1.23456) in 3
bytes, and the exponent (+23) in one byte, we can store real
numbers, with a greater range.
 Represent letters by a numerical code from 0 – 255 (ASCII) and store
them in one byte. Text can then be stored as a linked set of bytes,
called a String.

0 1 1 0 1 0 1 1 0 0 1 0 1 0 1 1

ASCII 107 is k ASCII 43 is +


10/04/2019 16
Types of Variables.
 Integers (whole numbers)
 Real numbers.
 Single.
 Double (twice the number of bytes).

 Strings.
 And others coming later…
 Variants.
 All types automatically converting.
 DANGEROUS! – Don’t go there!

10/04/2019 17
Variable Names.
Data is stored as variables, each with a
different name. Variable names are:

 Case insensitive.
 Up to 255 characters long.
 First character must be a letter.
 Can use any combination of alphanumeric
characters and the underscore character.

10/04/2019 18
Examples.
Value yes
3radius no
Question&Reply no
Density_of_water yes
lngRadius1 yes

10/04/2019 19
Hungarian Notation.
A convention of prefixes for variable
names, depending on variable type.

String : str Integer : int


Long Integer : lng Single : sng
Double : dbl Boolean : bln
Currency : cur Variant : vnt

10/04/2019 20
Variable Types I
VB6 has 14 standard types.

 String : Hold characters, approx 231, can


be null (empty). Can be identified by $.

strText$=“Hello.”
strEmptyText = “”

10/04/2019 21
Variable Types II.
 Integer : Range –32,768 to +32,767. Can
be identified by %.
intValue% = 1
intValue2 = 1000

 Long Integer : Larger range (231).


lngValue1& = 56000
lngValue2 = -56000
10/04/2019 22
Variable Types III.
 Single Precision : Real numbers. Accuracy of 7
digits.
sngReal_Number1! = 1.23
sngReal_Number2 = -0.345

 Double Precision : Accuracy of 16 digits


dblValue1# = 0.435827348593
dblValue2 = 23.4782947373

Reals are slower than integers in calculations!

10/04/2019 23
Variable Types IV.
 Boolean : True or False?

blnAnswer = False

 Other types include : Currency, Date, Byte


(range 0 to 255) and variant (can be any
data type – very flexible, but dangerous!).

10/04/2019 24
Variable Type Declarations I
Declare all variables before use! Good programming practice.
Also avoids having to use the suffixes $, #, ! etc.

Use the Dim statement. e.g.

Dim <variable> as <data type>

Dim intDays as Integer


Dim strText as String, dblAns as Double

10/04/2019 25
Variable Type Declarations II
Declaring all variables before use is good programming
practice. Use the “Option Explicit” command to make VB insist
of variables being declared before you can use them.

Option Explicit

This also avoids having to use the suffixes $, #, ! etc, as all


variable types are explicitly defined.

Note : This check is carried out when the program is run, not at
the editing stage!
10/04/2019 26
Assignments I.
 5 basic mathematical operators:
Addition (+) Subtraction (-)
Division(/) Multiplication (*)
Exponentiation (^)
 Precedence follows BODMAS.
 Parentheses can be used to over ride
precedence.
6*5+4*3 gives 42
(((6 * 5) + 4) * 3) gives 102

10/04/2019 27
Assignments II.
<variable> = <value> | <variable> | <expression>

Examples :

intValue = 1
strText2 = strText1
intValue = intValue + 1
sngVol = sngLength^3.0
strText = “Hello”
dblVol= (4#/3#)*dblPi*dblRadius^3

10/04/2019 28
Print Statements.
Print <expression>

Print “Hello!”
Print “Value is “; intValue
Print “A : “; intA; “ cm.”
Print dblRadius

We shall be using “Text Boxes”!


10/04/2019 29
Good Programming
Style.

 Comment your program!


 Hungarian notation for variable names.
 Use descriptive variable names.
 Blanks lines may be used to improve
readability.
 Indent code with “tabs”.

10/04/2019 30
References.
Visual BASIC 6 from the ground up.
Gary Cornell.
(Osborne).

MSDN On-line Help.


Active subsection :
Visual Basic Documentation.

10/04/2019 31

Das könnte Ihnen auch gefallen