LOCAL array and Object Behaviour with ONCLICK

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

Zimbo
Posts: 56
Joined: Mon Nov 05, 2012 1:28 pm
Location: Scotland
Contact:

Re: LOCAL array and Object Behaviour with ONCLICK

Post 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!
User avatar
esgici
Posts: 4543
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Contact:

Re: LOCAL array and Object Behaviour with ONCLICK

Post 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 ;) )
Viva INTERNATIONAL HMG :D
KDJ
Posts: 243
Joined: Mon Sep 05, 2016 3:04 am
Location: Poland

Re: LOCAL array and Object Behaviour with ONCLICK

Post 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.
User avatar
Rathinagiri
Posts: 5471
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: LOCAL array and Object Behaviour with ONCLICK

Post by Rathinagiri »

Nice explanation. :)
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
User avatar
esgici
Posts: 4543
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Contact:

Re: LOCAL array and Object Behaviour with ONCLICK

Post by esgici »

KDJ wrote: I will try to explain with examples.
OK; Thanks

Regards
Viva INTERNATIONAL HMG :D
Post Reply