May 13, 2021 vb.net
As we mentioned in the previous chapter, the Sub procedure is a process that does not return any values. W e've been using sub-process Main in all our examples. S o far, we've written console applications in these tutorials. W hen these applications start, control goes to the main sub-program, which in turn runs any other statements that make up the main body of the program.
Sub statements are used to declare the name, parameters, and body of a sub-procedure. T he syntax of sub statements is:
[Modifiers] Sub SubName [(ParameterList)] [Statements] End Sub
Modifiers modifier:
specifies
the level of access to the procedure;
Possible values are:
Public, Private, Protected, Friend, Protected Friend and information withing overloading, overriding, sharing, and shadowing
SubName sub-name: Represents the child's name
ParameterList Parameter List: A list of specified parameters
The following example demonstrates a sub-process, CalculatePay, which accepts two parameter hours and salaries and shows the employee's total salary:
Sub CalculatePay(ByRef hours As Double, ByRef wage As Decimal) 'local variable declaration Dim pay As Double pay = hours * wage Console.WriteLine("Total Pay: {0:C}", pay) End Sub Sub Main() 'calling the CalculatePay Sub Procedure CalculatePay(25, 10) CalculatePay(40, 20) CalculatePay(30, 27.5) Console.ReadLine() End Sub End Module
When the above code is compiled and executed, it produces the following results:
Total Pay: $250.00 Total Pay: $800.00 Total Pay: $825.00
This is the default mechanism for passing parameters to methods. I n this mechanism, when a method is called, a new storage location is created for each value parameter. T he values of the actual parameters are copied into them. T herefore, changes made to the parameters in the method have no effect on the parameters.
In VB.Net, you can use the ByVal keyword to declare reference parameters. T he following example demonstrates this concept:
Module paramByval Sub swap(ByVal x As Integer, ByVal y As Integer) Dim temp As Integer temp = x ' save the value of x x = y ' put y into x y = temp 'put temp into y End Sub Sub Main() ' local variable definition Dim a As Integer = 100 Dim b As Integer = 200 Console.WriteLine("Before swap, value of a : {0}", a) Console.WriteLine("Before swap, value of b : {0}", b) ' calling a function to swap the values ' swap(a, b) Console.WriteLine("After swap, value of a : {0}", a) Console.WriteLine("After swap, value of b : {0}", b) Console.ReadLine() End Sub End Module
When the above code is compiled and executed, it produces the following results:
Before swap, value of a :100 Before swap, value of b :200 After swap, value of a :100 After swap, value of b :200
It indicates that although they have changed inside the function, there has been no change in the value.
A reference argument is a reference to the memory location of a variable. W hen you pass parameters by reference, unlike value parameters, no new storage locations are created for those parameters. T he reference parameters represent the same memory location as the actual parameters provided to the method.
In VB.Net, you can use the ByRef keyword to declare reference parameters. T he following example demonstrates this:
Module paramByref Sub swap(ByRef x As Integer, ByRef y As Integer) Dim temp As Integer temp = x ' save the value of x x = y ' put y into x y = temp 'put temp into y End Sub Sub Main() ' local variable definition Dim a As Integer = 100 Dim b As Integer = 200 Console.WriteLine("Before swap, value of a : {0}", a) Console.WriteLine("Before swap, value of b : {0}", b) ' calling a function to swap the values ' swap(a, b) Console.WriteLine("After swap, value of a : {0}", a) Console.WriteLine("After swap, value of b : {0}", b) Console.ReadLine() End Sub End Module
When the above code is compiled and executed, it produces the following results:
Before swap, value of a : 100 Before swap, value of b : 200 After swap, value of a : 200 After swap, value of b : 100