Accessing UNO services

I am trying to understand how to read the API reference and deduce when to use createInstance, CreateUnoService or when to simply assign a reference to a variable. Could someone please explain to me why example 1 does not work, and example 2 does:

ex1:
dim servEM as object
dim oEM as object

servEM = createUnoService(“com.sun.star.container.EnumerableMap”)
oEM = servEM.create(“string”, “any”) ← throws an error

ex 2:
dim servEM as object
dim oEM as object

servEM = com.sun.star.container.EnumerableMap
oEM = servEM.create(“string”, “any”)

Thank you in advance.

seen this https://wiki.documentfoundation.org/Documentation/BASIC_Guide#Introduction_to_the_API ?

I agree it’s sometimes cryptic :wink:

to be solution-oriented, here’s what works :

em = com.sun.star.container.EnumerableMap.create("string", "any")
em.put("test_key", "test_val")
print em.get("test_key")(0)

(from Forum OpenOffice LibreOffice NeoOffice - [Résolu][Basic] Erreur: Variable indéfini sur EnumerableMap - (Consulter le sujet))

This specific case is explained in Service Constructors section of DevGuide. When you see member functions in a service definition (as opposed to interface definition; these in service definition have no return type), it’s a constructor, which must be used as

x = qualified.service.Name.constructorName(arguments)

In this case, both constructors internally create an instance of the service, and then call its initialize method, passing the arguments they take, as an array of any. Basically, you may mimic that this way:

oEM = CreateUnoService("com.sun.star.container.EnumerableMap")
oEM.initialize(array(CreateUnoValue("type", "string"), CreateUnoValue("type", "any")))

which is the same as

oEM = com.sun.star.container.EnumerableMap.create("string", "any")
2 Likes

and also: :slight_smile:

oEm = CreateUnoServiceWithArguments("com.sun.star.container.EnumerableMap", Array(CreateUnoValue("type", "string"), CreateUnoValue("type", "any")))
1 Like