B
Beth
OK, Cor, pardon my considerable ignorance, but I thought the whole question
was what is the value of 'a' AFTER myMethod is called.
If you have:
dim a = "Michel"
myMethod(a)
msgbox("a is " & a)
and myMethod is:
Private Sub myMethod(byVal a as string)
a = "Beth"
then the message says 'a is Michel'. If 'a' is passed byRef, the message
says 'a is Beth'. When 'a' is passed byVal, then a second copy of 'a' is
created in memory, instead of a pointer to the existing variable declared by
the caller when it's passed byRef.
Both variables would be marked (or whatever) for garbage collection when
they fell out of scope, although the ByRef variable can't be marked as trash
when it falls out of scope in myMethod because the caller still has a
reference to it, so it will fall out of scope within the calling routine.
The 'a' passed byVal will fall out of scope within myMethod, along with all
the other variables declared in myMethod.
So you use byVal when you want the variable to remain immutable (unchanged,
or not mutating,) and byRef when you want the variable used as an output
parameter.
But I think you know that, so when you say:
ByRef is for when the referenced object is immutable
you're referring to the pointer being immutable, not the value. Although I
thought pointers were always immutable, and it was just a question of whether
or not another one gets created.
That's my understanding, anyways, but if I'm off somewhere, feel free to let
me know.
Thanks again for your response,
-Beth
was what is the value of 'a' AFTER myMethod is called.
If you have:
dim a = "Michel"
myMethod(a)
msgbox("a is " & a)
and myMethod is:
Private Sub myMethod(byVal a as string)
a = "Beth"
then the message says 'a is Michel'. If 'a' is passed byRef, the message
says 'a is Beth'. When 'a' is passed byVal, then a second copy of 'a' is
created in memory, instead of a pointer to the existing variable declared by
the caller when it's passed byRef.
Both variables would be marked (or whatever) for garbage collection when
they fell out of scope, although the ByRef variable can't be marked as trash
when it falls out of scope in myMethod because the caller still has a
reference to it, so it will fall out of scope within the calling routine.
The 'a' passed byVal will fall out of scope within myMethod, along with all
the other variables declared in myMethod.
So you use byVal when you want the variable to remain immutable (unchanged,
or not mutating,) and byRef when you want the variable used as an output
parameter.
But I think you know that, so when you say:
ByRef is for when the referenced object is immutable
you're referring to the pointer being immutable, not the value. Although I
thought pointers were always immutable, and it was just a question of whether
or not another one gets created.
That's my understanding, anyways, but if I'm off somewhere, feel free to let
me know.
Thanks again for your response,
-Beth