PowerScript Syntax for .NET Calls

When you make calls to .NET assemblies or their methods or properties from PowerBuilder, you must follow PowerScript syntax rules. The following syntax rules are especially important for C# developers to keep in mind:

Instantiating a class

To instantiate a class, use “create”, not “new”. Even when you are referencing a .NET type in a .NET conditional block, you must use the PowerScript create syntax. The following line instantiates a .NET type from the logger namespace:
ls = create logger.LogServer

Note that a single dot (.) is used as a namespace separator in .NET conditional blocks.

Compound statements

You must use PowerScript syntax for compound statements, such as “if”, “for”, or “switch”. The preprocessors for .NET applications signal an error if C# compound statements are used. For example, you cannot use the following C# statement, even inside a .NET conditional block: for (int I=0;I<10;I++). The following script shows the PowerScript equivalent, with looping calls to the .NET WriteLine method, inside a PBDOTNET conditional block:
#IF Defined PBDOTNET THEN
  int i
  for I = 1 to 10
     System.Console.WriteLine(i)
  next
#END IF

PowerScript keywords

The .NET Framework uses certain PowerBuilder keywords such as “System” and “type”. To distinguish the .NET Framework usage from the PowerBuilder keyword, you can prepend the @ symbol. For example, you can instantiate a class in the .NET System namespace as follows:
#IF Defined PBDOTNET THEN
   @System.Collections.ArrayList myList
   myList = create @System.Collections.ArrayList
#END IF

The PowerBuilder preprocessor includes logic to distinguish the .NET System namespace from the PowerBuilder System keyword, therefore the use of the @ prefix is optional as a namespace identifier in the above example. However, you must include the @ identifier when you reference the .NET Type class in PowerScript code (@System.@Type or System.@Type). Also, if you use a PowerBuilder keyword for a .NET namespace name other than System, you must prefix the namespace name with the @ identifier.

Although PowerBuilder can support .NET Framework classes and namespaces, it does not support .NET keywords. For example, you cannot use the .NET keyword typeof, even if you prepend it with the @ identifier.

Line continuation and termination

You must use PowerScript rules when your script extends beyond a single line. The line return character indicates the end of a line of script except when it is preceded by the ampersand (&) character. Semicolons are not used to indicate the end of a PowerScript line.

Rules for arrays

To declare an array, use square brackets after the variable name, not after the array datatype. You cannot initialize an array before making array index assignments. PowerBuilder provides automatic support for negative index identifiers. (In C#, you can have negative index identifiers only if you use the System.Array.CreateInstance method.) The following example illustrates PowerScript coding for an array that can hold seven index values. The code is included inside a conditional compilation block for the .NET environment:
#IF Defined PBDOTNET THEN
  int myArray[-2 to 5]
  //in C#, you would have to initialize array
  //with code like: int[] myArray = new int[7]
  myArray[-1]=10 //assigning a value to 2nd array index
#END IF

In PowerBuilder, unbounded arrays can have one dimension only. The default start index for all PowerBuilder arrays is 1. The GetValue method on a C# array returns 0 for a default start index identifier, so you would call array_foo.GetValue (0) to return the first element of the array array_foo. However, after a C# array is assigned to a PowerBuilder array, you access the elements of the array with the PowerBuilder index identifier. In this example, you identify the first element in PowerScript as array_foo[1].

Case sensitivity

.NET is case sensitive, but PowerBuilder is not. The .NET Framework does provide a way to treat lowercase and uppercase letters as equivalent, and the PowerBuilder to .NET compiler takes advantage of this feature. However, if the .NET resources you are accessing have or contain names that differ only by the case of their constituent characters, PowerBuilder cannot correctly compile .NET code for these resources.

Cross-language data exchange

Code inside a .NET conditional compilation block is not visible to the main PowerBuilder compiler. If you use variables to hold data from the .NET environment that you want to access from outside the conditional block, you must declare the variables outside the conditional block. Variables you declare outside a .NET conditional block can remain in scope both inside and outside the conditional block.

Declaring enumeration constants

You use a terminal exclamation mark (!) to access enumeration constants in PowerScript. For information about using enumeration constants in the .NET environment, see User-Defined Enumerations.