Niek de Klein
Niek de Klein

Reputation: 8834

How can I call a function with differing amount of arguments using a loop?

The function WriteStartAttribute can be called with 1, 2 or 3 strings as arguments. The amount I want to call it with is dependent on the amount of arguments in writeInfo. To clarify, I want to do the following using a loop:

if (writeInfo.Count == 2)
{
    writer.WriteStartAttribute(writeInfo[1]);
}
else if (writeInfo.Count == 3)
{
    writer.WriteStartAttribute(writeInfo[1], writeInfo[2]);
}
else if (writeInfo.Count == 4)
{
    writer.WriteStartAttribute(writeInfo[1], writeInfo[2], writeInfo[4]);
}

I tried it using an array (and a List) like this:

for (int i = 0; writeInfo.Count() - 1 < i; i += 1)
{
     argumentList[i] = writeInfo[i + 1];
}
writer.WriteStartAttribute(argumentList);

However, because no overload accepts an array (or a List) this does not work.
How can I call a function with differing amount of arguments using a loop?

Upvotes: 0

Views: 107

Answers (3)

Samy Arous
Samy Arous

Reputation: 6812

hmm, using reflection:

MethodInfo mi = writer.GetType().GetMethod(WriteStartAttribute);
mi.Invoke(instance, argumentList);

The invoke function take an array of argument to be passed to the function. This seems to be what you are expecting.

I'm not a big fan of using reflection where there is a possible manual (longer) way of doing so.

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460288

You are probably looking for the params keyword.

The params keyword lets you specify a method parameter that takes a variable number of arguments.

You can send a comma-separated list of arguments of the type specified in the parameter declaration, or an array of arguments of the specified type. You also can send no arguments.

public void WriteStartAttribute(params string[] list)
{
    for (int i = 0; i < list.Length; i++)
    {
        Console.Write(list[i] + " ");
    }
    Console.WriteLine();
}

// this works:
writer.WriteStartAttribute(writeInfo[1], writeInfo[2], writeInfo[4]);

Upvotes: 2

Mark Brackett
Mark Brackett

Reputation: 85685

Assuming this is the XmlTextWriter.WriteStartAttribute, it doesn't accept params so you'll have to manually handle it.

Create your own params method, and then put your if statements in there:

void WriteStartAttribute(params string[] values) 
{
     if (values.Length > 4 || values.Length < 2) throw new ArgumentException();
     if (values.Count == 2)
     {
        writer.WriteStartAttribute(values[1]);
     }
     else if (values.Count == 3)
     {
        writer.WriteStartAttribute(values[1], values[2]);
     }
     else if (writeInfo.Count == 4)
     {
        writer.WriteStartAttribute(values[1], values[2], values[4]);
     }
  }

Then call that in your loop:

WriteStartAttributes(writeInfo);

Bonus points for making it an extension method:

public static void WriteStartAttribute(this XmlTextWriter writer, params string[] values) 
...

writer.WriteStartAttributes(writeInfo);

Upvotes: 3

Related Questions