The CreatorCon Call for Content is officially open! Get started here.

Method overloading check

abhijats
Tera Expert

Hi Everyone,

Last time my manager asked me to check if system support Method Overloading concept and I created a business rule with two Methods with different parameters, then I called 1st method in another business rule. but the result was not in support of Method Overloading concept. Everytime the last method was getting executing although I had created those methods with different parameters and also tried to call 1st method by its parameter only. So does that mean in SNC environment I can't use Method Overloading thing or I had performed that exercise in a wrong way. Please provide your opinion on the same, If have any sample program that will be better.

15 REPLIES 15

DaleHurtt
Kilo Contributor

First, I assume you are trying a script like this:


function Foo (a, b) { ... }



function Foo (a, b, c) { ... }


This does not work with JavaScript because "foo" is a property of the context you are working (with a global variable in a browser, it is "window.foo"). So your second definition of "foo" overwrites the first.

What JavaScript does allow, however, is dynamically determining passed parameters at runtime. For example:


function Foo (a, b, c) {
this.a = a || "";
this.b = b || "";
this.c = c || 0;
}


This is fine as long as each method uses a different number of parameters and the first argument is always the same, such as the pseudo equivalent:


void Foo (string a, string b) { ... }

void Foo (string a, string b, integer c) { ... }


If you want to do something more complex, like:


void Foo (string a) { ... }

void Foo (array x) { ... }


For that, where the parameter in any given position can change, you need to dynamically sniff out each variable type. The simplest way to handle a method (function) in JavaScript that can take a variable number of arguments is to pass a JSON object.


function Foo (x) {
if (x) {
this.a = x.a || "";
this.b = x.b || "";
this.c = x.c || 0;
}
else {
this.a = "";
this.b = "";
this.c = 0;
}
}

var x1 = new Foo ({c:7});
var x2 = new Foo ({b:"Hello"});


Hope that helps.


abhijats
Tera Expert

Hi Dale,

How do you know that I was trying like:

function Foo (a, b) { ... }

function Foo (a, b, c) { ... }


DaleHurtt
Kilo Contributor

I don't know; I was assuming you were, based upon your statement that you were trying the "Method Overloading concept and I created a business rule with two Methods with different parameters". Is that what you were trying to do?

Does the above help?


abhijats
Tera Expert

I am testing the code, will let you know the result in a short while.