Presentation is loading. Please wait.

Presentation is loading. Please wait.

Visual Basic 6 Programming.

Similar presentations


Presentation on theme: "Visual Basic 6 Programming."— Presentation transcript:

1 Visual Basic 6 Programming.
Lecture 4 : February 2004 Dr. Andrew Paul Myers 29/12/2018

2 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) 29/12/2018

3 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!! 29/12/2018

4 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() 29/12/2018

5 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 29/12/2018

6 Use of Function 2. Dim dlbValue As Double dblValue = Radians(90#)
dblValue = Sin(Radians(90#)) 29/12/2018

7 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 29/12/2018

8 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) 29/12/2018

9 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 29/12/2018

10 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! 29/12/2018

11 Subroutines. General Form: To execute:
Public Sub <name>( <parameters> ) <statement(s)> End Sub To execute: Call <name>( <parameters> ) 29/12/2018

12 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. 29/12/2018

13 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> 29/12/2018

14 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 29/12/2018

15 Using Multiple Forms. e.g. Form2.TextBox3.Text = “Hello”
dblValue = Form2.Pi() * 180# Form2.Show Form2.Hide 29/12/2018

16 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 29/12/2018


Download ppt "Visual Basic 6 Programming."

Similar presentations


Ads by Google