gpt4 book ai didi

c# - 从 arrayList 中获取值

转载 作者:行者123 更新时间:2023-11-30 13:27:17 24 4
gpt4 key购买 nike

我正在使用 C#,我正在创建一个名为“importControlKeys”的 ArrayList,它创建良好。但是,我终其一生都想不出一种方法来遍历 arrayList 并挑选出 ArrayList 中的值以供以后的代码使用。

我知道我错过了一些简单的东西,但是从 ArrayList 中提取值的语法是什么。我希望它在我下面的代码中是类似于 importControlKeys[ii].value 的东西,但那是行不通的。

我在这些板上进行了搜索,但找不到确切的解决方案,尽管我确信这很容易。 msot 的解决方案说重写为列表,但我必须相信有一种方法可以从数组列表中获取数据而无需重写为列表

private void button1_Click(object sender, EventArgs e)
{
ArrayList importKeyList = new ArrayList();
List<DataGridViewRow> rows_with_checked_column = new List<DataGridViewRow>();
foreach (DataGridViewRow row in grd1.Rows)
{
if (Convert.ToBoolean(row.Cells[Del2.Name].Value) == true)
{
rows_with_checked_column.Add(row);
importKeyList.Add(row.Cells[colImportControlKey.Name].Value);

//string importKey = grd1.Rows[grd1.SelectedCells[0].RowIndex].Cells[0].Value.ToString();
//grd1.ClearSelection();
//if (DeleteImport(importKey))
// LoadGrid();
}
}
for (int ii = 0; ii < rows_with_checked_column.Count; ii++)
{
//grd1.ClearSelection();
string importKey = importKeyList[ii].value; //ERRORS OUT

if (DeleteImport(importKey))
LoadGrid();

// Do what you want with the check rows
}

}

最佳答案

不确定为什么要使用 ArrayList,但如果需要循环遍历它,可以这样做

如果一个元素不能转换为该类型,您将得到一个 InvalidCastException。在您的情况下,您不能将 boxed int 转换为 string 导致抛出异常。

foreach (object obj in importKeyList ) 
{
string s = (string)obj;
// loop body
}

或者你做一个for循环

for (int intCounter = 0; intCounter < importKeyList.Count; intCounter++)
{
object obj = importKeyList[intCounter];
// Something...
}

关于c# - 从 arrayList 中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13072538/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com