Assignment for autoinstantiated user objects

Declaring an autoinstantiated user object creates an instance of that object (just like a structure). The CREATE statement is not allowed for objects with the Autoinstantiate setting. In the following example, uo_emp_data has the Autoinstantiate setting:

uo_emp_data uo_emp1, uo_emp2 // Two object instances

When you assign an autoinstantiated object to another autoinstantiated object, the whole object is copied to the second variable:

uo_emp1 = uo_emp2

You never have multiple references to an autoinstantiated user object.

Passing to a function

When you pass an autoinstantiated user object to a function, it behaves like a structure:

Restrictions for copying

Assignments are allowed between autoinstantiated user objects only if the object types match or if the target is a nonautoinstantiated ancestor.

Rule 1 If you assign one autoinstantiated object to another, they must be of the same type.

Rule 2 If you assign an autoinstantiated descendent object to an ancestor variable, the ancestor cannot have the Autoinstantiate setting. The ancestor variable will contain a reference to a copy of its descendant.

Rule 3 If you assign an ancestor object to a descendent variable, the ancestor must contain an instance of the descendant or an execution error occurs.

Examples

To illustrate, suppose you have these declarations. Uo_emp_active and uo_emp_inactive are autoinstantiated objects that are descendants of non-autoinstantiated uo_emp_data:

uo_emp_data uo_emp1 // Ancestor

uo_emp_active uo_empa, uo_empb // Descendants

uo_emp_inactive uo_empi // Another descendant

Example of rule 1 When assigning one instance to another from the user objects declared above, some assignments are not allowed by the compiler:

uo_empb = uo_empa // Allowed, same type

uo_empa = uo_empi // Not allowed, different types

Example of rule 2 After this assignment, uo_emp1 contains a copy of the descendent object uo_empa. Uo_emp_data (the type for uo_emp1) must not be autoinstantiated. Otherwise, the assignment violates rule 1. If uo_emp1 is autoinstantiated, a compiler error occurs:

uo_emp1 = uo_empa

Example of rule 3 This assignment is only allowed if uo_emp1 contains an instance of its descendant uo_empa, which it would if the previous assignment had occurred before this one:

uo_empa = uo_emp1

If it did not contain an instance of target descendent type, an execution error would occur.

For more information about passing arguments to functions and events, see “Passing arguments to functions and events”.