DEV Community

Peter + AI
Peter + AI

Posted on

🌍 Understanding Global Variables in Uniface 10.4: A Simple Guide

Note: This blog post was created with the assistance of AI to help make Uniface concepts more accessible to developers.

πŸ“‹ What Are Global Variables?

Global variables in Uniface are special variables that can be accessed from anywhere in your application. Think of them as shared storage that all parts of your program can read and write to. They work across all ProcScript in your application's components and global ProcScripts that use the same library or system library.

⚠️ Important Warning

Before you get too excited about using global variables everywhere, there's a catch! Multiple components can overwrite global variables, which can lead to unexpected bugs and hard-to-track issues. For this reason, Uniface recommends using component variables or local variables instead whenever possible.

πŸ› οΈ How to Create Global Variables

Creating global variables in Uniface is straightforward:

  1. Click the More Editors button and choose Global Variables
  2. Select or create a library to hold your variables
  3. Click New and enter the variable name and optional description
  4. Set the properties for data type and display format
  5. Save your changes - they will be automatically compiled

πŸ’» Using Global Variables in Your Code

To use a global variable in your ProcScript code, you need to prefix the variable name with $$. Variable references are not case sensitive, which gives you some flexibility.

You also need to specify the parent library in the component's Library property or set $variation in your ProcScript.

πŸ“ Practical Example

Here's a real-world example showing how to use a global variable for calculating sales tax:

$variation = "MyLib"                              ; specify the parent library
$$SALES_TAX = SUBTOTAL.ORDER * <TAX_RATE>      ; assign value to global variable
TOTAL = SUBTOTAL + $$SALES_TAX                    ; use the global variable
$$SALES_TAX = ""                                  ; clear global variable when done
Enter fullscreen mode Exit fullscreen mode

In this example:

  • We first set $variation to specify which library contains our global variable
  • We calculate the sales tax and store it in $$SALES_TAX
  • We use that value to calculate the total
  • We clear the variable when we're done to prevent issues

βœ… Best Practices

  • Clear variables when done: Always set global variables to empty string or reset them when you no longer need them
  • Use sparingly: Only use global variables when absolutely necessary - component or local variables are safer
  • Document usage: Keep track of which components use which global variables to avoid conflicts
  • Avoid handle data type: Global variables with Handle data type cannot be used for accessing operations - use component handles on services instead

🎯 When to Use Global Variables

Global variables can be useful for:

  • Application-wide configuration settings
  • User session information that needs to be accessed everywhere
  • Shared counters or flags

However, remember that in large complex applications, the risk of multiple components overwriting these values increases significantly. Always consider if there's a better alternative before reaching for global variables!

πŸ”š Summary

Global variables in Uniface 10.4 provide a way to share data across your entire application, but they come with risks. Use them carefully, always clear them when finished, and prefer local or component variables when possible. With proper care and documentation, they can be a useful tool in your Uniface development toolkit. πŸš€

Top comments (0)