Lecture 7 Methods (functions and subroutines) Parameter Passing Scope of Variables ByVal or ByRef
Agenda: To understand the need for methods To know how to write and call methods To know how to pass parameters to a method ByRef vs. ByVal for parameters passing Scope of variables
What is a method? A method is self-contained block of code that performs some operations Methods break the program up and make it more understandable Methods promote code reuse DRY – Don’t Repeat Yourself
Why use methods? Problem You want to have same message box titles throughout the application.
If rbnLess21.Checked Then MessageBox.Show("Sorry,…", "Cruel Games") ElseIf rbn21to35.Checked Then MessageBox.Show("Let’s rock", "Cruel Games") Else MessageBox.Show("You…", "Cruel Games") End If
But what if you want to change the title of the message boxes?
You should create method Private Sub ShowMessage (ByVal text As String) MessageBox.Show(text, "Cruel Games") End Sub
Functions And what if you want to have consistency among your InputBoxes?
The code… Private Function GetInput(ByVal Message As Double) As String Return InputBox(text, “Marks") End Function
Sub and Function VB.NET can define a method using Sub keyword or using Function keyword Sub is used when the method does not return a value and is short for subroutine Function is used when the method does return the value
Calling a method You can call a method within a block in your program by specifying the name of function and parameters in parenthesis (or empty parenthesis) Dim mark As Integer = GetInput(“Enter the mark”) ShowMessage(“Let’s rock”)
ByVal vs ByRef
What will be printed? Sub Main() dim A as Integer A = 5 DoSmt(A) Debug.Write(A) ‘? End Sub Sub DoSmt(ByVal B as Integer) B = B + B
Sending by Value When we send the parameters to the function by value the called function creates a local copy of that variable and any changes to that variable will have no effect on the original variable. It is safe to send by value to avoid accidental change to the variable
The Answer is 5 … 5 5 10 Why? Main A DoSmt(By Val b As Integer)
Sending By Reference When parameter sent by reference the called function does not create a local copy of the original variable but it creates the reference to it. So changes applied to variable in the called function will effect the variable in the calling function. Used when we need to change the value of the variable in the function
What will be printed? Sub Main() dim A as Integer A = 5 DoSmt(A) Debug.Write(A) ‘? End Sub Sub DoSmt(ByRef B as Integer) B = B + B
The Answer is 10 Why? Main() … 5 A DoSmt(By Ref B As Integer) B
Scope
Variable Scope You can't define a variable at any spot in the program and use it at any spot, there is a specific relationship between where a variable is defined and where it can be used. This is known as the scope of the variable.
Variable Scope Block begins with an opening statement (For, If, Sub…) and ends with a closing one (usually Next, End If, End Sub…) Block defines a scope Variable declared inside the outer block will be visible in the inner block, but not vise-versa.
Example Dim y As Integer = 56 For i As Integer = 0 To 9 y = y – 1 ‘possible - y is visible in the inner block Next If (y < 3) Then Dim x As Integer = 9 End If x = 8 ‘impossible - x is defined in the inner block and is not accessible in the outer block
Variable Lifetime Variables are valid only after their declaration Variables are destroyed when their scope is finished Variables declared within a block will be destroyed when their block finishes execution
Example Dim y As Integer = 56 For i As Integer = 0 To 9 ‘i is created here y = y – 1 Next ‘i is destroyed at this point If (y < 3) Then Dim x As Integer = 9 ‘x is created here MsgBox("X = " & x) End If ‘x is destroyed at this point
The End