Overloading Methods with ASNA Visual RPG

Today I’ll be discussing another great feature of the ASNA Visual RPG (AVR) for .NET language. This feature is called method overloading and it provides two great benefits to your coding efforts;

  1. Simplifies complex coding requirements
  2. Enhances code performance

In AVR, method overloading simply means that functions in an AVR class can have the same name but with different parameters passed to it. The name of a function, combined with the set of parameters passed to it, form what is called the signature of the method. Here is an example of an overloaded method called GetItem, with three different signatures;

The AVR.NET compiler will compile all three methods to the intermediate language code as expected. One thing to remember, is that when it comes to overloading a method, the value returned by the method is not considered part of the method signature. As you can see in the second overloaded example of GetItem, it returns a different type from the other two overloaded methods.

Method overloading gives us the ability to provide multiple, unique implementations of the method, while at the same reducing the complexity of the code required to satisfy application requirements. Without the ability to overload the GetItem method, there could only be one implementation of the method. This single implementation would be much more complex, because it would have to perform tests, to determine what kind of argument was passed into each parameter and then branch to the appropriate code, based on the results of each test. All of this testing and branching degrades the runtime performance and efficiency of the method.

Method overloading also provides consumers of the GetItem method with the kind of flexibility and ease-of-use they have come to expect, when interfacing with other .NET classes and methods. The public interface of the ItemDbContext class illustrated above, will expose three methods for retrieving an item object, either with its associated order object or without the order. The consumer of these methods simply has to code the appropriate arguments when invoking the GetItem method and the compiler and common language runtime, will take care of calling the appropriate method. The consumer does not have to be concerned with how these methods are implemented. Their only concern is to satisfy the requirements of the public class interface.

When writing code in Visual Studio that will invoke the GetItem method, the IntelliSense feature of Visual Studio will automatically present each signature of GetItem to the programmer for them to choose from, as illustrated here.

Overloaded Method GetItem Signature 1

Overloaded Method GetItem Signature 1


Overloaded Method GetItem Signature 2

Overloaded Method GetItem Signature 2


Overloaded Method GetItem Signature 3

Overloaded Method GetItem Signature 3


Please feel free to share your thoughts on this post by clicking the leave reply above, or post your comments on Twitter #ASNAVisualRPG.

Below is the complete code for the ItemDbContext class used in the example above. The example application developed for this post, and all of the source code can be downloaded at the following link;

Method Overload Example

The sample database files, which are included with the example application download, can be imported into a local DataGate database on your PC.

2 thoughts on “Overloading Methods with ASNA Visual RPG

  1. Roger Pence

    Method, or subroutine, overloading may look a little foreign to some RPG coders. However, those coders should be reminded that ILE RPG has used method overloading since the mid-90s. Take a look at the ILE RPG docs for how many arguments the %LEN or %SUBST BIFs use. That’s also overloading!

    Reply
    1. Tim Daniels Post author

      Hi Roger. Thanks for relating the subject of this post back to our RPG roots on the IBMi Server. It is always easier to understand a new concept when you can relate to an old concept you are already familiar with.

      Reply

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">