Search code examples
c#methodssharepoint-2010optional-arguments

Call a method after statements have been evaluated


This should be very easy to answer but I'm not even sure how to ask it properly, so I apologize in advance for my n00b-ness. I have struggled to paraphrase it for searches with no luck...

Basically I have a method that takes several arguments as "switches" (set to 0 or 1 by the calling method) and optional strings and uses them to "build" its plan of action. It goes something like this:

public static void Foo(int a, int b, int c, optionalString aa, optionalString, bb, optionalString cc)
{
    if (a == 1)
    { Object1 o1 = Thing.Property1[aa]; }
    if (b == 1)
    { Object2 o2 = Thing.Property2[bb]; }
    if (c == 1)
    { Object3 o3 = Thing.Property3[cc]; }

    Bar(optionalo1, optionalo2, optionalo3); // Edit: I explained this part a little wrong, see below.
}

Edit for clarification: I cannot pass null values to Bar() because it needs to be called only with the properties that were actually set. For example, Foo() is invoked with a, b, and c set like this:

Foo(1, 0, 1, string1, string3) //In this instance I only want the first and third properties set. The strings contain the values I want them set to.
{
    if (a == 1)
    { set this property based on string1 }
    if (b == 1)
    { this one would not be set because b was 0 }
    if (c == 1)
    { set this property based on string3 }

    Bar(property1, property3);
    // In this instance, Bar() must be called with only those two arguments, it cannot contain any null values.

End of edit

So, without using a crapload of nested if() statements or methods for every possible combination of Bar(), is there a way to just call it once all those have been evaluated? Technically the variables haven't been assigned yet, so Bar() is not valid. Alternately, is there a better way to accomplish something like this?

This is for a console app that interacts with SharePoint server object model, if that makes any difference. Thank you very much for your time!


Solution

  • What you need is to convert your code into data. You have some input parameters and you need to perform some action on them.

    Use a dictionary structure defined as Dictionary<Key, Action> where Key = whatever unique value you can create. Then all you have to do in your method is to calculate the key and execute the associated action.

    From your example:

        public static void Foo(int a, int b, int c, optionalString aa, optionalString, bb, optionalString cc)
        {
    Dictionary<int, Action> objectMapper = new Dictionary<int, Action>
            {
                { 0, () => Bar() },
                { 1, () => Bar(Thing.Property1[aa]) },
                { 2, () => Bar(Thing.Property2[bb]) },
                { 4, () => Bar(Thing.Property3[cc]) },
                { 3, () => Bar(Thing.Property1[aa], Thing.Property2[bb]) },
                { 5, () => Bar(Thing.Property1[aa], Thing.Property3[cc]) },
                { 6, () => Bar(Thing.Property2[bb], Thing.Property3[cc]) },
                { 7, () => Bar(Thing.Property1[aa], Thing.Property2[bb], Thing.Property3[cc]) },
            };    
            objectMapper[a & b & c]();
        }
    

    In my example the unique key is simply ANDing the 3 input variables. However, as you see, covering every possibility is pretty tedious, which is why I do not recommend doing exactly this way, but try and rework your Bar method to be more flexible on input parameters.