Tech Articles


How to Emulate C# Style Enumerations in PowerScript


This article describes how you can emulate C# style enumeration types in PowerScript since PowerScript currently does not support creation of custom enumerations. Personally, I found myself in need of such enumerations when interfacing to Microsoft Word/Excel using OLE Automation.

Calls to Word or Excel functions without enumerated values (or named constants) are incredibly hard to read and understand. An example:

oleDocument.Selection.Move( 10, 2)
oleDocument.Selection.Move( 12, 4)

Using C# style enumerations, the same code could read like this:

oleDocument.Selection.Move( wdUnits.Row, 2)
oleDocument.Selection.Move( wdUnits.Cell, 4)

PowerScript Enumerations vs. C# Enumerations

PowerScript's built-in enumeration types differ from C# style enumerations in several ways:

Enumerations comparison PowerScript vs. C#
Topic PowerScript C#
Enumerated identifiers Suffixed by exclamation point
Maximized!
Standard identifiers without an exclamation point
wdWindowStateMaximize
Custom definitions Not supported Defined using the enum keyword
Access using type prefix
MyType.Value
Invalid syntax
​You must refer without prefix.
InitialState = Maximized!
Supported - sometimes required depending on context
InitialState = wdWindowStateMaximize
InitialState = wdWindowState.wdWindowStateMaximize
Convert from number Not supported Supported using typecast
(wdWindowState)1
Convert to number Not supported
Long(AnyValue!) returns 0
Supported using typecast
(int)InitialState
Convert to string Not supported
String(AnyValue!) returns ""
Supported using ToString
InitialState.ToString()

 

Emulating C# Enumerations in PowerScript

In C# you create an enumeration using this type of syntax:

C# enumeration for LogLevel
enum LogLevel { Info=0, Warning=1, Error=2, FatalError=3, DebugTrace=128 }

 

PowerScript has no similar type definition syntax. However, by creating a separate nonvisualobject class where you define each enumerated value as a constant within the Instance Variable pane, you obtain an enumeration-like​ class that you can reference with syntax similar to C#. What you lack in PowerScript is compiler enforcement of only using the values named as constants and the type conversion functionality.

You can prefix your constant name with an underscore if the name itself is invalid due to leading digit or reserved word.

PowerScript enumeration for LogLevel
Object Type nonvisualobject
Class Name LogLevel
Instance Variables PUBLIC:
constant long Info = 0
constant long Warning = 1
constant long _Error = 2
constant long FatalError = 4
constant long DebugTrace = 128

Note: _Error used as constant name since Error conflicts with global Error object.

 

Accessing Enumerated Values

Access to the enumerated follows different rules in PowerScript than in C#.

  • PowerScript
    • You must prefix the enumerated value by its type. EX: LogLevel.FatalError
    • You can use explicit numeric values instead of the named constants because the actual datatype is numeric. EX: 4
  • C#
    • In some contexts you can reference the enumerated value without prefix. EX: FatalError
    • You can always access the enumerated value using type prefix. EX: LogLevel.FatalError
    • You must typecast to the enumerated type if you use explicit numeric value. EX: (LogLevel)4
Language Allowed Syntax
Example - Access Enumerated Value in PowerScript vs. C#
PowerScript LogToDatabase(4, 'Exception ...')
LogToDatabase(LogLevel.FatalError, 'Exception ...')
C# LogToDatabase((LogLevel)4, 'Exception ...'));
LogToDatabase(FatalError, 'Exception ...'));
LogToDatabase(LogLevel.FatalError, 'Exception ...'));

 

Enumerations Exposed by Microsoft Office

The APIs for Microsoft Word, Excel, and the other Office applications, expose many enumerations. It is common across these enumerations that all or most values share a common prefix - and that prefix is often the same as the enumeration type or a shorthand for it.

Example: Enumeration = wdWindowState contains the values wdWindowStateMaximize, wdWindowStateMinimize, and wdWindowStateNormal

For brevity you may want to remove the prefix from the values when you define the enumeration in PowerScript since you must prefix the value by the enumeration type.

Enumeration Type C# Value Using Full Names Using Abbreviated Names
Examples - Abbreviating Enumerated Value Names in PowerScript
wdWindowState wdWindowStateMaximize wdWindowState.wdWindowStateMaximize wdWindowState.Maximize
wdGoToDirection wdGoToFirst wdGoToDirection.wdGoToFirst wdGoToDirection._First
wdDateLanguage wdDateLanguageLatin wdDateLanguage.wdDateLanguageLatin wdDateLanguage.Latin

Note: wdGoToFirst is abbreviated to _First because First without the underscore is a reserved word.

 

Conclusion

Personally, I like using C# style enumerations in PowerScript. I use the technique described above using a separate nonvisualobject class for each enumeration type.

Also, I prefer abbreviating the enumerated constants when I create enumeration types for the Microsoft Office APIs. Even when this results in an underscore prefix to create a valid identifier.

Comments (0)
There are no comments posted here yet

Find Articles by Tag

Stored Procedure Expression RichTextEdit Control WebBrowser GhostScript Service JSONParser WinAPI DLL PBNI Import JSON Database Painter Git ActiveX Import API DataWindow Debugger Error PowerServer Web REST PBVM Messagging SnapObjects Menu .NET Std Framework Database Connection DataWindow JSON CrypterObject Encoding RibbonBar Builder Graph Linux OS SQL Server Event Handling InfoMaker SQL .NET Assembly DevOps SVN Bug Automated Testing Oracle Installation UI Modernization CI/CD Interface ODBC UI Database Application TLS/SSL Platform Performance C# Database Table Schema Database Table Authentication PowerBuilder (Appeon) PowerScript (PS) 64-bit PowerBuilder Debugging File Source Control RESTClient Testing Script DataType Data Charts Filter Debug Database Object 32-bit NativePDF Sort Deployment IDE SDK Migration Array JSONGenerator RibbonBar Resize PDF Syntax .NET DataStore Repository Windows OS Mobile Authorization Open Source BLOB Export OAuth COM UI Themes Database Table Data Event Handler iOS Visual Studio SOAP Azure Text PostgreSQL Windows 10 Icons Design PFC XML Outlook Window OLE Source Code License Export JSON CoderObject Excel Branch & Merge PBDOM Jenkins PowerServer Mobile SqlExecutor Validation Configuration PDFlib Web API Android Model PostgreSQL ODBC driver Web Service Proxy OAuth 2.0 DragDrop Elevate Conference Class Variable Encryption Icon JSON TortoiseGit OrcaScript Database Profile MessageBox SnapDevelop External Functions Transaction HTTPClient PowerBuilder Compiler Event TreeView SqlModelMapper TFS Trial