PowerBuilder to C# .NET Migration Tip 2


PowerBuilder Migrations to C#  .NET – Handling Inheritance in C#

This article was created to answer a question on a post written about PowerBuilder inheritance, and dynamic function calls in PowerBuilder. The person asking the question, Jose, asked the following question from the PowerBuilder Tip – Dynamic Functions and Events article.

The question and example about migrating PowerBuilder Inheritance in C# .NET

Hello:

I have a large app. that use Dynamic Object Creation, in my case I have NVO’s that have general functionally and using inheritance I’m able to extend business functionalyty and call it dynamically.. like this:

nvo_base_bc cust_rule

SELECT rule_name
INTO :cust_rule_Name
FROM business_rules_customer WHERE cust_id = :cust_id_var;

cust_rule = CREATE USING cust_rule_Name
cust_rule.Validation()
cust_rule.CustomRule()

… In this Case Validation() and CustomRule() method must be in nvo_base_bc and in the class to be instanciated… the power of this is inheritance… you can call base methods and extended methods from your Dynamic Class.

Well… the point is: Can I do that on C#?

 

Emulating PowerBuilder Inheritance in C# .NET (or other .NET languages)

The short answer to the question asked is YES, you can implement inheritance in C# (.NET), in fact you can implement it in many .NET languages.  You may already know that no matter which .NET language you develop in, your target interpreted code will look similar if not exact.

This means that if you write a simple program in C# .NET that declares a string, then assigns a value, displays it via console program and then exits, the compiled intermediate code created will look pretty much the same as if it were coded in VB.NET, Cobol.NET, etc., so C# doesn’t really matter.

.NET is an object oriented programming language, but there are differences in how it is implemented compared to PowerBuilder. There are also limitations in .NET, in some cases visual inheritance is not possible in .NET like it is in PowerBuilder, one of the negatives of using .NET for a PB developer.

Even though PB.NET (WPF Apps) technically will allow you to inherit visual user objects or windows, in reality it doesn’t work very well except for relatively simple situations. I’m not sure if this is due to PB.NET (12.5) being buggy, or because it is just difficult to support with WPF and XAML.

Implementing Non-Visual Inheritance (NVO’s) from PowerBuilder in the .NET Framework

Using the example code given by Jose in his question we will examine how to incorporate the same functionality in C#.NET. There are at least three ways to implement this functionality in C#.NET and I’ll show the basic inheritance without the “new” keyword.  Using Interfaces is another way to implement this functionality and is probably a better (but more complex) way.

  • Inheritance (with or without the new keyword)
  • Interfaces

Inheritance in .NET compared to PowerBuilder

The .NET Framework is built using the concept of Object Oriented Programming (OOP) which includes inheritance.

Inheritance is the ability to create classes which inherits certain aspects from parent classes. The entire .NET framework is built on this concept with most things being an “object” as a result. Like PowerBuilder there are variable types that are basic and are not “object” based for example in PowerBuilder “string” is not an object, and neither is “string” in .NET, however, String (capitalized) is an object type in .NET and you can use System.String in PowerBuilder.NET if you want to use the String object.

This subject can be a challenging at first to comprehend, but is usually simplified with some examples.

Simple .NET Console Application Inheritance Example

Example creates two NVO’s which inherit from the same base NVO having the same Validation and CustomRule functions.  Note the use of “virtual” in the base class, this allows the function to be overridden in the descendant. You may receive warnings about not using the “new” keyword, this is okay.

** corrected code typo Dec 5, 2013, thank you for the comment Mark!

using System;

namespace Inheritance
{
  class Program
{
    static void Main(string[] args)
{
       nvo_invoice_rule invInvoiceRule = new nvo_invoice_rule();
       nvo_cust_rule invCustomerRule = new nvo_cust_rule();

invInvoiceRule.Validation();
invInvoiceRule.CutomRule(“invoice_id”, 1234);

invCustomerRule.Validation();
invCustomerRule.CutomRule(“customer_id”, 24);
}
}

  class nvo_base_bc

{
      public virtual bool Validation()
{
        Console.WriteLine(“Hello, I am validation for object nvo_base_bc”);
        return true;
}

    public virtual void CutomRule(string custValue, int custKey)
{
        Console.WriteLine(“CustomRule for nvo_base_bc with args {0}, and {1}”,  custValue.ToString(), custKey.ToString());
}
}

  class nvo_invoice_rule : nvo_base_bc
{
public override bool Validation()
{

         // overridden custom version of validation, note that the validation

// on the ancestor does not get fired! IF you wanted to call the ancestor

        // function like call super does in PB, then use base.function name
        Console.WriteLine(“Hello, I am validation for nvo_invoice_rule”);
        return true;
}
      public override void CutomRule(string custValue, int custKey)
{
        Console.WriteLine(“CustomRule for nvo_invoice_rule with args {0}, and {1}”, custValue.ToString(), custKey.ToString());
}
}

  class nvo_cust_rule : nvo_base_bc
{
    public override bool Validation()
{
        // overridden custom version of validation
        Console.WriteLine(“Hello, I am CUSTOM validation in nvo_cust_rule”);
return true;
}
    public override void CutomRule(string custValue, int custKey)
{
          Console.WriteLine(“CustomRule overridden in nvo_cust_rule args {0}, and {1}”, custValue.ToString(), custKey.ToString());
}
}
}

 

What does the above program output?

Inheritance_PowerBuilder_vs_Csharp

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

What if you wanted to call Validation on the base class and not implement it in the nvo_cust_rule?

Modify the class as shown in the print screen, it will create output shown by using the “base” keyword causing .NET to call the base class implementation of the Validation function.

Inheritance_PowerBuilder_vs_Csharp_call_super

 

 

 

 

 

 

 

 

 

 

 

 

 

One more variation on coding, Validation is not specified as overridden but it still runs like it is

Inheritance_PowerBuilder_vs_Csharp_call_super_v2

There is another better way of implementing this behavior in .NET but goes beyond the scope of this article, it is called “Interfaces” and would have been a better choice for this example.

 


Leave a comment

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

2 thoughts on “PowerBuilder to C# .NET Migration Tip

  • Mark

    Hi Rich,
    The console.writelines CustomRule override methods are the same as the one in the base class which makes the output confusing. You may want to change them. Good article though. Keep them coming.
    Thanks,
    Mark L.

    • DisplacedGuy Post author

      Thanks for the heads-up on that typo/mistake. I’m correcting it right away, it is a pain for me to paste code into my blog posts because I manually “codify” them with colors/indentation so that the code looks like it would in a code editor. All this manual work makes it easy to overlook dumb mistakes… thanks again!