DEV Community

Peter + AI
Peter + AI

Posted on

๐Ÿš€ Understanding Uniface 10.4 Component Instances: A Beginner's Guide

โš ๏ธ This blog post was created with AI assistance

๐Ÿ’ก What is a Component Instance?

A component instance is simply a running version of your Uniface component. Think of it like this: if a component is a recipe, then a component instance is the actual cake you bake from that recipe. You can bake multiple cakes from the same recipe, just like you can have multiple instances of the same component running at the same time! ๐ŸŽ‚

๐Ÿ”ง Creating and Deleting Instances

In Uniface 10.4, you control component instances with two main commands:

  • newinstance - Creates a new instance
  • deleteinstance - Removes an instance

๐Ÿ“ Simple Example

; Create a new instance
newinstance "MyComponent", MyHandle

; Do something with it
activate MyHandle, "DoSomething"

; Clean up when done
deleteinstance MyHandle
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”— What are Attached Instances?

An attached instance is a special type of component instance that stays connected to the component that created it. It's like a child holding their parent's hand - when the parent leaves, the child follows! ๐Ÿ‘จโ€๐Ÿ‘ง

You create attached instances in two ways:

  1. Using the /attached switch with the newinstance command
  2. Setting the Modality & Attachment widget property to "Non-Modal, Attached"

๐ŸŽฏ Why Use Attached Instances?

Attached instances are great for automatic cleanup! When the parent component exits, all attached instances are automatically deleted. This means you don't have to remember to manually delete them - Uniface does the housekeeping for you! ๐Ÿงน

๐Ÿ“ Attached Instance Example

; Create an attached instance
newinstance /attached "DialogComponent", MyDialog

; The dialog will automatically close when 
; this component exits - no cleanup needed!
Enter fullscreen mode Exit fullscreen mode

๐ŸŽญ Component Signatures and Interfaces

Every component has a signature - think of it as a contract that defines what operations you can call. You can invoke these operations using the activate statement or through handles.

๐Ÿ“ Activation Example

; Activate an operation on an instance
activate MyHandle, "ProcessData", InputParam, OutputParam
Enter fullscreen mode Exit fullscreen mode

๐Ÿ—‘๏ธ Memory Management Tips

Component instances created with handles use reference counting. This is an automatic memory management technique where Uniface keeps track of how many references point to an instance. When the reference count drops to zero, the instance is automatically deleted. Smart! ๐Ÿง 

โš ๏ธ Important Reminders

  • Always clean up instances you create (unless they're attached)
  • Multiple instances of the same component can run simultaneously
  • Attached instances automatically delete when parent exits
  • Use handles for better memory management

๐ŸŽ“ Conclusion

Component instances in Uniface 10.4 give you powerful control over your application's runtime behavior. Whether you need multiple forms running at once or automatic cleanup with attached instances, understanding these concepts will make your Uniface development much smoother! ๐ŸŒŸ

Happy coding! ๐Ÿ’ปโœจ

Top comments (0)