visual basic 6 programming

16
24/08/22 1 Visual Basic 6 Programming. Lecture 4 : February 2004 Dr. Andrew Paul Myers

Upload: kareem-noble

Post on 31-Dec-2015

24 views

Category:

Documents


2 download

DESCRIPTION

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). - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Visual Basic 6 Programming

19/04/23 1

Visual Basic 6 Programming.

Lecture 4 : February 2004

Dr. Andrew Paul Myers

Page 2: Visual Basic 6 Programming

19/04/23 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)

Page 3: Visual Basic 6 Programming

19/04/23 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!!

Page 4: Visual Basic 6 Programming

19/04/23 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()

Page 5: Visual Basic 6 Programming

19/04/23 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

Page 6: Visual Basic 6 Programming

19/04/23 6

Use of Function 2.

Dim dlbValue As Double

dblValue = Radians(90#)

dblValue = Sin(Radians(90#))

Page 7: Visual Basic 6 Programming

19/04/23 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

Page 8: Visual Basic 6 Programming

19/04/23 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)

Page 9: Visual Basic 6 Programming

19/04/23 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

Page 10: Visual Basic 6 Programming

19/04/23 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!

Page 11: Visual Basic 6 Programming

19/04/23 11

Subroutines.

General Form:

Public Sub <name>( <parameters> )

<statement(s)>

End Sub

To execute:

Call <name>( <parameters> )

Page 12: Visual Basic 6 Programming

19/04/23 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.

Page 13: Visual Basic 6 Programming

19/04/23 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>

Page 14: Visual Basic 6 Programming

19/04/23 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

Page 15: Visual Basic 6 Programming

19/04/23 15

Using Multiple Forms.

e.g.

Form2.TextBox3.Text = “Hello”

dblValue = Form2.Pi() * 180#

Form2.Show

Form2.Hide

Page 16: Visual Basic 6 Programming

19/04/23 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