What is the difference? I always use ByVal, but, I don't really have a good idea of when should I and when not...
ByRef
= You give your friend your term paper (the original) he marks it up and can return it to you.
ByVal
= You give him a copy of the term paper and he give you back his changes but you have to put them back in your original yourself.
As simple as I can make it.
Why to use ByRef:
ByRef will pass the POINTER to the object you are passing. If you are in the same memory space, this means passing just the 'word' not the object. The method you are passing it to can make changes in the original object, and does not need to pass them back at all, as they are in the original object. Useful for making large data passes faster. You can also use ByRef to allow use of a SUB rather then a FUNCTION (In VB) since it does not need to pass back the object.
Why not to use ByRef:
Since the method has access to the original, any changes made will be immediate and permanent. If the method fails, the object could be corrupted. Using ByVal will make a copy, pass the whole copy into the method, and then the method will process the info and either return a copy back, report information or do nothing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With