LO Basic - Variant or Object data type for UNO objects?

In his OpenOffice.org Macro document, Andrew Pitonyak says:

16.2.6. Should I Use Object Or Variant
When writing code that interacts with the UNO objects, you must decide which type to use. Although most examples use Object, page 132 of the Developer’s Guide suggests otherwise.
Always use the type Variant to declare variables for UNO objects, not the type Object. The OpenOffice.org Basic type Object is tailored for pure OpenOffice.org Basic objects and not for UNO OpenOffice.org Basic objects. The Variant variables are best for UNO objects to avoid problems that can result from the OpenOffice.org Basic specific behavior of the type Object:
Dim oService1 ' Ok
oService1 = CreateUnoService( "com.sun.star.anywhere.Something" )
Dim oService2 as Object ' NOT recommended
oService2 = CreateUnoService( "com.sun.star.anywhere.SomethingElse" )

I checked the developer documentation, and it says:

Always use the type Variant to declare variables for UNO Basic objects, not the type Object. The LibreOffice Basic type Object is tailored for pure LibreOffice Basic objects and not for UNO LibreOffice Basic objects. The Variant variables are best for UNO Basic objects to avoid problems that can result from the Template:V Basic specific behavior of the type Object:

However, in a separate discussion, the idea was mentioned that this information may no longer be correct due to updates to LO Basic that may have improved compatibility between the LO Basic Object type and the UNO Object.

Can anyone confirm (or has anyone heard) that this information is no longer up-to-date?

I don’t see how can the use of Object variable type to be problematic. The only internal difference between Variant and other variables is the “fixed” flag, which simply prevents changing data type on assignment, causing either implicit conversion (where possible) or a run-time error.

Uno objects are, from the Starbasic PoV, just objects. Their management in the SbxVariable do not depend on if it’s fixed or not (Variant). Using fixed type, you get some convenience (fewer options how to check validity; rejection of wrong assignments at run time). I think that, even if the warning had ever been useful at some point, it was really distant past, long before LibreOffice.

@mikekaganski Thank you for this very interesting information. If the warning is no longer necessary, do you think the documentation should be changed to reflect this? I would be glad to update the documentation, and I see that it is a Wiki, so it should be easy to do so, but I’m sure I would need to obtain permission first. How would I go about this?

Does anyone know if I am allowed to just go ahead and edit the documentation Wiki? Or should I ask permission first? I’m not sure how this works.

I’d say - just go ahead and do it. :slight_smile: Thank you for your contributions!

1 Like