Difference between revisions of "Documentation/DevGuide/ProUNO/Basic/Mapping of Enums and Constant Groups"
(Initial author Sun Microsystems, Inc.) |
|||
(7 intermediate revisions by 5 users not shown) | |||
Line 7: | Line 7: | ||
|NextPage=Documentation/DevGuide/ProUNO/Basic/Case Sensitivity | |NextPage=Documentation/DevGuide/ProUNO/Basic/Case Sensitivity | ||
}} | }} | ||
+ | {{Documentation/DevGuideLanguages|Documentation/DevGuide/ProUNO/Basic/{{SUBPAGENAME}}}} | ||
{{DISPLAYTITLE:Mapping of Enums and Constant Groups}} | {{DISPLAYTITLE:Mapping of Enums and Constant Groups}} | ||
− | Use the fully qualified names to address the values of an enum type by their names. The following examples assume that <code>oExample</code> and <code>oExample2</code> support <idl>com.sun.star.beans.XPropertySet</idl> with a property Status of the enum type <idl>com.sun.star.beans.PropertyState</idl>: | + | Use the fully qualified names to address the values of an enum type by their names. The following examples assume that <code>oExample</code> and <code>oExample2</code> support <idl>com.sun.star.beans.XPropertySet</idl> with a property <code>Status</code> of the enum type <idl>com.sun.star.beans.PropertyState</idl>: |
− | + | <syntaxhighlight lang="oobas"> | |
Dim EnumValue | Dim EnumValue | ||
EnumValue = com.sun.star.beans.PropertyState.DEFAULT_VALUE | EnumValue = com.sun.star.beans.PropertyState.DEFAULT_VALUE | ||
Line 15: | Line 16: | ||
eExample.Status = com.sun.star.beans.PropertyState.DEFAULT_VALUE | eExample.Status = com.sun.star.beans.PropertyState.DEFAULT_VALUE | ||
− | + | </syntaxhighlight> | |
Basic does not support Enum types. In Basic, enum values coming from UNO are converted to <code>Long</code> values. As long as Basic knows if a property or an interface method parameter expects an enum type, then the <code>Long</code> value is internally converted to the right enum type. Problems appear with Basic when interface access methods expect an <code>Any</code>: | Basic does not support Enum types. In Basic, enum values coming from UNO are converted to <code>Long</code> values. As long as Basic knows if a property or an interface method parameter expects an enum type, then the <code>Long</code> value is internally converted to the right enum type. Problems appear with Basic when interface access methods expect an <code>Any</code>: | ||
− | + | <syntaxhighlight lang="oobas"> | |
Dim EnumValue | Dim EnumValue | ||
EnumValue = oExample.Status ' EnumValue is of type Long | EnumValue = oExample.Status ' EnumValue is of type Long | ||
Line 26: | Line 27: | ||
' Accessing the property explicitly using XPropertySet methods | ' Accessing the property explicitly using XPropertySet methods | ||
oExample2.setPropertyValue( "Status", EnumValue ) ' WRONG! Will probably fail! | oExample2.setPropertyValue( "Status", EnumValue ) ' WRONG! Will probably fail! | ||
− | + | </syntaxhighlight> | |
The explicit access could fail, because <code>EnumValue</code> is passed as parameter of type <code>Any</code> to <code>setPropertyValue()</code>, therefore Basic does not know that a value of type PropertyState is expected. There is still a problem, because the Basic type for <idl>com.sun.star.beans.PropertyState</idl> is <code>Long</code>. This problem is solved in the implementation of the <idl>com.sun.star.beans.XPropertySet</idl> interface. For enum types, the implicit property access using the Basic property syntax <code>Object.Property</code> is preferred to calling generic methods using the type <code>Any</code>. In situations where only a generic interface method that expects an enum for an <code>Any</code>, there is no solution for Basic. | The explicit access could fail, because <code>EnumValue</code> is passed as parameter of type <code>Any</code> to <code>setPropertyValue()</code>, therefore Basic does not know that a value of type PropertyState is expected. There is still a problem, because the Basic type for <idl>com.sun.star.beans.PropertyState</idl> is <code>Long</code>. This problem is solved in the implementation of the <idl>com.sun.star.beans.XPropertySet</idl> interface. For enum types, the implicit property access using the Basic property syntax <code>Object.Property</code> is preferred to calling generic methods using the type <code>Any</code>. In situations where only a generic interface method that expects an enum for an <code>Any</code>, there is no solution for Basic. | ||
Constant groups are used to specify a set of constant values in IDL. In Basic, these constants can be accessed using their fully qualified names. The following code displays some constants from <idl>com.sun.star.beans.PropertyConcept</idl>: | Constant groups are used to specify a set of constant values in IDL. In Basic, these constants can be accessed using their fully qualified names. The following code displays some constants from <idl>com.sun.star.beans.PropertyConcept</idl>: | ||
− | + | <syntaxhighlight lang="oobas"> | |
− | MsgBox com.sun.star.beans.PropertyConcept.DANGEROUS ' Displays 1 | + | MsgBox com.sun.star.beans.PropertyConcept.DANGEROUS ' Displays 1 |
MsgBox com.sun.star.beans.PropertyConcept.PROPERTYSET ' Displays 2 | MsgBox com.sun.star.beans.PropertyConcept.PROPERTYSET ' Displays 2 | ||
− | + | </syntaxhighlight> | |
A constant group or enum can be assigned to an object. This method is used to shorten code if more than one enum or constant value has to be accessed: | A constant group or enum can be assigned to an object. This method is used to shorten code if more than one enum or constant value has to be accessed: | ||
− | + | <syntaxhighlight lang="oobas"> | |
Dim oPropConcept | Dim oPropConcept | ||
oPropConcept = com.sun.star.beans.PropertyConcept | oPropConcept = com.sun.star.beans.PropertyConcept | ||
− | msgbox oPropConcept.DANGEROUS ' Displays 1 | + | msgbox oPropConcept.DANGEROUS ' Displays 1 |
msgbox oPropConcept.PROPERTYSET ' Displays 2 | msgbox oPropConcept.PROPERTYSET ' Displays 2 | ||
− | + | </syntaxhighlight> | |
{{PDL1}} | {{PDL1}} | ||
− | [[Category: Professional UNO]] | + | |
+ | [[Category:Documentation/Developer's Guide/Professional UNO]] |
Latest revision as of 13:40, 23 December 2020
Use the fully qualified names to address the values of an enum type by their names. The following examples assume that oExample
and oExample2
support com.sun.star.beans.XPropertySet with a property Status
of the enum type com.sun.star.beans.PropertyState:
Dim EnumValue EnumValue = com.sun.star.beans.PropertyState.DEFAULT_VALUE MsgBox EnumValue ' displays 1 eExample.Status = com.sun.star.beans.PropertyState.DEFAULT_VALUE
Basic does not support Enum types. In Basic, enum values coming from UNO are converted to Long
values. As long as Basic knows if a property or an interface method parameter expects an enum type, then the Long
value is internally converted to the right enum type. Problems appear with Basic when interface access methods expect an Any
:
Dim EnumValue EnumValue = oExample.Status ' EnumValue is of type Long ' Accessing the property implicitly oExample2.Status = EnumValue ' Ok! EnumValue is converted to the right enum type ' Accessing the property explicitly using XPropertySet methods oExample2.setPropertyValue( "Status", EnumValue ) ' WRONG! Will probably fail!
The explicit access could fail, because EnumValue
is passed as parameter of type Any
to setPropertyValue()
, therefore Basic does not know that a value of type PropertyState is expected. There is still a problem, because the Basic type for com.sun.star.beans.PropertyState is Long
. This problem is solved in the implementation of the com.sun.star.beans.XPropertySet interface. For enum types, the implicit property access using the Basic property syntax Object.Property
is preferred to calling generic methods using the type Any
. In situations where only a generic interface method that expects an enum for an Any
, there is no solution for Basic.
Constant groups are used to specify a set of constant values in IDL. In Basic, these constants can be accessed using their fully qualified names. The following code displays some constants from com.sun.star.beans.PropertyConcept:
MsgBox com.sun.star.beans.PropertyConcept.DANGEROUS ' Displays 1 MsgBox com.sun.star.beans.PropertyConcept.PROPERTYSET ' Displays 2
A constant group or enum can be assigned to an object. This method is used to shorten code if more than one enum or constant value has to be accessed:
Dim oPropConcept oPropConcept = com.sun.star.beans.PropertyConcept msgbox oPropConcept.DANGEROUS ' Displays 1 msgbox oPropConcept.PROPERTYSET ' Displays 2
Content on this page is licensed under the Public Documentation License (PDL). |