četvrtak, 29. siječnja 2009.

ByVal vs ByRef (Difference between ByVal and 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.