Visual Basic 2008 9.0 .NET Examples and Ebook
  Home

Procedures and Functions

Vorig Onderwerp

Arrays

|

Object Oriented Programming

Volgend Onderwerp

Arguments - ByVal and ByRef

Vorig Onderwerp

Introduction to Procedures

|

Local Variables and Argument Variables

Volgend Onderwerp
Working By Value with Argument Values

Working By Value with Argument Values

Working By Reference with Argument Values

Working By Reference with Argument Values

Several Arguments in One Procedure

Several Arguments in One Procedure

Exercises

Exercises



The examples of the previous topic produce the same error ( "An error occurred, please try again." ) when an invalid menu-option was chosen and when an illegal value was entered.

It's not unthinkable that you would prefer a more specific error depending on the wrongdoing.

This can be done by changing ShowError into ShowMenuError and ShowValueError.


Module Example1
    Sub Main()
        Dim menu As Char
        Dim value As Decimal
        '
        Console.Write("1 centimetres -> inches / 2 - inches -> centimetres : ")
        menu = Console.ReadLine()
        Do Until menu = "1"c OrElse menu = "2"c
            ShowMenuError()
            Console.Write("1 centimetres -> inches / 2 - inches -> centimetres : ")
            menu = Console.ReadLine()
        Loop
        '
        Console.Write("Value : ")
        value = Console.ReadLine()
        Do Until value > 0
            ShowValueError()
            Console.Write("Value : ")
            value = Console.ReadLine()
        Loop
        '
        If menu = "1"c Then
            Console.WriteLine(value & " centimetres is " & value * 0.3937 & " inches")
        Else
            Console.WriteLine(value & " inches is " & value * 2.54 & " centimetres")
        End If
        '
        Console.ReadLine()
    End Sub
    Sub ShowMenuError()
        Console.WriteLine("An illegal menu option is chosen.  Please try again.")
    End Sub
    Sub ShowValueError()
        Console.WriteLine("The value should be above zero.  Please try again.")
    End Sub
End Module
Download Broncode

Output :

 1 centimetres -> inches / 2 - inches -> centimetres : 3
 An illegal menu option is chosen.  Please try again.
 1 centimetres -> inches / 2 - inches -> centimetres : 4
 An illegal menu option is chosen.  Please try again.
 1 centimetres -> inches / 2 - inches -> centimetres : 1
 Value : -5
 The value should be above zero.  Please try again.
 Value : -10
 The value should be above zero.  Please try again.
 Value : 10
 10 centimetres is 3,937 inches

In stead of defining two ShowError procedure, we could define only one that shows a certain ( undefined ) error.

The errormessage is not statically defined, but the procedure is parametrized with the message.


Module Example2
    Sub Main()
        Dim menu As Char
        Dim value As Decimal
        '
        Console.Write("1 centimetres -> inches / 2 - inches -> centimetres : ")
        menu = Console.ReadLine()
        Do Until menu = "1"c OrElse menu = "2"c
            ShowError("An illegal menu option is chosen.")
            Console.Write("1 centimetres -> inches / 2 - inches -> centimetres : ")
            menu = Console.ReadLine()
        Loop
        '
        Console.Write("Value : ")
        value = Console.ReadLine()
        Do Until value > 0
            ShowError("The value should be above zero.")
            Console.Write("Value : ")
            value = Console.ReadLine()
        Loop
        '
        If menu = "1"c Then
            Console.WriteLine(value & " centimetres is " & value * 0.3937 & " inches")
        Else
            Console.WriteLine(value & " inches is " & value * 2.54 & " centimetres")
        End If
        '
        Console.ReadLine()
    End Sub
    Sub ShowError(ByVal message As String)
        Console.WriteLine(message & "  Please try again.")
    End Sub
End Module
Download Broncode

Output :

 1 centimetres -> inches / 2 - inches -> centimetres : 3
 An illegal menu option is chosen.  Please try again.
 1 centimetres -> inches / 2 - inches -> centimetres : 4
 An illegal menu option is chosen.  Please try again.
 1 centimetres -> inches / 2 - inches -> centimetres : 1
 Value : -5
 The value should be above zero.  Please try again.
 Value : -10
 The value should be above zero.  Please try again.
 Value : 10
 10 centimetres is 3,937 inches

An argument is defined/declared between the parentheses of the procedure definition. Just like local variables ( declared between Sub and End Sub ) these variables have an identifier and are from a specific datatype.
Keyword ByVal is added by default and will be discussed later.

When ShowError is called, a String needs to be provided by the calling routine.

The implementation of ShowError will then show the provided String value as an error.


Module Example3
    Sub Main()
        ShowMenuError()
        '
        Example2.ShowError("message 1")
        '
        Dim someMessage As String = "message 2"
        Example2.ShowError(someMessage)
        '
        Example2.ShowError("message" & " " & "3")
        '
        Example2.ShowError(Console.ReadLine())
        '
        Console.ReadLine()
    End Sub
End Module
Download Broncode

Output :

 An illegal menu option is chosen.  Please try again.
 message 1  Please try again.
 message 2  Please try again.
 message 3  Please try again.
 message 4
 message 4  Please try again.

A procedure declared within a module, is by default "public" or accessible for other modules. Example3 calls ShowError(...) defined in Example2 ( Example2.ShowError(...) ).
A call to ShowError(...) ( without referring to the declaring module ) would be ambiguous, modules Example2 and Example2 define a different ShowError.


Working By Value with Argument Values


By default ( and when the keyword ByVal is used ) a procedure works by value ( or with a shallow copy ) of the provided argument value.

MultiplyBy10 has a value that holds a copy of the provided value 5. This copy is multiplied by 10, so value of Main is untouched.


Module Example4
    Sub Main()
        Dim value As Integer = 5
        MultiplyBy10(value)
        Console.WriteLine("Main value : " & value)
    End Sub
    Sub MultiplyBy10(ByVal value As Integer)
        value *= 10
        Console.WriteLine("MultiplyBy10 value : " & value)
    End Sub
End Module
Download Broncode

Output :

 5
 50

Klik hier om terug naar boven te gaan.  Up


Working By Reference with Argument Values


If we want that the variable ( used to express the argument value ) of the calling routine changes when the argument variable of the called routine changes, we'll need to work by reference ( 'ByRef' ) with the provided argument value.

In Example5 MultiplyBy10 works by reference with the value that is expressed by value of Main. Both variables ( value of Main and value of MultiplyBy10 ) refer to the same value in memory. So if value in MultiplyBy10 is multiplied by 10, this will have effect on value of Main.


Module Example5
    Sub Main()
        Dim value As Integer = 5
        MultiplyBy10(value)
        Console.WriteLine("Main value : " & value)
        '
        MultiplyBy10(value + 1)
        Console.WriteLine("Main value : " & value)
    End Sub
    Sub MultiplyBy10(ByRef value As Integer)
        value *= 10
    End Sub
End Module
Download Broncode

Output :

 50
 50

The call MultiplyBy10(value + 1) will have no effect on value of Main.
Here value of MultiplyBy10 holds a reference to value 51 and not to 50 ( or value of Main ). 51 gets multiplied by 10, which will have no effect on value of Main ( 50 ).

When the provided argument values only serve as input for the calling routine ( "in"-arguments ) ByVal is used.
If the argument is ( also ) used to retrieve some output from the called routine ( "(in/)out" arguments ) ByRef is used.

In/Out- or out-arguments are rarely used. More elegant constructions
( functions ) can be used to retrieve output from a called routine. In a next topic we'll discuss functions.


Klik hier om terug naar boven te gaan.  Up


Several Arguments in One Procedure


Use commas to separate the declaration of several arguments in a definition of a procedure.


Module Example6
    Sub Main()
        ShowSum(1, 2)
        '
        Console.WriteLine()
    End Sub
    Sub ShowSum(ByVal value1 As Integer, ByVal value2 As Integer)
        Console.WriteLine(value1 + value2)
    End Sub
End Module
Download Broncode

Output :

 3

Each argument can be either ByVal or ByRef.


Klik hier om terug naar boven te gaan.  Up


Exercises


Task :

What will be the output of Exercise1Task ?


Module Exercise1Task
    Sub Main()
        Dim x As Integer = 4
        Dim y As Integer = 5
        Dim z As Integer = 6
        '
        Test(x, y, z + 1)
        '
        Console.WriteLine("Main x : " & x)
        Console.WriteLine("Main y : " & y)
        Console.WriteLine("Main z : " & z)
        Console.ReadLine()
    End Sub
    Sub Test(ByRef y As Integer, ByVal z As Integer, ByRef x As Integer)
        z ^= 2
        x += z
        y += z
        '
        Console.WriteLine("Test x : " & x)
        Console.WriteLine("Test y : " & y)
        Console.WriteLine("Test z : " & z)
    End Sub
End Module
Download Broncode

Solution :


Output :

 Test x : 32
 Test y : 29
 Test z : 25
 Main x : 29
 Main y : 5
 Main z : 6

Task :


Create module Exercise2Solution with procedures Input and ShowSmallest.


Module Exercise2Task
    Sub Main()
        Dim number1, number2 As Integer
        '
        'Exercise2Solution.Input(number1, "Number 1 ? : ")
        'Exercise2Solution.Input(number2, "Number 2 ? : ")
        '
        'Exercise2Solution.ShowSmallest(number1, number2)
        '
        Console.ReadLine()
    End Sub
End Module
Download Broncode

Output :

 Number 1 ? : 5
 Number 2 ? : 4
 Smallest : 4

Solution :


Module Exercise2Solution
    Sub Input(ByRef value As Integer, ByVal message As String)
        Console.Write(message)
        value = Console.ReadLine()
    End Sub
    Sub ShowSmallest(ByVal value1 As Integer, ByVal value2 As Integer)
        Dim smallest As Integer = value1
        If value2 < smallest Then smallest = value2
        Console.WriteLine("Smallest : " & smallest)
    End Sub
End Module
Download Broncode

Updated On : 2008-11-06

Download Broncode

Published On : 2008-11-06

Arguments - ByVal and ByRef

Vorig Onderwerp

Introduction to Procedures

|

Local Variables and Argument Variables

Volgend Onderwerp

Procedures and Functions

Vorig Onderwerp

Arrays

|

Object Oriented Programming

Volgend Onderwerp
  Home  
Nederlands
Nederlands

Add to favorites (IE).


No printable version available.