Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloaded function with params string[] in the signature

Tags:

c#

.net

c#-4.0

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.

like image 634
user3587624 Avatar asked Mar 14 '17 04:03

user3587624


People also ask

What is function signature in function overloading?

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.

Can you overload functions in TypeScript?

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.

When to use function overloading TypeScript?

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.

What is function overloading with example in C++?

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.


2 Answers

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

like image 166
Usman Avatar answered Nov 14 '22 04:11

Usman


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.

like image 30
CodingYoshi Avatar answered Nov 14 '22 05:11

CodingYoshi