π 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 ) }
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
What does this code do? π€
- It creates a lookup table to convert the letter codes into readable names
- Checks if we're in the application shell (A)
- 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)