Download presentation
Presentation is loading. Please wait.
1
Visual Basic 6 Programming.
Lecture 3 : February 2004 Dr. Andrew Paul Myers 01/12/2018
2
Scope of Variables. So far variables have been local to procedures (subroutines). Could have the same names, but different values. We can have : Form level variables. Declare in “Declarations – General”, i.e with Option Explicit statement. 01/12/2018
3
Scope of Variables. Option Explicit Private intValue as Integer
Public sngRadius as Single Private Sub Form_Load() ‘ Some VB statements! End Sub Private : Form level variable Public : All forms. 01/12/2018
4
Doing it again & again. So far…
The order VB statements are executed inside a subroutine can be changed using conditional statements. VB statements can be repeated by using loops (there are several types). 01/12/2018
5
For… Next Loop. General Form: e.g.
For <counter> = <start> To <stop> Step <increment> <statement(s)> Next <counter> e.g. For intLoop = 0 To 10 Step 1 MyTextBox.SelText = Str(intLoop) & vbCrLf Next intLoop 01/12/2018
6
Nested Loops. Dim intLoop As Integer Dim sngX As Single
For intLoop = 1 To 10 For sngX = 0# to 10# Step 0.1 <Statement(s)> Next sngX Next intLoop 01/12/2018
7
Indeterminate Loops. These do not execute a fixed number of times!
General Form: Do <statement(s)> Loop Until <condition(s)> 01/12/2018
8
Do... Until Loop. Dim strPassWord As String Do
strPassWord = _ InputBox(“Password?”) Loop Until strPassWord = “123” 01/12/2018
9
More Error Checking. <statement(s)> Do
strX = InputBox(“Enter a +ve no.”) If (IsNumeric(strX) = False) Then strX = “-1” Loop Until ( Val(strX) > 0 ) 01/12/2018
10
More Loops. e.g. Do <statements(s)>
Loop While <condition(s)> e.g. <statement(s)> Loop While ( dblValue < 0# ) 01/12/2018
11
Yet More Loops… Do While <condition> <statement(s)> Loop
e.g. Dim blnProg_Finished As Boolean blnProg_Finished = False Do While (blnProg_Finished = False) 01/12/2018
12
Arrays. An array is a simple structure, capable of storing many variables of the same type in a single data structure. Declare arrays with other variables. Arrays can be of any data type. Scope can be global to a form or local to a procedure, but not global to all forms! 01/12/2018
13
Declaring Arrays. Dim sngValue(6) As Single
This sets up 7 single precision variables called : sngValue(0) sngValue(1) sngValue(2) sngValue(3) sngValue(4) sngValue(5) sngValue(6) Each identified by a unique subscript in brackets. 01/12/2018
14
Using Arrays. After declaration, they can be used in the same way as other variables, but remember the subscript!!! e.g. Dim intArray(20) As Integer intArray(1) = 1 intArray(2) = 2 intArray(3) = 3 * intX 01/12/2018
15
Array Storage. intX(0) intX(1) intX(2) intX(3) intX(4)
Arrays can be thought of thus: e.g. If we have a 5 element array. Dim intX(4) intX(0) = 21 intX(1) = 22 intX(2) = 23 etc… intX(0) intX(1) intX(2) intX(3) intX(4) 01/12/2018
16
Using Arrays. Dim intLoop As Integer Dim intArray(20) As Integer
For intLoop = 0 To 20 intArray(intLoop) = 0 Next intLoop 01/12/2018
17
Using Arrays. Dim intLoop As Integer
Dim dblArray(360) As Double, dblPi As Double dblPi = 4# * Atn(1#) For intLoop = 0 To 360 dblArray(intLoop) = Sin(intLoop * dblPi / 180#) Next dblLoop 01/12/2018
18
More on Arrays. Arrays bounds can be user defined:
Dim strName( 5 To 10 ) As String Dim sngX( -25 To 25 ) As Single intValue = LBound(sngX) gives -25 intValue = UBound(sngX) gives 25 01/12/2018
19
Scope of Arrays. Local : Private Sub Form_Load()
Dim strName(20) As String End Sub Global : In “General Declarations”. Option Explicit Private strName(20) As String Note : You can not share arrays across forms! 01/12/2018
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.