无法向命名元组列表添加值
Not able to add value to List of named tuple
我有一个命名元组的命名列表,来自这个函数OnEndCircularReferencesCalculation()
我正在尝试向命名元组添加值,但收到如下错误消息:'Index was out of range. Must be non-negative and less than the size of the collection. Arg_ParamName_Name'
请告诉我代码哪里出错了。我需要更改哪一行?谢谢
查看我的代码:
private class CustomCalculationService : ICustomCalculationService
{
private List<(string sheet, int rowindex, int columnindex)> _ListOfCells;
public List<string>? Sheets { get; set; }
public List<(string sheet, int rowindex, int columnindex)>? ListOfCells { get { return _ListOfCells; } }
public void OnEndCircularReferencesCalculation(IList<CellKey> cellKeys)
{
if (cellKeys.Count > 0)
{
if (_ListOfCells == null)
_ListOfCells = new List<(string sheet, int rowindex, int columnindex)>();
for (int r = 0; r <= cellKeys.Count - 1; r++)
{
_ListOfCells.Add((Sheets[cellKeys[r].SheetId].ToString(), cellKeys[r].RowIndex, cellKeys[r].ColumnIndex));
}
}
}
}
如果您在表格中没有项目,或者 'cellKeys[r].SheetId' > Sheets.Count 您将收到此错误。
'Index was out of range. Must be non-negative and less than the size
of the collection. Arg_ParamName_Name
顺便说一句,您的代码可以更简洁:
private class CustomCalculationService : ICustomCalculationService
{
public List<string>? Sheets { get; set; }
public List<(string sheet, int rowindex, int columnindex)>? ListOfCells { get; } = new();
public void OnEndCircularReferencesCalculation(IList<CellKey> cellKeys) =>
ListOfCells.AddRange(cellKeys.Select(ck => (Sheets[ck.SheetId], ck.RowIndex, ck.ColumnIndex)));
}
- 您不需要对从列表中得到的字符串进行 ToString
- 在制作列表的同时制作列表 class 意味着您可以使用自动道具
- LINQ 可以将您的 cellkey 列表变成元组,AddRange 可以将它全部消耗掉
我有一个命名元组的命名列表,来自这个函数OnEndCircularReferencesCalculation()
我正在尝试向命名元组添加值,但收到如下错误消息:'Index was out of range. Must be non-negative and less than the size of the collection. Arg_ParamName_Name'
请告诉我代码哪里出错了。我需要更改哪一行?谢谢
查看我的代码:
private class CustomCalculationService : ICustomCalculationService
{
private List<(string sheet, int rowindex, int columnindex)> _ListOfCells;
public List<string>? Sheets { get; set; }
public List<(string sheet, int rowindex, int columnindex)>? ListOfCells { get { return _ListOfCells; } }
public void OnEndCircularReferencesCalculation(IList<CellKey> cellKeys)
{
if (cellKeys.Count > 0)
{
if (_ListOfCells == null)
_ListOfCells = new List<(string sheet, int rowindex, int columnindex)>();
for (int r = 0; r <= cellKeys.Count - 1; r++)
{
_ListOfCells.Add((Sheets[cellKeys[r].SheetId].ToString(), cellKeys[r].RowIndex, cellKeys[r].ColumnIndex));
}
}
}
}
如果您在表格中没有项目,或者 'cellKeys[r].SheetId' > Sheets.Count 您将收到此错误。
'Index was out of range. Must be non-negative and less than the size of the collection. Arg_ParamName_Name
顺便说一句,您的代码可以更简洁:
private class CustomCalculationService : ICustomCalculationService
{
public List<string>? Sheets { get; set; }
public List<(string sheet, int rowindex, int columnindex)>? ListOfCells { get; } = new();
public void OnEndCircularReferencesCalculation(IList<CellKey> cellKeys) =>
ListOfCells.AddRange(cellKeys.Select(ck => (Sheets[ck.SheetId], ck.RowIndex, ck.ColumnIndex)));
}
- 您不需要对从列表中得到的字符串进行 ToString
- 在制作列表的同时制作列表 class 意味着您可以使用自动道具
- LINQ 可以将您的 cellkey 列表变成元组,AddRange 可以将它全部消耗掉