VISUAL BASIC PROGRAMMING AN INTRODUCTION
Why Visual Basic?
H Programming for the Windows User Interface is extremely complicated.
H Other Graphical User Interfaces (GUI) are no better.
H Visual Basic provides a convenient method for building user interfaces.
H Visual Basic can interface with code written in C, for efficiency.
What Visual Basic is not
H Visual Basic is not, a powerful programming language that enables you to
do anything you want.
H Visual Basic is not, elegant or fast.
H Visual Basic is not, a replacement for C.
H Visual Basic is not, anything like any other programming language you
have ever used
When You Program in VB:
H You draw pictures of your user interface.
H You draw buttons, text boxes, and other user-interface items.
H You add little snippets of code to handle the user interaction.
H You add initialization code, usually as the last step.
H If you like, you can code more complex functions. (But many do not.)
The Visual Basic Interface
Drawing The Program
Types of Controls
More Complex Controls
H Complex Controls Have:
o Action Properties to Execute Commands
o Active Properties that Cause Actions When Values Are Assigned to
Them
o Many Types of Events for Program Interaction
H Examples:
o Spreadsheets
o Word Processors
o Web Browsers
Using C Code
H Write a DLL in C
H Use the _export Property on Appropriate Functions
H Write Visual Basic Definitions for each Function
H Add VB Definitions to The (general) section of the VB Program
H Use Functions as if they were VB functions
C Definition vs. VB Definition
C:
Long FAR PASCAAL_export HexToLong (char*Hex)
VB:
Declare Dunction HexToLong Lib “[Link]” (ByVal Instring As String)
As Long
Function name Must Be The Same in Both Decalrations. The Lib keyword Must
Give The Name of the Library, Argument Name in VB is arbitrary.
A (Very Annoying) Problem
H It is sometimes difficult for VB to FIND the .DLL file.
H If this occurs, copy the .DLL file to the WINDOWS directory.
H Remember to Delete the file when you are done.
Alternative Methods
H Some Versions of VB do not allow DLL function definitions in the
(general) section of a form.
H To Get Around this Problem, Create a new Module (File Menu)
H Add the declarations to the (general) section of the module
H You can add your own VB functions to the (general) section of a form or
a module.
Syntax Considerations
H All Functions are Global in VB
H Variables are declared using the syntax:
o Dim <Name> As <Type>
o Every variable must have a type
o Dim A,B,C As <Type> will work, but gives weird results
H Most Common Types: Integer, String, Long
More VB Syntax
H Use Integers for Booleans
o As in C, 0 = False, everything else = True
o Symbolic constants True and False may be used
o True = -1, False = 0
H Assignments are the same as in C
H The Val function converts strings to integers
H The Format$ function converts integers to strings
VB Statements
H Assignments are the Same as in C
H Case is not significant
o Case will be adjusted for you on keywords
o For Variable Names, Case is ignored
H The Usual Operators can be used
o AND is the same as both & and && depending on context
o OR = | and ||
o NOT = !
VB IF Statements
If <condition> Then If <condition> Then
<List of Statements> <List of Statements>
Else EndIf
<List of Statements>
EndIf
Comparators: +, <, >, <=, >=, <> (not equal)
Connectives: And, Or, Not
DON’T FORGET THE ENDIF!
VB While Statements
While <condition> do
The VB Manual Recommends a different structure. Use
<List of Statements>
the alternative if you wish.
Wend
VB For Statements
For <Variable> = <start> to <finish>
<List of statements>
Next <Variable>
For <Variable> = <start> to <finish> Step <increment>
<List of Statements>
Next <Variable>
Example:
For 1 = 1 to 10 do
A[1] = A[1] + 1
Next 1
VB Arrays
H Indices Always Start With Zero
H Dim A[10] As Integer Declares 11 elements, indexed from 0 through 10.
H Multi-Dimensional Arrays are Permitted.
H Arrays can be resized at run time (See VB Help File for ReDim)
VB Strings
H Variable Length
H Compare using standard comparators
H Maximum length is about 64Kb
H Minimum length is zero
H Allocated from VB “String Space”, so may run out of space even on
systems with much memory.
And in Conclusion…. Have Fun!