I have these two functions
public static string Function1 (string id, params string[])
{
return Function1(id, null, null, params)
}
public static string Function1 (string id, string id2, Object a, params string[])
{
string id = id,
if (IsValidId(id))
{
start = new ProcessStartInfo();
start.Arguments = params;
if (string.IsNullOrEmpty(id2)==false)
{
start.RedirectStandardOutput = true;
}
}
}
I want to use the second overload when I do the following call
MyStaticClaass.Function1(
input1,
input2,
null, // (This one represents the Object)
input3,
input4,
input5);
Is there a way I can force it to go to the second definition of the method?
The compilation error I have is: This call is ambiguous between the following methods or properties: (and then the two methods above)
PS: I haven't chose to have these two functions. I can't change their signature or their names.
A function's signature includes the function's name and the number, order and type of its formal parameters. Two overloaded functions must not have the same signature. The return value is not part of a function's signature.
TypeScript provides the concept of function overloading. You can have multiple functions with the same name but different parameter types and return type. However, the number of parameters should be the same.
Function overloading in TypeScript lets you define functions that can be called in multiple ways. Using function overloading requires defining the overload signatures: a set of functions with parameter and return types, but without a body. These signatures indicate how the function should be invoked.
Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading.
You can use Named arguments
to overload a specific function like
Program.Function1(
id: input1,
id2: input2,
o:null,
array: new string[] {input3,
input4,
input5});
and it will hit the function
public static string Function1 (string id, string id2, Object o, params string[] array)
{
}
for more detail you can check this Named Arguments
It can be like this as well:
var result = Rootobject.Function1(
"",
"",
null, // (This one represents the Object)
new string[] { "", "", ""});
However, since you already know this is a fragile design or you wouldn't have ended here asking about it, perhaps you should rethink your overloads. Of course, we got it to work but it is not a good design.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With