Confused by ByRef

I have a shared method that has the parameters: Username as string, Password as string, ByRef LoginDetails as Dictionary

I call the method with “aaa”, “bb”, session.myLoginDetails

The myLogin details is a dictionary and what I am trying to do is for the method to add values to the myLoginDetails object but I keep getting the following erro:

“You cant pass an expression as a parameter that is defined as ByRef”

Session it is a computed entity.
Use an intermediate variable of type Dictionary:

dim mydict As Dictionary = session.myLoginDetails

and then call the method.

In general you don’t need to pass a class to a method with ByRef.
A property of type Dictionary (i.e. a class ) it is already a reference to a class.
This it not true for other variable types like, for example, integers.

Regards

Well the error is misleading, but Dictionary is an object, and by definition is passed by reference.
So just strip the ByRef.

Thank you both for your replies, both of the solutions worked but I decided to remove the ByRef as I now understand why it was going wrong.

Is not it that when ‘ByRef’ is repaced with ‘ByVal’, only the local copy of the ‘session.myLoginDetails’ will get updated inside the called function/subroutine and this value will not be passed back to the calling code?

Having tested it, that does not appear to be the case as the details are being passed back to the session property and i am using them elsewhere in the app.

Objects are always passed by reference (a pointer). They live in the heap.
So when you make changes to a passed object in a method, really you are modifying the same object passed from the caller. That’s is, the caller just passed a reference to the object.

On the contrary, compound data types like Integer or Double are passed byVal by default, so they live in the stack and changes are local to the method. But you can decide to pass them byRef as an option and, in this case, the behavior is like for objects.

and by the way, making

obj1 = obj2

again doesn’t make a copy of the object, it just store the reference to the same object.

Thank you.

Does this also mean that in Xojo it is not possible to stop the called code from updating the passed object?

If an object reference is passed, then the object is visible and anything can be done with that object as in any other method.
If you need to have a copy of the object, then you have to clone it, but there are no default methods to clone an object, you have to do it yourself.
Or, if you need to prevent modifications to the passed object, use another strategy, like passing a flag (e.g. enableModifications as Boolean). But this seems to me not very good design.