0

I want to send along several integers with my function through a list like this:

public string createCompleteJump(List<int>kID)

kID is they key ID which is connected to several dictionaries hence why it is important to select different integers.

function:

public string createCompleteJump(List<int>kID)
{
    // List<int> kID = new List<int>(); // Key ID
    double vID = 3; // Value ID
    string v2ID = "Framåt";
    var myKey = qtyScrews[kID[0]].FirstOrDefault(y => y == vID);
    var myKey2 = qtyFlips[kID[1]];
    var myKey3 = jumpCombination[kID[2]].FirstOrDefault(y => y == v2ID);
    var myKey4 = jumpHeight[kID[3]];
    var myKey5 = startPos[kID[4]];

    var str = $"{myKey}{myKey2}{myKey3}{myKey4}{myKey5}";
    Console.Write("\nCompleteJump:" + str);
    return str;
}

How would I send along 5 arguments when calling the function through a list? Eg:

public string createCompleteJump(3,4,2,3,4)
Maksim Simkin
  • 9,561
  • 4
  • 36
  • 49
Joel
  • 5,732
  • 4
  • 37
  • 65
  • Possible duplicate of [Why use the params keyword?](http://stackoverflow.com/questions/7580277/why-use-the-params-keyword) – Keith Hall Feb 06 '17 at 10:41

3 Answers3

2

You either need to create a list to pass as argument:

createCompleteJump(new List<int> {3,4,2,3,4});

or you could change your method to accept an undefined number of arguments:

public string createCompleteJump(params int[] kID)
{ /* ... */ }

The params key word states that there can be an arbitrary number of int and that they are combined into an array:

createCompleteJump(3,4,2,3,4);

results in an int[] array containing the specified values.

René Vogt
  • 43,056
  • 14
  • 77
  • 99
1

use params keyword:

public string createCompleteJump(params int[] kID)

https://msdn.microsoft.com/library/w5zay9db.aspx

Maksim Simkin
  • 9,561
  • 4
  • 36
  • 49
0

You can use params or you can instantiate your list to be sent to your function, like this:

List<int> keyIDList = new List<int> {3,4,2,3,4};
createCompleteJump(keyIDList)
dumdum
  • 31
  • 5