I created the following class:
namespace Prototype.ViewModel.MyVM
{
public clas TheVm
{
List<Tuple<string, bool>> list = new List<Tuple<string, bool>>();
public List<Tuple<string, bool>> List
{
get { return this.list; }
set { this.list = value; }
}
}
}
In another code file, I'm trying modify one of the values of the encapsulated List> object:
for (int i = 0; i < anotherList.Count; i++)
{
TheVM.List[i].Item2 = (anotherList[i].Item2 == 1);
}
But I get the following error message:
Property or indexer 'Tuple.Item2' cannot be assigned to “--” it is read only.
How can I solve that?
You will need to create a new Tuple as they are immutable:
for (int i = 0; i < anotherList.Count; i++)
{
TheVM.List[i] = new Tuple<string, bool>(TheVM.List[i].Item1, anotherList[i].Item2 == 1);
}
This being said, I would recommend against using Tuples for view models.
If you need to change one part of the tuple after it is created, you don't need Tuple, just create you own class:
public class MyTuple
{
public MyTuple(string item1, bool item2)
{
Item1 = item1;
Item2 = item2;
}
public string Item1 {get;set;}
public bool Item2 {get;set;}
}
After that you could define your List as :
public List<MyTuple>> List
and will be able to change Item1/Item2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With