Function swap reference parameters Learn programming Visual Basic (VB.net)

Lesson:

Functions


Exercise:

Function swap reference parameters


Objetive:

Create a function named "Swap" to swap the values of two integer numbers, which are passed by reference.

An example of use might be:

int x=5, y=3;
Swap(ref x, ref y);
Console.WriteLine("x={0}, y={1}", x, y);
(which should write "x=3, y=5")


Code:

Imports System
Public Class exercise106
    Public Shared Sub Swap(ByRef x As Integer, ByRef y As Integer)
        Dim swap As Integer
        swap = x
        x = y
        y = swap
    End Sub

    Public Shared Sub Main()
        Dim x As Integer = 5
        Dim y As Integer = 3
        Swap(x, y)
        Console.WriteLine("x: {0} , y: {1}", x, y)
    End Sub
End Class

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.