日期:2014-05-18  浏览次数:20494 次

关于对象生存期,进入foreach()里面就不能用了
下面的代码里面的itemr对象在进入foreach后就不能用了,提示要用new声明。请问怎么解决?
C# code

        ListItem itemr = new ListItem();
        foreach (string s in ArrRt)
        {
            itemr.Text = s;//<-这里出错!
}



------解决方案--------------------
这代码也少了点
------解决方案--------------------
ListItem itemr ;
foreach (string s in ArrRt)
{
itemr = new ListItem();
itemr.Text = s;//<-这里出错!
}
------解决方案--------------------
问题和你贴出来的代码没关系...