170 likes | 270 Views
Visual Basic 6 Programming. Lecture 4 : February 2004 Dr. Andrew Paul Myers. Functions. Functions return a value. So far you have been using inbuilt VB functions, e.g. sngValue = Sin(sngX_in_rads) dblPi = 4# * Atn(1#) VB allows you to write your own! i.e. dblRad = Radians(dblDegrees).
E N D
Visual Basic 6 Programming. Lecture 4 : February 2004 Dr. Andrew Paul Myers
Functions. Functions return a value. So far you have been using inbuilt VB functions, e.g. sngValue = Sin(sngX_in_rads) dblPi = 4# * Atn(1#) VB allows you to write your own! i.e. dblRad = Radians(dblDegrees)
Writing Functions. General Form: Private Function <name>( <parameters> ) As <type> <statement(s)> <name> = <return value> End Function Note : Functions can bePublic, i.e. across all forms!!
Example Function 1. Private Function Pi() As Double ‘ A parameterless function to return Pi. Pi = 4# * Atn(1#) End Function ************************ ‘ Use of this function. dlbPi = Pi()
Example Function 2. Public Function Radians(dlbDegrees As Double) _ As Double ‘ Function to convert Degrees to Radians. Dim dblPi As Double ‘ A local variable. dlbPi=Pi() ‘ Call Pi function. Radians = dblPi * dblDegrees / 180# End Function
Use of Function 2. Dim dlbValue As Double dblValue = Radians(90#) dblValue = Sin(Radians(90#))
Example Function 3. Public Function Add(intX As Integer, intY As _ Integer) As Integer ‘ Function to add two numbers together. Add = intX + intY End Function
Use of Function 3. Dim intResult As Integer Dim intFirst As Integer, intSecond As Integer intResult = Add(1, 2) ‘ Not an array!!! intResult = Add(intFirst, intSecond)
Arrays as Parameters. Private Function Sum(intValues() As Integer) As Integer Dim intLoop As Integer, intTotal As Integer ‘ local scope intTotal = 0 For intLoop = LBound(intValues) To UBound(intValues) intTotal = intTotal + intValues(intLoop) Next intLoop Sum = intTotal End Function
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!
Subroutines. General Form: Public Sub <name>( <parameters> ) <statement(s)> End Sub To execute: Call <name>( <parameters> )
Writing Subroutines. Public Sub change_text(strText As String) strText = “It’s been changed!” End Sub To call: Call change_text(strSome_Text) Again Subs can be Public or Private.
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>
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 Form2 Load Form3 End Sub
Using Multiple Forms. e.g. Form2.TextBox3.Text = “Hello” dblValue = Form2.Pi() * 180# Form2.Show Form2.Hide
Exiting a Multiple Form Program. Private Sub Form_QueryUnload(Cancel _ As Integer, UnloadMode As Integer) Dim AForm As Form For Each AForm In Forms Unload AForm Next End End Sub