8. Using Vision Advanced Features : Global Components : Using Global Variables
 
Share this page                  
Using Global Variables
In addition to constants, you can create variables--called global variables--to use throughout an application. You can use the variable in any of these ways:
In an expression to assign values on an Append frame
In an expression to specify a query restriction on a Browse or Update frame
As part of a parameter passed between frames
As a local variable on a frame
Note:  Global variables can be used wherever local variables are used. However, do not replace all local variables with global variables; this increases compilation time because global variables are stored in the system catalogs. You must use local variables instead of global variables wherever possible. Do not use a global variable unless the variable is used in several frames, and you do not want to pass it as a parameter.
In 4GL escape code
For example, create a global variable called "user_id" to store users' passwords. Write escape code to prompt users for their passwords when they start the application and check the value of "user_id" against a table of authorized users before allowing access to the application.
See How Writing Escape Code Works for an example of this code.
Defining a Global Variable
To define a global variable
1. Select Edit from the menu in the Application Flow Diagram Editor or Visual Query Editor window.
Because the variable definition applies to the entire application, you can select any frame as the current frame.
2. Select Global Components from the list of options.
3. Select Variables from the list of global components.
Vision displays the Edit Global Variables window as shown in the following figure:
4. Select Create from the menu.
Vision displays the Create a Global Variable pop-up window as shown in the following figure:
5. Enter a name for the variable.
6. Press Tab.
7. Enter an optional descriptive Short Remark.
This remark appears in the Edit Global Variables window. It also appears as a comment in the 4GL code generated for the variable.
8. Press Tab.
9. Specify a data type in either of these ways:
Enter a valid Ingres data type or the name of a record type that you have created previously for this application. If the type requires a length, enter the length in parentheses.
Use the ListChoices operation.
Vision displays a list of valid Ingres data types. If the type you select requires a length, Vision prompts you for the length. Enter the length without parentheses in the pop-up field. Vision places the data type in the Type field with the length, if required, in parentheses.
For example, to indicate a char(20) data type, select "char," then enter "20" in the length popup. Vision displays the type as char(20).
Note that you cannot use ListChoices for a record type.
Be sure the data type is consistent with the function of the constant. For example, if the value of the variable is a percentage, use a float data type.
10. Press Tab to move the cursor to the next field.
If you have specified a record type in the Type field, the Array field is displayed. Enter yes to indicate the variable is an array or no to indicate the variable is a single record.
If you have specified an Ingres data type in the Type field, the Nullable field is displayed. The default value of yes indicates that the variable can accept a null value. Enter no to make the variable not nullable.
11. Select OK.
Vision displays your global variable definition in the Edit Global Variables window.
12. Select End to return to the window in which you were working.
Assign an Initial Value to a Global Variable
By default, Vision sets the initial value of global variables to a blank string for character data types and zero for numeric data types.
To assign a different initial value to a global variable
1. Write a 4GL assignment statement.
2. Place this statement as "Form-Entry" escape code for the starting frame of the application (see How Writing Escape Code Works).
In this way, Vision compiles the code at the beginning of the application. Be sure to include this frame when you run the application. Otherwise, Vision does not see the assignment statement and uses the default values (a blank string or zero) instead.
Edit a Global Variable Definition
To edit the definition of a global variable to change its data type or nullability
1. Follow Steps 1 through 3 described above for defining a global variable.
Vision displays the Edit Global Variables window.
2. Position the cursor on the name of the variable you want to change.
3. Select Edit.
Vision displays the Edit a Global Variable Definition window. This window contains the information you entered when you created the variable (or the last time you edited it).
4. To change the data type, type the new data type over the current value and press Tab.
5. To change the Nullable or Array field, type yes or no as appropriate over the current value and press Tab.
6. Edit the descriptive Short Remark.
7. Select End.
Vision displays the new definition in the Edit Global Variables window.
8. Select End again to return to the window in which you were working.
Rename a Global Variable
When changing the name of a global variable you have defined, be sure to change the name at each place you use the variable throughout the application, including any 4GL assignment statement or other escape code.
To rename a global variable
1. Follow Steps 1 through 3 as described above for defining a global variable.
Vision displays the Edit Global Variables window.
2. Position the cursor on the name of the variable you want to rename.
3. Select Rename from the menu.
Vision prompts you for the new name.
4. Enter a new name for the variable.
5. Press Return.
The new name of the variable appears in the Edit Global Variables window.
Destroy a Global Variable
When destroying a variable that you no longer are using in an application, be sure to delete all references to the variable in the application, including any escape code, or Vision cannot compile and run your application.
To destroy a global variable
1. Follow Steps 1 through 3 as described above for defining a global variable.
Vision displays the Edit Global Variables window.
2. Position the cursor on the name of the variable you want to destroy.
3. Select Destroy from the menu.
Vision asks you to confirm that you want to destroy the variable.
4. Enter y and press Return.
Vision destroys the variable and removes it from the Edit Global Variables window display.