I need to write a function that will take a variable number of arguments. I read a little about params[], but I don't think that will work in my case. My function needs to take a variable number of ints and then a corresponding bool value for each of them. I have to iterate through each of these combinations and input them into a database. Just looking for someone to point me in the right direction. Thanks.
4 Answers
I would recommend building a struct and then passing those in as params. In my example, your struct represents a score of some kind:
public struct RaceScore
{
public bool FinishedRace;
public int Points;
}
Your method signature would then be:
public void SaveScores(params RaceScore[] scores)
Here's an example of calling SaveScores:
RaceScore score = new RaceScore() { FinishedRace = true, Points = 20 };
RaceScore score2 = new RaceScore() { FinishedRace = false, Points = 15 };
SaveScores(score, score2);
3 Comments
You can do this with params, but the params needs to be some class or struct that holds your int + your bool. KeyValuePair<TKey,TValue> in the base class libraries would work, if you don't want to write your own class or struct.
If you're going to iterate through them, though, I'd recommend using IEnumerable<T> instead, though, as it's much simpler to use.
For example:
public void SaveValues(IEnumerable<KeyValuePair<int,bool>> values)
{
foreach(var pair in values)
{
int intVal = pair.Key;
bool boolVal = pair.Value;
// Do something here...
}
}
The same thing would work with params, ie:
public void SaveValues(params KeyValuePair<int,bool>[] values)
This, though, forces you to make an array. Using IEnumerable<T> will work with an array, but will also work with lists of values, or LINQ query results, etc. This makes generating calling this function easier in many cases.
2 Comments
That is right - it will not work as described. What you could do is to use a struct to join the int and the bool value. Params of structs are working:
public struct IntBoolStruct {
public bool BoolValue;
public int IntValue;
}
public void YourMethod( params IntBoolStruct[] values ) {}
EDIT: Depending of what you want to do, a nullable int might help you, too.
public void YourMethod( params int?[] values ) {}
1 Comment
And another answer: you can do this with a simple object-array and params.
class paramstest {
private void _passALot(params Object[] values) {
System.Console.WriteLine(" [foreach]");
foreach (object o in values) {
System.Console.Write(o.ToString() + ", ");
}
System.Console.WriteLine(System.Environment.NewLine + " [for]");
for (int i = 0; i < values.Length; i += 2) {
int? testval = values[i] as Int32?;
bool? testbool = values[i + 1] as Boolean?;
System.Console.WriteLine(String.Format("Int: {0}, Bool: {1}", testval, testbool));
}
}
public void test() {
_passALot(1, true, 2, false, 3, true, "q", false);
}
}
giving you
[foreach]
1, True, 2, False, 3, True, q, False,
[for]
Int: 1, Bool: True
Int: 2, Bool: False
Int: 3, Bool: True
Int: , Bool: False
Passing a Dictionary<int, bool> or a List<struct> with an appropriate struct would be better though :)