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

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