eugeneK
eugeneK

Reputation: 11116

Passing dynamic parameter to similar function

I have a function that runs other functions ( inner functions ). So each inner function is step of main function. I have to update progress each time inner function end to run. Each inner function accepts current progress as parameter and i do that manually.

Problem is that i have about 10 main function while number of inner function of each main function changes.

Is there a way to introduce dynamic value of some kind in this flow ?

Example:

public void Run() {
DoWork1(33);
DoWork2(66);
Dowork3(100);
}

public void Run() {
DoWork1(25);
DoWork4(50);
Dowork5(75);
Dowork2(100);
}

Upvotes: 1

Views: 197

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500525

It's not really clear what you mean, but perhaps you want:

public void Run(params Action<int>[] actions)
{      
    for (int i = 0; i < actions.Length; i++)
    {
        Action<int> action = actions[i];
        int progress = ((i + 1) * 100) / actions.Length;
        action(progress);
    }
}

Then call it with:

Run(DoWork1, DoWork2, DoWork3);

Run(DoWork1, DoWork4, DoWork5, DoWork2);

Upvotes: 6

Related Questions