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

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