Passing Arguments by Reference

The alternative to passing arguments by value is passing them by reference. This gives the function or subroutine full access to the original data item rather than access to a copy. To receive an argument by reference, begin its argument declaration with ByRef. Here’s an example.

Sub ZapFirst(ByRef astrNouns() As String)
    astrNouns(0) = "Madeline"
    ReDim Preserve astrNouns(5)
    astrNouns(0) = "Malinda"
End Sub

When a ByRef argument receives a value type, updating the argument updates the original variable. When a ByRef argument receives a reference type, it can reliably delete and re-create the entire structure as well as its properties. Rerunning the previous example with ByRef in effect produces this output:

Because ...

Get Faster Smarter Beginning Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.