# Pass a variable (class) to a function without using ByRef

Hello,
The subject has already been discussed but I did not understand anything and I do not find the solution to my problem.
The solution is certainly here in this forum but I did not find it.
I hope my question will be clear because I have a little trouble explaining it.
When I pass a variable (class) to a function without using ByRef the variable returns modified.
First I do not understand this principle and secondly how to return the variable unchanged?
How to assign my variable again to its starting value?

Exemple:

``````Class
Ptxy
x As Integer
y As Integer
----------------------------------------
Call
Dim Pt As Ptxy
Dim i As Integer
Pt = New Ptxy
Pt.x = 1
Pt.y = 2

i = Method(Pt)
----------------------------------------
Method( Pt As Ptxy ) Integer

Dim Pt1 As Ptxy

Pt1 = Pt

Pt1.x = 5
Pt1.y = 10

i = 5
Return i``````

Pt changes in the call function in (5,10) while I want to keep (1,2)
Where and how to keep the starting value (1,2)?
Thanks for your help.
BB

When you pass an object to a method, you pass the actual object, not a copy of it. If you want it to not be modified, you will have to create a copy of it in the method and act on that copy instead of the original.

Thank you Tim but the original is changing when I work on the copy.
As in my example Pt1 is a copy of Pt and Pt is modified.

When you

``Pt1 = Pt``

you are essentially pointing Pt1 to Pt, no new copy is made

If you do this instead

``Pt1 = New Ptxy``

Pt1 is now a new Ptxy where x and y have not been set to anything

so you can do this

```Pt1 = New Ptxy Pt1.x = Pt.x // actually copy x Pt1.y = Pt.y // actually copy y```

You can now work on Pt1 without changing values in Pt

You might ask why

``Pt1.x = Pt.x``

acts differently to

``Pt1 = px``

because x is an intrinsic type (integer) and not an object, the value is copied across rather than pointing one to the other as happened right at the top of this post.

I hope that helps a little

And just for the sake of those not aware, arrays are also passed by reference even when you don’t use ByRef. More information is available by looking at the Xojo docs for ByRef and ByVal .

No arrays are NOT passed by reference
They are a reference type
There’s a HUGE difference
https://blog.xojo.com/2019/01/23/byref-vs-reference-types/
https://blog.xojo.com/2019/01/24/some-follow-up-regarding-byref/

Thank you friends,
I chose a simple example to ask the question but in reality my variable is an array (15,15).
I read this topic in the documentation but I hoped I could get around this problem.
Isn’t there a more elegant solution because there I have to make 225 intialization operations of my variable and this function is called hundreds of times?

No, you must duplicate your array.

[quote=449779:@BenotBouillon]Thank you friends,
I chose a simple example to ask the question but in reality my variable is an array (15,15).
I read this topic in the documentation but I hoped I could get around this problem.
Isn’t there a more elegant solution because there I have to make 225 intialization operations of my variable and this function is called hundreds of times?[/quote]
Can you provide a bit more information about your array and what you are doing with it in the method that requires you to clone it?

In fact, I’m translating a program that I wrote few years ago in another language from the well-known words game.
For example, one of my variables is the grid of the game which is an array (15,15) of information from the grid where I test all possible words.
So now I memorize each variable (there is a lot) in simple variable before their call and then I reinject them into the called variable after processing the method.
That makes the program heavier and increases the calculation time; if you have another solution I am more than interrested!

You need to make a strategy which object is responsible for which action. You need to include how the data flows into the objects and out again. Then you need to take into consideration what makes your app fast and what makes it slow. In OO each object should be responsible for it’s own data. That is usually bad for speed.

My own application handles a lot of data. It has a central repository for the main data - a class as property of the app. The operations on the app property are done byref which avoids copying the data into a new variable. This gives a very nice speed gain over the “proper” OO approach.

You need to be aware of the OO rules and when to break them.