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

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