Zebra0.com

visual-basic procedures

Pass by reference means that instead of passing the value of a variable to a function, we pass the address of the variable as an argument. When a function receives the address of a variable it can change the value stored at that address.

Pass by reference is usually only used if we need to change the value of a variable.

In this program the variable title in Form Load is given an initial value of "Hello World" However, when we call getTitle and pass the variable title ByRef, the sub getTitle receives the variable title (it calls it str) When getTitle changes the value of str to "Welcome to Zebra0.com" it changes the value of the variable title that was passed to it.

'Programmer: Janet Joy
'Illustrate passing a value to a procedure by reference
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim title As String = "Hello World"
        getTitle(title)
        Me.Text = title
    End Sub
    Private Sub getTitle(ByRef str As String)
        str = "Welcome to Zebra0.com"
    End Sub
End Class

When you run this program you will see the word "Welcome to Zebra0.com" NOT "Hello World"

NEXT: Adding a pet