1 / 16

Visual Basic 6 Programming.

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

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 4 : February 2004 Dr. Andrew Paul Myers

  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)

  3. 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!!

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

  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

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

  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

  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)

  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

  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!

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

  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.

  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>

  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

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

  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

More Related