DEV Community

Peter + AI
Peter + AI

Posted on

πŸš€ Understanding $componenttype in Uniface 10.4: A Complete Gui

πŸ“ This blog post was created with AI assistance to help developers understand Uniface 10.4 better.

🎯 What is $componenttype?

The $componenttype function is a built-in ProcScript function in Uniface 10.4 that tells you what type of component you're working with. Think of it as asking "What kind of building block is this?" in your Uniface application.

πŸ’‘ Why Do You Need This Function?

Imagine you're building a complex application with different types of components - forms, services, reports, and web pages. Sometimes your code needs to behave differently depending on which type of component is running. That's where $componenttype becomes your best friend! 🀝

πŸ”§ How to Use $componenttype

The syntax is super simple:

$componenttype { ( InstanceName | componentName ) }
Enter fullscreen mode Exit fullscreen mode

You can use it in three ways:

  • Without parameters: $componenttype - checks the current instance
  • With instance name: $componenttype("MyFormInstance")
  • With component name: $componenttype("CustomerForm")

πŸ“‹ Return Values Explained

The function returns single letters that represent different component types:

Return Value Component Type Description
A Application Shell 🏠 The main application container
D Dynamic Server Page 🌐 Web page with dynamic content
P Static Server Page πŸ“„ Web page with static content
F Form πŸ–ΌοΈ User interface form
R Report πŸ“Š Report component
S Service βš™οΈ Business logic service
E Entity Service πŸ—ƒοΈ Database entity service
N Session Service πŸ‘€ Session management service
"" Error ❌ Something went wrong

πŸ” Real-World Example

Here's a practical example from the documentation:

getitem/id $COMPTYPE$, "D=DSP;P=USP;S=Service;=unknown", $componenttype
if ($componenttype = "A")
  putmess "Application shell is %%$applname"
else
  putmess "Current instance is from a %%$COMPTYPE$ named %%$componentname"
endif
Enter fullscreen mode Exit fullscreen mode

What does this code do? πŸ€”

  1. It creates a lookup table to convert the letter codes into readable names
  2. Checks if we're in the application shell (A)
  3. Displays different messages based on the component type

⚠️ Error Handling

When things go wrong, $componenttype returns an empty string. Common errors include:

  • -1105: Invalid instance or component name 🚫
  • -1304: Function called from wrong context (like apStart trigger) πŸ”„

Always check $procerror when you get an empty string to understand what went wrong!

✨ Best Practices

  • Always handle errors: Check if the return value is empty
  • Use meaningful variable names: Store the result in descriptive variables
  • Document your code: Explain why you're checking component types
  • Consider context: Remember some contexts don't have current instances

πŸŽ‰ Conclusion

The $componenttype function is a powerful tool for creating flexible, context-aware Uniface applications. Whether you're building web applications, desktop forms, or complex services, understanding component types helps you write smarter code that adapts to its environment.

Remember: Good code knows what it's working with! 🧠✨

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

Top comments (0)