Page 2 of 2

Re: LOCAL array and Object Behaviour with ONCLICK

Posted: Tue Sep 27, 2016 6:56 am
by Zimbo
All interesting input! Arrays are wonderful!

I'm still working on completing the content for the next few chapters which I hope to publish before I focus on uplifting the website to use a CMS.

A lot more to do but the weather is horrible here in Scotland, so I have quite a bit of time to work on this!

Re: LOCAL array and Object Behaviour with ONCLICK

Posted: Tue Sep 27, 2016 9:34 am
by esgici
esgici wrote:...
What is difference (by mean of passing argument) between ChangeOrj() and ChangeArray() functions ?
In the first adding an element ( by AADD() ) to orijinal array and array changed;
in the second array reassigned entirely new elements and array not changed;
...
There is more article about arrays:

Clipper 5's Super Arrays Section : "Passing arrays as parameters"

Laying The Foundation Part 1 - Arrays (Section : "Arrays in 5.0" )

The Secrets of Array Handling Part-1 (Section : "Arrays as parameters")

And this last one has an answer to my question :
(assigning a new value to a variable with same name as received array parameter) doesn't affect (original array).

!!!

IMHO this is because: A major rule says : except sent by reference by "@" sign, received parameter are LOCAL variable for called rutin (function or procedure). So, assigning a value such variable has LOCAL scope and doesn't affect value of parameter in in calling place. In other hand, when made an operation other than assigning a value to that received parameter, since array always passed as reference echoed to the orijinal array.

Again, what is your opinion :?: :?

Happy Clipper :D ( as a programming language, not a commercial product ;) )

Re: LOCAL array and Object Behaviour with ONCLICK

Posted: Tue Sep 27, 2016 7:40 pm
by KDJ
esgici

I will try to explain with examples.

1. Array passed by value:

Code: Select all

PROCEDURE DoTest()
  LOCAL aArr1 := {1,2,3}

  AddElement(aArr1)   //aArr1 passed by value
  MsgBox(aArr1)       //{1,2,3,4}

  ChangeArray(aArr1)  //aArr1 passed by value
  MsgBox(aArr1)       //{1,2,3,4}
RETURN

PROCEDURE AddElement(aArr2)
  aAdd(aArr2, 4)
RETURN

PROCEDURE ChangeArray(aArr2)
  aArr2 := {7,8,9}
RETURN
aArr1 := {1,2,3}
Creates new array {1,2,3} and assigns to the variable aArr1 a reference to this array.
It means, that aArr1 does not contain any array.
aArr1 contains a pointer (address in memory) to the beginning of the array.

AddElement(aArr1)
PROCEDURE AddElement(aArr2)
aArr1 is passed by value, which means, that is passed copy of variable.
aArr1 and aArr2 are different variables, but contain the same value - pointer to the same array.

aAdd(aArr2, 4)
Adds one element to the array pointed by aArr2.
Changes the contents and length of the array, but the pointer to the array does not change.
aArr1 and aArr2 still contain the pointer to the same array.

aArr2 := {7,8,9}
Creates new array {7,8,9} and assigns to the variable aArr2 a reference to this array.
Now aArr1 and aArr2 contain the pointers to two different arrays.
Array pointed by aArr1 has not been changed.

----------------------------------

2. Array passed by reference:

Code: Select all

PROCEDURE DoTest()
  LOCAL aArr1 := {1,2,3}

  AddElement(@aArr1)   //aArr1 passed by reference
  MsgBox(aArr1)       //{1,2,3,4}

  ChangeArray(@aArr1)  //aArr1 passed by reference
  MsgBox(aArr1)       //{7,8,9}
RETURN

PROCEDURE AddElement(aArr2)
  aAdd(aArr2, 4)
RETURN

PROCEDURE ChangeArray(aArr2)
  aArr2 := {7,8,9}
RETURN
AddElement(@aArr1)
PROCEDURE AddElement(aArr2)
aArr1 is passed by reference, which means, that is passed the pointer (address) to this variable.
So aArr1 and aArr2 are the same variables and contain the same value - pointer to the same array.

aArr2 := {7,8,9}
Creates new array {7,8,9} and assigns to the variable aArr2 a reference to this array.
Now aArr2 contains the pointer to {7,8,9}.
Because aArr1 and aArr2 are the same, aArr1 also contains the pointer to {7,8,9}.
In this way original array has been changed.

Re: LOCAL array and Object Behaviour with ONCLICK

Posted: Wed Sep 28, 2016 8:05 am
by Rathinagiri
Nice explanation. :)

Re: LOCAL array and Object Behaviour with ONCLICK

Posted: Wed Sep 28, 2016 10:40 am
by esgici
KDJ wrote: I will try to explain with examples.
OK; Thanks

Regards