0

Requesting examples for how to properly do this: (The reasons for doing it like this are plenty, and in this case there is no better way)

public void customForLoop(CodeThatCaBeExecuted _code1, CodeThatCaBeExecuted _code2)
{
    for(int i = 0; i < number1; i++)
    {
        _code1 //Doing something with i
        for(int j = 0; j < number2; j++)
        {
            _code2 //Doing something with j
        }
    }
}

The situation: I have a list of Player objects. The first for loop loops trough all the players in the list. The 2nd for loop runs trough a list of information about the selected player. I have to do this several times trough the code, so I would like to be able to fill in the repeating code in the function and call the method with the required additional code.

In this case _code1 is for example: if(something) do something;

In short: I want to execute an argument.

More complex explaination by request: I want to make a function that contains code i need in every case. I also want t be able to fill in more code in the form of arguments, because most of the situations in wich i need this method it is slightly diferent (1 or 2 lines). The hole function itself is rather large and complex, and therefore i would like to be able to fill in the minor changes that i need.

I must stress this: I do not want to pass a function as an argument. I want to be able to insert code directly.

nosknut
  • 51
  • 1
  • 4
  • 2
    I am unclear what the question is. – mjwills Jun 15 '17 at 11:05
  • *so i would like to be able to fill in the repeating code in the function and call the method with the required additional code.* Sorry this makes no sense, can you edit using different more descriptive wording – Jeremy Thompson Jun 15 '17 at 11:06
  • Are you asking how to pass "code" as arguments? If so look at the `Func` and `Action` delegates. – juharr Jun 15 '17 at 11:07
  • Func and Action is not what i am loking for – nosknut Jun 15 '17 at 11:18
  • Then what *are* you looking for? To provide "more code in the form of arguments", delegates are what you should use. The most typical ones are `Func<...>` and `Action<...>`. You can also provide objects that you can call methods on. Please elaborate what you want. – Lasse V. Karlsen Jun 15 '17 at 12:14

0 Answers0