1 / 13

Sub Procedures and Functions

Sub Procedures and Functions. Advanced GIS Week 2/3 2008 Spring. Subroutines (or subprocedures in VB) – Don’t return value. Option Explicit ‘manually check variable declaration Dim intMyNumber As Integer ‘Dim outside Sub Private Sub cmdCalcualte_Click()

trixie
Download Presentation

Sub Procedures and Functions

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. Sub Procedures and Functions Advanced GISWeek 2/3 2008 Spring

  2. Subroutines (or subprocedures in VB) – Don’t return value Option Explicit ‘manually check variable declaration Dim intMyNumber As Integer ‘Dim outside Sub Private Sub cmdCalcualte_Click() intMyNumber = InputBox("Input your number") Call SquareNumber End Sub Private Sub SquareNumber() lblAnswer.Caption = intMyNumber * intMyNumber End Sub

  3. Passing arguments to subroutines Private Sub SquareNumber(intNum As Double) lblAnswer.Caption = intNum * intNum End Sub Private Sub cmdCalculate_Click() intMyNumber = InputBox (“Input your _ number”) Call SquareNumber(intMyNumber) End Sub

  4. Exercise: create a sub procedure with two arguments for calculating area of a rectangle Private Sub Area(x as single, y as single) lblAnswer.Caption = x * y End Sub Private Sub cmdCalculate_Click() ‘need to declare _ width/height width = Inputbox (“Input width of a square”) height = inputbox (“Input height of a square) Call Area(width, height) End Sub

  5. Exercise –finding minimum values Private Sub cmdSmallest_Click() Dim value1 as Long, value2 as Long, value3 as Long value1 = txtOne.Text value2 = txtTwo.Text value3 = txtThree.Text Call Minimum(value1, value2, value3) Private Sub Minimum (min as Long, y as Long, z as Long) If y < min Then min = y End if If z < min Then min = z End If lblSmallest.Caption = “Smallest value is “ & min End Sub

  6. ByVal or ByRef • Parameters can be passed byVal or byRef • byRef : passes the memory location of the argument to the subprogram instead of the argument’s value. • Allows subprogram to change • Deafult • byValue: pass the copy of variable. If value of copy changed, the original will not.

  7. What will happen if cmdClick clicked 10 times? What if ByVal is replaced with ByRef? Private Sub cmdClick() Dim Number1 As Integer Number1 = 10Call IncrementVariable(Number1) MsgBox(Number1) End Sub ------------------------------------------------------------------------- Private Sub IncrementVariable(ByVal Number1 As Integer)Number1 = Number1 + 1 End Sub

  8. Functions – return values which determines data type of functions • Return a value from a function by assigning it to the name of the function itself. Basically, the function is treated as though it were a variable. • In slides 10, the SqureNumber sub can only change the lblAnswer.Caption. If a function is in place such as the follows: Option Explicit Dim intMyNumber As Integer Private Function SquareNumber(intNum As Double) As Double SquareNumber = intNum * intNum End Function Private Sub cmdCalcualte_Click() Dim intMyNumber As Double intMyNumber = txtNum.Text lblAnswer.Caption = SquareNumber(intMyNumber) End Sub

  9. Built-in Functions • String, numeric, date and time, financial functions. • Commonly used String functions • LCase/UCase: converts to L/U case • Len: return length of string • Left/Right/Mid: return specified number of cha. form left/right/middle side of a string. • Replace: replace one or more occurrence of a string inside another • LTrim/RTrim/Trim: removes spaces from the beginning (end)(both ends) of a string. • InStr/InStrRev: returns the position of the first(last) occurrence of one string within another • Join: return a string created by joining a number of substrings contained in an array.

  10. Numeric/Date/Time • Commonly used functions • Abs/Sin/Cos/Tan/Atn(arctangent)/Exp/Log/ Sqr/Sgn(sign of a number)/Rnd (random number)/Int,Fix (integer portion of a number) • Date-current system date • DateAdd- add specified interval to a date • DateDiff - differ of two specified dates

  11. Public or Private • Public subprocedures are declared like this Public Sub <subprocedure name> Public Function <function name> As <return type> • A public routine can be accessed throughout the program but a private can only called in the same Form or Module • Advantage of using Private use less memory, code protected within one corner, and names used over and over

  12. Variable Scope

  13. Static statement - to preserve the values of procedure-level variables, use Static VariableName [As data type] • used to maintain the values of variables between calls of the procedure. • Try this Private Sub Command1_Click() Static intNum As Integer intNum = intNum + 1 MsgBox intNum End Sub Try to run these code several times, the increment of intNum can be shown in Static but not Dim

More Related