ASSIGN a Data Structure variable to another Data Set variable

You can assign Data Structure variables using the SET statement.

SET destination -> source 				(* Creates a reference *) 
SET destination -> NEW source (* Creates a copy *) 

Assignment of a Data Structure variable to another Data Set variable creates a reference to the source Data Structure. No data is copied. All instances of a reference access the same set of content members, and changes affect both variables. Both the source variable Data Structure and destination variable Data Structure must have the same type.

KCM does not support using _data variable as a destination.

The optional keyword NEW forces KCM to copy all content from the source Data Structure instead of creating a reference. Any subsequent change to the source Data Structure variable or the destination Data Structure variable affects that variable only.

DECLARE pol DEFINED_AS Policy 
DECLARE customer DEFINED_AS Relation 
DECLARE someone_else DEFINED_AS Relation 
... 
ASSIGN customer.CustomerNumber := 42 
ASSIGN someone_else.CustomerNumber := 1 
SET pol.Insured -> customer 

The preceding example creates a reference between the customer variable and the insured member of the pol variable. Both customer.CustomerNumber and pol.Insured.CustomerNumber share the same content and have the value 42.

ASSIGN pol.Insured.CustomerNumber := 100

Because of the reference both customer.CustomerNumber and pol.Insured.CustomerNumber now have the value 100.

SET pol.Insured -> NEW someone_else

Copy the content of the variable someone_else to pol.Insured. The existing reference is overwritten by this copy and does not affect the variable customer.

ASSIGN pol.Insured.CustomerNumber := 123456

Since there is no reference anymore the assignment to pol.Insured only affects its own content. CustomerNumber still has the value 100. Because of the copy someone_else.CustomerNumber is also unaffected and retains the value 1.

You can use the statement SET to create references to Data Structure members within a larger variable Data Structure and access these structures directly.

FOR i FROM 1 UPTO length_struct_array (life_insurance.Beneficiaries) DO 
		DECLARE ben 
		SET ben -> life_insurance.Beneficiaries[i] 
		... 
		ASSIGN ... := ... ben.CustomerNumber ... 
		ASSIGN ben.Person := ... 
		... 
OD 

The following code loops through all elements of the member ARRAY Beneficiaries and assigns them to the variable ben. Since ben is assigned as a reference, it functions as an alias to the current element in the Benificiaries member. Any change to ben also affects the current element.

The declaration of the variable ben does not need an explicit type because this can be derived from the subsequent statement SET.

DECLARE senior 
FOR i FROM 1 UPTO length_struct_array (life_insurance.Benificiaries) DO 
		IF life_insurance.Beneficiaries[i].Age > senior.Age THEN 
			SET senior -> life_insurance.Beneficiaries[i] 
		FI 
OD 
...

The preceding loop selects the most senior element from the Benificiaries ARRAY for further processing.