Passing Variables By Value or Reference

ByVal vs ByRef

The difference between ByVal and ByRef is very simple once we recall the subject on pointers. These keywords are simply abbreviations of the full definition of the differences between the two.

ByVal = By Value and ByRef = By Reference

Now we can recall what I said in the previous chapter about C/C++ and pointers. Pointers act as a Reference to a memory address location. In VB however, this means that the reference is occurring in your statement directly at the location of the actual data, and where it is stored.

Selecting a ByVal would thereby mean you are only working on a copy or replica of the data, and not the actual data segment it self.

This has specific implications for how your statements can handle and alter the data in your variable. The ByRef use, would imply that a change performed by the statement of the value you pass, has an affect on the actual data, while the ByVal use would only alter the copy the statement has to work on.

Simply described, if passed ByRef, a procedure can permanently alter the data stored in the variable. Since it also is faster to send a memory address location, rather than passing a copy of a data argument, it is the default value used by Visual Basic.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC. As potential programmers, they are mentally mutilated beyond hope of regeneration.” - E. W. Dijkstra