What is the difference? I always use ByVal, but, I don't really have a good idea of when should I and when not...
相关问题
- Keeping track of variable instances
- How to get the maximum of more than 2 numbers in V
- F#: Storing and mapping a list of functions
- VBA Self-Function returns #VALUE! Error on cell, w
- How does colnames function assign new column names
相关文章
- Accessing an array element when returning from a f
- Equivalent to window.setTimeout() for C++
- How can I write-protect the Matlab language?
- Java Equivalent to iif function
- How do you create a formula that has diminishing r
- Add multiplication signs (*) between coefficients
- version of .net framework launch not match .net fr
- Is it legal to take the address of a function para
I will try to explain the difference in simple words.
passing argument by value makes it input only parameter. This is the safest way, therefore is used by default in 95% of the cases.
passing argument by reference makes it both input and output parameter. The output parameter can be changed inside the function which creates a rarely used side-effect.
I know this question has pretty much been answered, but I just wanted to add the following...
The object you pass to a function is subject to ByRef/ByVal, however, if that object contains references to other objects, they can be modified by the called method regardless of ByRef/ByVal. Poor explanation, I know, see code below for a better understanding:
EDIT:
Also, consider if this function was called:
What happens in this case is "ByVal Change 3" is added to the callers list, but at the point you specify that "aList = New List" you are then pointing the new reference, to a new object, and become detached from the callers list. Both common sense and might catch you out one day, so something to bear in mind.
Think there may have been a typo in the last sample: The last sub should be "byval" and not "byref". :)
Also added a msgbox statement in trying_byval so you can understand what is meant.