Easy demonstration of globally scoped and locally scoped components

Date:12 May 2003
Product/Release:Visual LANSA
Abstract:Easy demonstration of global scope and locally scoped components
Submitted By:LANSA Support
Last Review:November 2010

This tip demonstrates the difference between a locally scoped component and a globally scoped component. In order to benefit from this tip cut and paste the following code into three form components and then compile and run tipfrm1. The demonstration also displays a locally scoped modeless form. In order to get a full understanding of this, run the demonstration in debug.

TIPFRM2. Is an example of a form that has been created dynamically. Because the define_Com has been defined at the top of the form, the reference exists as soon as it has been created and can be referenced from any where within the owner form. This can be referred to as global scope. Meaning that this component is available to anything within the form that creates it.

TIPFRM3. Is an example of a form that has been created dynamically. Because the define_Com has been defined in the event routine, the reference exists for the duration of the event or method in which the define_com exists. This is referred to as locally scoped. What that means is that when the event routine has completed the reference that was created is then destroyed. This is very useful as you do not need to remember to destroy a reference after it has been created.

However, there is also the risk that the reference is destroyed before you have finished with it. For example, TIPFRM3 is an object that is locally defined, the only reason that this form has not been destroyed by the ending of the routine, is that this form is modal. If this form was not being shown as modal, the form would be created and then destroyed when it reached the end of the routine.

The above is explained when the demonstration is run.

* TIPFRM1
* 
* COMPONENT: STD_FORM
* 
Function Options(*DIRECT)
Begin_Com Role(*EXTENDS #PRIM_FORM) Clientheight(38) Clientwidth(482) Height(65) Left(326) Top(135) Width(490)
Define_Com Class(#PRIM_PHBN) Name(#PHBN_1) Caption('Globally Scoped Form') Displayposition(1) Left(8) Parent(#COM_OWNER) Tabposition(1) Top(8) Width(137)

* The *DYNAMIC at the end of this define_com specifies that I CAN created tipfrm2 if I want to. It is not automatically created
Define_Com Class(#tipfrm2) Name(#NextForm) Reference(*dynamic)
Define_Com Class(#PRIM_PHBN) Name(#PHBN_2) Caption('Locally Scoped Modal Form') Displayposition(2) Left(152) Parent(#COM_OWNER) Tabposition(2) Top(8) Width(177)
Define_Com Class(#PRIM_PHBN) Name(#PHBN_3) Caption('Locally Scoped Modeless') Displayposition(3) Left(336) Parent(#COM_OWNER) Tabposition(3) Top(8) Width(145)

Evtroutine Handling(#com_owner.Initialize)
Set Com(#com_owner) Caption(*component_desc)
Endroutine

Evtroutine Handling(#PHBN_1.Click)
* Create the second form and show it

* This creates an instance of tipfrm2 called NextForm
Set_Ref Com(#NextForm) To(*Create_as #tipfrm2)

* Now that I have created an instance of #tipfrm2 I can access its methods
Invoke Method(#NextForm.Show_Form)
Endroutine

Evtroutine Handling(#PHBN_2.Click)
* This example shows dynamically creating an instance that has method scope
* Because we have placed the define_com in the event routine SecondForm only exists in this routine
* it is not global. Meaning that we can not use this oject outside of this routine
Define_Com Class(#tipfrm3) Name(#SecondForm) Reference(*dynamic)

* This creates an instance of tipfrm2 called NextForm
Set_Ref Com(#SecondForm) To(*Create_as #tipfrm3)

Invoke Method(#SecondForm.Show_Form)
Endroutine

Evtroutine Handling(#PHBN_3.Click)
* This form will only last the duration of this event routine. The instance is alive until the end of this routine only.
* In order to see the effect of this run this in debug.
Define_Com Class(#prim_form) Name(#ThirdForm) Reference(*dynamic)

Set_Ref Com(#ThirdForm) To(*create_as #prim_form)

Invoke Method(#ThirdForm.showform)
Endroutine
End_Com

* TIPFRM2
* 
* COMPONENT: STD_FORM
* 
Function Options(*DIRECT)
Begin_Com Role(*EXTENDS #PRIM_FORM) Clientheight(193) Clientwidth(483) Height(220) Left(286) Top(152) Width(491)
Define_Com Class(#PRIM_LABL) Name(#LABL_1) Caption('This is an example of a form that has been created dynamically. Because the define_Com has been defined at the top off the form, the reference exists as soon as it has been created and can be referenced from any where within the owner form. This can be referred to as global scope, meaning that this component is available to anything within the form that creates it.') Displayposition(1) Height(65) Left(16) Parent(#COM_OWNER) Tabposition(1) Tabstop(False) Top(8) Width(457)
Define_Com Class(#PRIM_LABL) Name(#LABL_2) Caption('This form can only be reference after it has been created. The form is created when this line of code has been executed Set_Ref Com(#NextForm) To(*Create_as #tipfrm2)') Displayposition(2) Left(15) Parent(#COM_OWNER) Tabposition(2) Tabstop(False) Top(85) Width(466)
Define_Com Class(#PRIM_PHBN) Name(#PHBN_1) Caption('Close') Displayposition(3) Left(400) Parent(#COM_OWNER) Tabposition(3) Top(160)

Evtroutine Handling(#com_owner.Initialize)
Set Com(#com_owner) Caption(*component_desc)
Endroutine

Mthroutine Name(Show_Form)
Invoke Method(#com_owner.ShowModalForm)
Endroutine

Evtroutine Handling(#PHBN_1.Click)
Invoke Method(#com_owner.closeform)
Endroutine
End_Com

* TIPFRM3
* 
* COMPONENT: STD_FORM
* 
Function Options(*DIRECT)
Begin_Com Role(*EXTENDS #PRIM_FORM) Clientheight(277) Clientwidth(492) Height(304) Left(351) Top(163)
Define_Com Class(#PRIM_LABL) Name(#LABL_1) Caption('This is an example of a form that has been created dynamically. Because the define_Com has been defined in the event routine, the reference exists for the duration of the event or method in which the define_com exists. This is referred to as locally scoped.') Displayposition(1) Height(57) Parent(#COM_OWNER) Tabposition(1) Tabstop(False) Top(16) Width(471)
Define_Com Class(#PRIM_LABL) Name(#LABL_2) Caption('What that means is that when the event routine has completed the reference that was created (i.e. that is the reference for this form) is then destroyed. This is very useful as you do not need to remember to destroy a reference after it has been created. ') Displayposition(2) Height(58) Left(11) Parent(#COM_OWNER) Tabposition(2) Tabstop(False) Top(87) Width(470)
Define_Com Class(#PRIM_LABL) Name(#LABL_3) Caption('However, there is also the risk that the reference is destroyed before you have finished with it. For example, this form that you are looking at now is an object that is locally defined, the only reason that this form has not been destroyed by the ending of the routine, is that this form is modal. If this form was not being shown as modal, the form would be created and then destroyed when it reached the end of the routine. ') Displayposition(3) Height(70) Left(13) Parent(#COM_OWNER) Tabposition(3) Tabstop(False) Top(163) Width(476)
Define_Com Class(#PRIM_PHBN) Name(#PHBN_1) Caption('Close ') Displayposition(4) Left(408) Parent(#COM_OWNER) Tabposition(4) Top(248)

Evtroutine Handling(#com_owner.Initialize)
Set Com(#com_owner) Caption(*component_desc)
Endroutine

Mthroutine Name(Show_Form)
Invoke Method(#com_owner.ShowModalForm)
Endroutine

Evtroutine Handling(#PHBN_1.Click)
Invoke Method(#com_owner.closeForm)
Endroutine
End_Com