开发者

Same function with different action depending on caller

开发者 https://www.devze.com 2023-03-28 07:13 出处:网络
I\'m not sure if what I\'m asking about is possible, so bear with me. Currently I have multiple functions, each with the same loop bit and different action.

I'm not sure if what I'm asking about is possible, so bear with me.

Currently I have multiple functions, each with the same loop bit and different action.

I would like a function who's main purpose is to loop through certain entries in a list and have different action based on the caller.

One solution I though of is to have a switch/if block and a unique variable from the caller to determine the action. However, I was开发者_如何转开发 more thinking along the lines of calling the function and some how passing in the action it should take on each entry.


Perhaps your function could take an Action<T> as a parameter, and each caller can provide its own action as an argument?

public void LoopOperation(Action<MyType> action)
{
    foreach(MyType myObject in myCollection)
    {
         action(myObject);
    }
}

You could call it like the following:

LoopOperation(o => Console.WriteLine(o.ToString()));

Or perhaps like the following:

LoopOperation(o => o.SaveToDatabase());

Or with any other operation you want to perform.


You could use some form of a lamba (predicate) where you could do something like

public void dosomethingfunciton(Func<YourType, ResultType> func)
{
     foreach(vat item in List)
     {
        var result = func(item);
     }

}


I thought about answering this question with the 'use a delegate' response that you seem to be reaching for... but then I thought to myself.. why? It's overly complex, especially if you're never going to reuse the 'action' code in some other routine.

So, I'm going to say something along the lines of KISS:

enum MyActions
{
   Action1,
   Action2,
   Action3
}

public DoStuff(MyActions theAction, Itemlist theList)
{
   foreach (SomeItem in theList)
   {
       // do any common stuff

       switch (theAction)
       {
           case Action1:
              //Action 1 specific stuff
              break;
           case Action2:
              //Action 2 specific stuff
              break;
           case Action3:
              //Action 3 specific stuff
              break;
       }
   }
}

I know... basic.... but simple, and does the job.


You could simply add a parameter to the function that would be used to determine which action has to be taken.

public enum CallerType {
    RedCaller, GreenCaller, BlueCaller
}

public MyResultType MyFunction(CallerType callerType)
{
    switch (callerType) {
        case CallerType.RedCaller:
            DoRedCallerStuff();
            break;
        case CallerType.GreenCaller:
            DoGreenCallerStuff();
            break;
        case CallerType.BlueCaller:
            DoBlueCallerStuff();
            break;
        default:
            DoDefaultStuff();
            break;
    }
}


You can pass an action to the function. Look at the delegates Action, Action<T>, and so on to see some examples on how to pass "actions" to a function.


For a slightly alternative strategy, you could achieve this with interitance: eg

public abstract class BaseLooper<TItem>
{
    public void DoLoop (List<TItem> list)
    {
        foreach (TItem item in list)
        {
            DoLoopAction(item);
        }
    }

    protected abstract void DoLoopAction(TItem item);
}

public class BlueLooper
    : BaseLooper<BlueClass>
{
    protected overrides DoLoopAction (BlueClass item)
    {
        ...Do Stuff
    }
}

Note: I've not run this so there's probably the odd error, but I'm sure you get the idea!

The benefits of this are that the actions performed in the loop can be really messy - other calls, properties on the child class to be brought in etc etc.

0

精彩评论

暂无评论...
验证码 换一张
取 消