KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Moving an array element from one array to another array
PRODUCT: 4D | VERSION: | PLATFORM: Mac & Win
Published On: February 6, 2002

Compatibility: 6.5.x and 6.7.x

The following method allows you to move an array element from one array to

another. This method is a generic method which enables you to use it with any array.


`Project Method: MoveArrayElement

`$1 - a pointer to the array whose

` element will be moved from

` $2 - a pointer to the array to which the element will be moved to

`By: Add Komoncharoensiri - 4D, Inc.


C_POINTER($1;$2)

C_LONGINT($selected;$lastelement)

If (($1->)>0)

 $selected:=Find in array($1->;$1->{$1->})

 $lastelement:=Size of array($2->)

 INSERT ELEMENT($2->;$lastelement+1)

 $2->{$lastelement+1}:=$1->{$selected}

 DELETE ELEMENT($1->;$selected;1)

End if




To see how this method works, let's consider the following example:


Two scrollable areas are created: sArray1 and sArray2. Both objects have the On Double Clicked event selected.


sArray1 has the following code in its object method.


Case of

 : (Form event=On Load )

 ARRAY TEXT(sArray1;5)

 sArray1{1}:="Joe@yahoo.com"

 sArray1{2}:="Sammy@aol.com"

 sArray1{3}:="Add@4d.com"

 sArray1{4}:="Mike2002@Nowhere.com"

 sArray1{5}:="James007@bond.com"

 : (Form event=On Double Clicked )

 MoveArrayElement (->sArray1;->sArray2)

End case


sArray2 has the following code in its object method.


Case of

 : (Form event=On Load )

 ARRAY TEXT(sArray2;0)

 : (Form event=On Double Clicked )

 MoveArrayElement (->sArray2;->sArray1)

End case


When the user double-clicks an element in the Contact emails list, the element is removed from the "Contact emails" list and added to the "To send" list. The picture below shows both scrollable objects in their initial state.


After the user double-clicked on Sammy@aol.com and Add@4d.com from the "Contact emails" list.


After the user double-clicked on Sammy@aol.com from the "To send" list: