Using .NET Assembly Methods in PB.NET and vice-versa

I noticed a question on StackOverflow.com that asked how make methods of a .NET assembly visible to PowerBuilder 12 (or PowerBuilder 12.5).  Since putting pictures into an answer is kind of a pain I decided to write a quick article on how to do it along with the reverse, or marking methods of a PowerBuilder created .NET Assembly as available to any other .NET application.

.NET Assemblies

Make Methods, Events, Variables Visible in PowerBuilder.Net Programs

 

Use and Assembly that has been compiled using the correct.NET version

Using a .NET Assembly in PowerBuilder.NET is actually pretty simple, there are a few basic requirements. First you need to use a .NET Assembly (dll) of the same .NET version being used in PowerBuilder.

  • PowerBuilder 11 uses .NET 2.0
  • PowerBuilder 12 uses .NET 3.5
  • PowerBuilder 12.5 uses .NET 4.0

Make your methods and classes Public

You must make your methods public for them to be visible outside the .NET Assembly and you need to make the enclosing class public as well.  Also you need to prefix the class with the namespace that the class resides in.

Here is a VERY simple .NET 4.0 program that can be created, added and used in a PB12.5 WPF window in about five minutes time.

1. Create a new .NET Class Library

2. Create a single class (public)

3. Create a single method within the class (public)

4. Compile the assembly and make a note of the location.

The .NET 4.0 code:

using System;
namespace dotNetAssembly
{
 public class DotNetClass
 {
    public double secondsBetween(DateTime time1, DateTime time2)
    {
      double secondsVal;
      secondsVal = time2.Subtract(time1).TotalSeconds;
      if (secondsVal < 0)
      {
         secondsVal = secondsVal * -1;
      }
      return secondsVal;
     }
  }
}

5. Create a quick-and-dirty PB.NET WPF application (example named: PBusingAssembly )

6. Add a Reference To Assembly To PB.NET Application, just browse to the location of your .NET assembly

PB_add_dotnet_reference

 

 

 

 

 

 

 

 

7. Throw together a quick and dirty WPF Window with two date picker controls, a single line edit and a button and add code to the clicked event of the button to use the new dotNetAssembly.dll with the method for computing the number of seconds between two dates.  Notice how the intellisense picks up the method for the .NET class from within PB12.5.

PBNET_Use_Class

 

 

 

 

 

 

 

 

 

8. Finish up the code in the button clicked event.

PBNET_Buttonclicked

 

 

 

 

9. Compile, and run the new PowerBuilder WPF Window Application and try it out.

PBNET_Window

 

PowerBuilder.NET Assemblies

Make Methods, Events, Variables Visible In .NET Programs

If you are interested in the REVERSE, or how to make methods created in a PowerBuilder.NET Assembly available in a .NET program you need to change some settings on your PowerBuilder “Project” object.

 

PowerBuilder.NET Project Object in the Solution Explorer Pane

PowerBuilder.NET Project Object in the Solution Explorer Pane (figure 1)

 

 

  1. First select your ‘Project” object from within the Solution Explorer. (figure 1)
  2. Double click the project object to open the project painter.  The example shown is has a project object named “p_pbnet_handler“.

 

Once the Project Object has been opened then you can select the object and its’ methods, events, properties that you wish to make available to other .NET programs that might be using your PowerBuilder.NET Assembly.

 

  1. Once the project object is opened, select the second tab, or the “Objects” tab.
  2. From the top section (Custom Class Properties) select the object (usually a non-visual) that has methods, events or instance variables you want to be available.
  3. From the bottom section, mark the check box to the left of each function, event, or instance variable that you want to make available to a .NET program that uses the assembly you are building. (figure 2)

 

PB.NET Make Methods Visible to .NET Programs

PB.NET Make Methods Visible to .NET Programs (figure 2)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Note: notice the icon next to the n_pbnet non-visual user object in the top list of Custom Class Objects, the fact that an icon is shown indicates that at least one method, event or instance variable has been selected making this object available externally to other .NET programs.

 

 

 

Tags:

No responses yet

Leave a Reply

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