How To Leverage .NET Framework Code in PowerBuilder.NET Applications

This article is intended to get you pointed in the right direction when it comes to using .NET Framework code in your PowerBuilder.NET programs.

We will cover the following areas in this article:

  • High level overview of important concepts of the .NET Framework 4 that relate to use in your PowerBuilder.NET
  • Show some examples of using .NET Framework code in PowerBuilder.NET
  • Show how to include specific .NET Assemblies in your PowerBuilder program
  • Learn how PowerBuilder resolves object names when there are duplicates
  • Understand the basic mappings between PowerBuilder and .NET datatypes

Microsoft .NET Framework – Important Concepts

Assemblies and Namespaces are an important concept to grasp at a high level.  These are how you ensure that the proper code is being used and also how you include .NET code in your PowerBuilder applications.  Often times when you want to use .NET code in your PowerBuilder application you will find yourself using the .NET Framework documentation to find out which Assembly and Namespace the code you are interested in resides.

Assemblies

An assembly is a collection of resources and types that make usually form a logical unit of functionality and are stored in an executable or dynamic link library file.

Namespaces

Another way of organizing .NET code is through the use of namespaces. Namespaces are different than assemblies and do not replace them but rather they are a second way to organize type names.  An Assembly may contain many different Type Names and it may also contain additional Namespaces for a finer level of granularity in organizing types.

Simple Example – Using .NET Framework Code in PowerBuilder.NET

I recently came across the need to use GUID’s in a PowerBuilder.NET application, so it made sense to leverage that functionality from the .NET Framewwork.  The first step in using a .NET Framework method is determining which Assembly and Namespace the method lives in.  Next you’ll want to look at the documentation for the method to understand how it works.

Here is the Microsoft documentation for Guid.NewGuid and I’ve marked all of the important areas of interest

PowerBuilder .NET to .NET Assembly and Namespaces

 

 

 

 

 

 

 

 

PowerBuilder PowerScript and .NET Framework Code Used in Harmony

// ———————————————————–
// Creates a new category. Generates a GUID for the unique
// identifier using the .NET Framework and assigns the name
// from the function argument.
//
// Assembly: mscorlib.dll
// Namespace: System
// ———————————————————–

// Not necessary to create an intstance of structure in PB but
// doing so reinforces the fact that structures in PB are equal
// to objects (classes) in .NET and classes get instantiated
// in .NET via the new command.
//
// It is common to hear .NET Developers say that they are going
// to “new-up” an instance of a class. In C#.NET the equivalent
// code uses new in place of create!

Category cat = create Category

// Technique 1 – Fully qualified Guid function and the ToString extension method

cat.catGUID = System.Guid.NewGuid().ToString()

// Technique 2 – uses the PowerScript String command to convert the GUID to string

cat.catGUID = String(System.Guid.NewGuid())

// Technique 3 – Not fully qualified naming when using the Guid .NET method

cat.catGUID = Guid.NewGuid().ToString()

Notes about the PowerBuilder Code:

Technique 3 will NOT compile unless you add the Assembly and Namespace as a “Using” in the object where this code is written.  Before I added the “Using” to my object I would get a compiler error on Guid because PB did not know what Guid was.  Here is how I added the Assembly as a “Using”

  • Go to Declare –> Using on the non-visual user object
  • Click New
  • Browse to Assembly mscorlib and highlight it
  • Highlight System in the Namespace section
  • Click OK

 

 

 

 

 

 

 

 

Your object will show “System” in the Using section like below.  Now you will have to save and then the code in Technique 3 will compile because by adding the proper assembly and namespace PowerBuilder is now able to resolve Guid.

 

 

 

 

 

PB.NET Access Order with Unqualified Names

By declaring and using namespaces, you can qualify objects that would otherwise have the same names. Once declared, you can call these objects without using their qualified names.

PowerBuilder .NET uses the following algorithm to determine which object to call when multiple objects have the same names in a window that belongs to a declared namespace, and that includes a Using directive for a different namespace:

  1. The object with the same namespace as its window or container object.
  2. The object without a namespace.
  3. The object with a different namespace that is included in the Using directive of the window or container object.

Datatype Mappings Between .NET and PB

When you call methods from managed assemblies in PowerScript, you must use PowerBuilder datatypes in any method arguments or return values.

This table shows the mappings between .NET, C#, and PowerBuilder datatypes.  Structures are not listed but they are equivalent to Objects (classes) in .NET.

 
.NET datatype C# datatype PowerBuilder datatype
System.Boolean boolean Boolean
System.Byte Byte Byte
System.Sbyte Sbyte Sbyte
System.Int16 short Int
System.UInt16 ushort Uint
System.Int32 int Long
System.UInt32 uint Ulong
System.Int64 long Longlong
System.UInt64 ulong Unsignedlonglong
System.Single float Real
System.Double Double Double
System.Decimal Decimal Decimal
System.Char Char Char
System.String String String
System.DateTime System.Datetime Datetime

Advanced .NET Concepts Used – Extension Methods (method chaining)

In the above example we used the Guid.NewGuid() method and we “chained” the ToString() method directly after the NewGuid() method.  This is a really cool concept in .NET that is easy to understand when looking at the code, but not as easy to figure out how it is implemented in .NET.

I’ll post a link with some detailed info about the concept in this blog article.

 

Tags:

One response

Leave a Reply to PowerBuilder 12.5 PB.NET Free Training, Tutorials & Videos » The Displaced Guy Cancel reply

Your email address will not be published. Required fields are marked *