Visual Basic 6 Programming Lecture 4 February 2004
Visual Basic 6 Programming. Lecture 4 : February 2004 Dr. Andrew Paul Myers 2/4/2022 1
Functions return a value. So far you have been using inbuilt VB functions, e. g. sng. Value = Sin(sng. X_in_rads) dbl. Pi = 4# * Atn(1#) VB allows you to write your own! i. e. dbl. Rad = Radians(dbl. Degrees) 2/4/2022 2
Writing Functions. General Form: Private Function <name>( <parameters> ) As <type> <statement(s)> <name> = <return value> End Function Note : Functions can be Public, i. e. across all forms!! 2/4/2022 3
Example Function 1. Private Function Pi() As Double ‘ A parameterless function to return Pi. Pi = 4# * Atn(1#) End Function ************ ‘ Use of this function. dlb. Pi = Pi() 2/4/2022 4
Example Function 2. Public Function Radians(dlb. Degrees As Double) _ As Double ‘ Function to convert Degrees to Radians. Dim dbl. Pi As Double ‘ A local variable. dlb. Pi=Pi() ‘ Call Pi function. Radians = dbl. Pi * dbl. Degrees / 180# End Function 2/4/2022 5
Use of Function 2. Dim dlb. Value As Double dbl. Value = Radians(90#) dbl. Value = Sin(Radians(90#)) 2/4/2022 6
Example Function 3. Public Function Add(int. X As Integer, int. Y As _ Integer) As Integer ‘ Function to add two numbers together. Add = int. X + int. Y End Function 2/4/2022 7
Use of Function 3. Dim int. Result As Integer Dim int. First As Integer, int. Second As Integer int. Result = Add(1, 2) ‘ Not an array!!! int. Result = Add(int. First, int. Second) 2/4/2022 8
Arrays as Parameters. Private Function Sum(int. Values() As Integer Dim int. Loop As Integer, int. Total As Integer ‘ local scope int. Total = 0 For int. Loop = LBound(int. Values) To UBound(int. Values) int. Total = int. Total + int. Values(int. Loop) Next int. Loop Sum = int. Total End Function 2/4/2022 9
Subroutines or Procedures. Functions return a value. Best suited to calculations. Subroutines/procedures do not, but they can modify the parameters (arguments) passed if required. Hence “returning” more than one value. The “Building Blocks” of a program! 2/4/2022 10
Subroutines. General Form: Public Sub <name>( <parameters> ) <statement(s)> End Sub To execute: Call <name>( <parameters> ) 2/4/2022 11
Writing Subroutines. Public Sub change_text(str. Text As String) str. Text = “It’s been changed!” End Sub To call: Call change_text(str. Some_Text) Again Subs can be Public or Private. 2/4/2022 12
Multiple Forms. Projects can contain more than one Form. Use the “Add Form” option under the “Project” pull down menu. Addressing Object/Control Properties: <Form>. <Object>. <Property> = <Value> 2/4/2022 13
Displaying Multiple Forms. <Form Name>. Show <Form Name>. Hide To speed up Form display times use the Load command in the subroutine Form_Load() on the first Form. e. g. Private Sub Form_Load() Load Form 2 Load Form 3 End Sub 2/4/2022 14
Using Multiple Forms. e. g. Form 2. Text. Box 3. Text = “Hello” dbl. Value = Form 2. Pi() * 180# Form 2. Show Form 2. Hide 2/4/2022 15
Exiting a Multiple Form Program. Private Sub Form_Query. Unload(Cancel _ As Integer, Unload. Mode As Integer) Dim AForm As Form For Each AForm In Forms Unload AForm Next End 2/4/2022 End Sub 16
- Slides: 16