I am just passing a ValueTuple to a function. I want to process the Values within this ValueTuple.
Unfortunately, VS 2017 only allows me to access credit.Item1
. No further Items. So far I had no issues with ValueTuples.
The error in the compiler is:
ValueTuple<(string loanID, decimal y, ...)> does not contain a definition for 'loanID'...
The code is
public void LogCredit(
ValueTuple<(
string loanID,
decimal discount,
decimal interestRate,
decimal realReturn,
decimal term,
int alreadyPayedMonths)>
credit)
{
// not working!
string loanID = credit.loanID;
// this is the only thing i can do:
string loanID = credit.Item1;
// not working either!
decimal realReturn = credit.Item2;
}
Meanwhile, when hovering over credit
I can see it correctly:
Any Suggestions?
Your parameter has only one single field Item1
because it is not of type ValueTuple<...>
, but of type ValueTuple<ValueTuple<...>>
: The single type parameter of the outer ValueTuple is another ValueTuple
, where this inner C# tuple now contains your string
, decimal
and int
fields.
Therefore, in your code you have to write string loanID = credit.Item1.loanID;
to access those fields.
In order to access your fields as expected, remove the enclosing ValueTuple
, leaving just the C# tuple
behind:
public void LogCredit((string loanID, decimal discount, decimal interestRate,
decimal realReturn, decimal term, int alreadyPayedMonths) credit)
{
string loanID = credit.loanID;
decimal realReturn = credit.Item4;
}
To utilize named fields of ValueTuple
, I prefer the usage of the C# 7 Tuples
.
For the sake of completeness, here is a general blog article and an in-depth blog article about tuples in C# 7.