8 21 2 SET_REF Examples

LANSA Technical

8.21.2 SET_REF Examples

Example 1: *CREATE_FROM

This form can be used to load (create and display) instances of other forms:

This loader form has a collection of forms (#FORMS). When you click the Load button, a new instance of the form which you specified is created and added to the #FORMS collection and then shown.

BEGIN_COM HEIGHT(123) LEFT(296) TOP(120) WIDTH(209)
DEFINE_COM CLASS(#PRIM_PHBN) NAME(#PHBN_1) CAPTION('Load') DISPLAYPOSITION(2) LEFT(16) PARENT(#COM_OWNER) TABPOSITION(2) TOP(45)
*The name of the form to be loaded
DEFINE_COM CLASS(#STD_OBJ.Visual) NAME(#STD_OBJ) CAPTION('Form to Load:') DISPLAYPOSITION(1) HEIGHT(19) LABELTYPE(Caption) LEFT(8) MARGINLEFT(80) PARENT(#COM_OWNER) TABPOSITION(1) TOP(8) WIDTH(161)
*The form counter and collection
define #FormTot Reffld(#STD_NUM) default(0)
DEFINE_COM CLASS(#PRIM_KCOL) NAME(#FORMS) COLLECTS(#PRIM_FORM) KEYEDBY(#STD_NUM) STYLE(Collection)
 
define #Position Reffld(#STD_NUM) default(1)
 
EVTROUTINE HANDLING(#PHBN_1.Click)
* increase form counter by one
Change #FormTot '#FormTot + 1'
 
* create a new instance of the named form and set a reference to it
Set_Ref #Forms<#FormTot> (*Create_from #Std_Obj.Value)
 
* control the position of the form
Set #Forms<#FormTot> Left(#Position) Top(#Position)
Change #Position '#Position + 10'
 
*show the form
Invoke #Forms<#FormTot>.ShowForm
ENDROUTINE
 
END_COM
 

Try copying and pasting the source code to a form component and compile it. Execute the form and use it to create and display instances of other forms by specifying the name of the form to be displayed and clicking the Load button.

Example 2: *CREATE_AS

The component reference to an ActiveX component for Microsoft Word is defined dynamically so that no reference is created when the component definition statement is executed.

DEFINE_COM Class(#VA_WORD.Application) Name(#WordApp) Reference(*DYNAMIC)
 

A reference to the object is created only when it is explicitly declared in a SET_REF command.

SET_REF COM(#WordApp) To(*CREATE_AS #VA_WORD.application)
 

Because the reference is created with the *CREATE_AS keyword, a new instance of the #WordApp component is created.

Example 3: *DYNAMIC and Variable Name

A variable of the type Microsoft Word document is defined so that the this variable can be used to refer to a document:

DEFINE_COM Class(#VA_WORD.Document) Name(#WordDoc) Reference(*DYNAMIC)
 

The variable name is used to create a reference to a newly created document which is returned (in the parameter add_retval) when a document is created:

INVOKE Method(#WordApp.documents.add) add_retval(#WordDoc)
 

This reference is then used to access the methods, properties and events of the document:

Set Com(#edit_1) Value(#WordDoc.Name_COM)
INVOKE Method(#WordDoc.PrintOut)
 

Later on the same variable is changed to refer to which ever document is active at the moment:

SET_REF COM(#WordDoc) To(#WordApp.ActiveDocument)