1 / 19

Visual Basic 6 Programming.

Visual Basic 6 Programming. Lecture 3 : February 2004 Dr. Andrew Paul Myers. 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

leisenhauer
Download Presentation

Visual Basic 6 Programming.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Visual Basic 6 Programming. Lecture 3 : February 2004 Dr. Andrew Paul Myers

  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.

  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.

  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).

  5. For… Next Loop. General Form: 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

  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

  7. Indeterminate Loops. These do not execute a fixed number of times! General Form: Do <statement(s)> Loop Until <condition(s)>

  8. Do... Until Loop. Dim strPassWord As String Do strPassWord = _ InputBox(“Password?”) Loop Until strPassWord = “123”

  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 ) <statement(s)>

  10. More Loops. Do <statements(s)> Loop While <condition(s)> e.g. Do <statement(s)> Loop While ( dblValue < 0# )

  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) <statement(s)> Loop

  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!

  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.

  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 * 3 + 2 + intX

  15. Array Storage. 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) 21 22 23 24 25

  16. Using Arrays. Dim intLoop As Integer Dim intArray(20) As Integer For intLoop = 0 To 20 intArray(intLoop) = 0 Next intLoop

  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

  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

  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!

More Related