Mapping of Enums and Constant Groups

From Apache OpenOffice Wiki
Jump to: navigation, search



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).
Personal tools
In other languages