Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何更新列表的项目值?_C#_List - Fatal编程技术网

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
字段的同一类中的静态方法?