C# 如何更新列表的项目值?
我有一张单子。我将所有查询输出放在这里。现在使用 线因此,当工作完成时,需要更新列表项的值。 请参阅下面我的代码: 公开公布名单:C# 如何更新列表的项目值?,c#,list,C#,List,我有一张单子。我将所有查询输出放在这里。现在使用 线因此,当工作完成时,需要更新列表项的值。 请参阅下面我的代码: 公开公布名单: public static List<string[]> OutboxList = new List<string[]>(); 通过线程调用该方法: static void OutboxThreadProcessor(string id,string status) { //predefine value of status is
public static List<string[]> OutboxList = new List<string[]>();
通过线程调用该方法:
static void OutboxThreadProcessor(string id,string status)
{
//predefine value of status is "QUE". Process some work here for that perticular item list.if data process success full then need to update
// the status of the list item
// need to update the perticular value of that list item here.
How i do it???????
//Say for Example 1-> Success
// 2-> Failed
// 3-> Success
}
您需要在数组列表中找到
项[0]
等于id
的项,并将状态设置为项[1]
。你可以用一个循环,像这样
foreach (string[] item in OutboxList) {
if (item[0] == id) {
item[1] = status;
break;
}
}
Thread OutboxThread = new Thread(() => OutboxThreadProcessor(OutBoxFields));
OutboxThread.Start();
或者使用LINQ,如下所示:
var item = OutboxList.FirstOrDefault(a => a[0] == id);
if (item != null) {
item[1] = status;
}
class OutBoxFields {
public string Id {get;set;}
public string Status {get;set;}
... // and so on
}
请注意,您的数据结构不是特别面向对象的。如果将七个字符串
项的数组替换为具有七个字符串字段的类
,则程序的可读性会更高,如下所示:
var item = OutboxList.FirstOrDefault(a => a[0] == id);
if (item != null) {
item[1] = status;
}
class OutBoxFields {
public string Id {get;set;}
public string Status {get;set;}
... // and so on
}
将数组直接传递到线程
,以便完成后可以更新数组
static void OutboxThreadProcessor(string[] OutBoxFields)
{
string id = OutBoxFields[0];
string status = OutBoxFields[1];
//Do work
OutBoxFields[0] = "2";//update the array
OutBoxFields[1] = "something";
}
这样说吧
foreach (string[] item in OutboxList) {
if (item[0] == id) {
item[1] = status;
break;
}
}
Thread OutboxThread = new Thread(() => OutboxThreadProcessor(OutBoxFields));
OutboxThread.Start();
还请注意,在这种情况下,您正在关闭循环,如果您在c#5.0编译器中编译,这很好,否则您需要在循环中使用局部变量。通过线程的静态列表显示错误。您能提供帮助吗?线程OutboxThread=新线程(()=>OutboxThreadProcessor(OutboxList));您正在传递列表变量OutboxList
。看看我的答案,你应该通过OutBoxFields
我也尝试过outboxfiled。但两者都显示错误..线程OutboxThread=新线程(()=>OutboxThreadProcessor(OutBoxFields));列表有任何限制吗?..错误1方法“OutboxThreadProcessor”没有重载需要1个参数。如果有帮助,请不要忘记标记为答案。并考虑用户DasBrink的解决方案将你的代码转换为面向对象,谢谢你的建议。但是,此代码块的另一部分是OutboxThreadProcessor方法。。是否需要任何更新???OutboxThreadProcessor
是否是定义静态OutboxList
字段的同一类中的静态方法?