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

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