DEV Community

Peter + AI
Peter + AI

Posted on

πŸš€ Understanding Component Variables in Uniface 10.4

ℹ️ This blog post was created with AI assistance to help explain Uniface concepts in simple terms.

πŸ“¦ What Are Component Variables?

Component variables are temporary storage areas in your Uniface component. Think of them as containers where you can store data temporarily while your component is running. They work like boxes where you can put information and retrieve it later within the same component.

The key thing to remember: these variables are only available within the component where you define them. They live and die with that component instance. πŸ’«

🎯 When Should You Use Component Variables?

Component variables are perfect for several situations:

  • Running Totals πŸ“Š - Store intermediate results from calculations before showing them in a field
  • Status Flags 🚩 - Keep track of whether something has happened or not
  • Temporary Counters πŸ”’ - Count things during the component session
  • Print Data Transfer πŸ–¨οΈ - Move data into break, header, and footer frames when printing

✍️ How to Define Component Variables

Defining a component variable is straightforward. You declare it in a variables block inside the Declarations container of your component.

πŸ“ Example 1: Basic Variable Declaration

; Declarations container of component
variables 
  string vString
  numeric DIS(999P99) myNumber
endvariables
Enter fullscreen mode Exit fullscreen mode

In this example:

  • vString is a string variable (for text) πŸ“
  • myNumber is a numeric variable with specific formatting (up to 999 digits before decimal, 2 after) πŸ”’

πŸ” How to Use Component Variables

To reference your component variables in ProcScript, wrap the variable name with dollar signs: $VariableName$

πŸ’‘ Example 2: Using a Variable in Code

if ($myNumber$ <= 123)
  ; do something when number is less than or equal to 123
  message "Number is small!"
endif
Enter fullscreen mode Exit fullscreen mode

🎲 Example 3: Counter Variable

; Declarations
variables 
  numeric counter
endvariables

; In your ProcScript
$counter$ = 0
while ($counter$ < 10)
  ; perform some action 10 times
  $counter$ = $counter$ + 1
endwhile
Enter fullscreen mode Exit fullscreen mode

🏷️ Example 4: Status Flag

; Declarations
variables 
  string dataLoaded
endvariables

; In your ProcScript
if ($dataLoaded$ = "YES")
  message "Data already loaded!"
else
  ; load the data
  $dataLoaded$ = "YES"
endif
Enter fullscreen mode Exit fullscreen mode

⚠️ Important Limitations

Remember these key points when working with component variables:

  • πŸ”’ Component-Specific - Variables only work within the component where they're defined
  • πŸ”„ Data Transfer - To move data between components, use operation parameters or global variables instead
  • ⏳ Temporary Storage - These variables don't persist after the component closes

πŸŽ“ Best Practices

  • Use clear, descriptive names for your variables (like orderTotal instead of x) πŸ“›
  • Declare all variables at the beginning in the Declarations container πŸ“‹
  • Initialize your variables before using them 🎬
  • Use appropriate data types (string for text, numeric for numbers) 🎯

πŸŽ‰ Conclusion

Component variables in Uniface 10.4 are simple but powerful tools for managing temporary data within your components. They help you write cleaner code by providing a clear place to store intermediate values, status flags, and counters. Just remember they're component-specific and temporary!

Happy coding! πŸ‘¨β€πŸ’»πŸ‘©β€πŸ’»

Top comments (0)