METER DB = new METER(); //struct
List<METER> DBs = new List<METER>();
i = 0;
foreach (METER item in DBs)
{
i++;
if (RightStr(item1.REMARK, 2).Equals("A"))
{
item.GROUP_NO = group_no_jx; //提示 item是迭代变量,无法修改其成员
}
}怎样修改这个泛型中的元素值呢?
List<METER> DBs = new List<METER>();
i = 0;
foreach (METER item in DBs)
{
i++;
if (RightStr(item1.REMARK, 2).Equals("A"))
{
item.GROUP_NO = group_no_jx; //提示 item是迭代变量,无法修改其成员
}
}怎样修改这个泛型中的元素值呢?
for(int i = 0;i < DBs.Length ; i++)
{
}
建议把结构换成类来做方便点,否则实现起来比较麻烦
List <METER> DBs = new List <METER>();
i = 0;
for(int i = 0;i < DBs.Length ; i++)
{
METER item = DBs[i];
if (RightStr(item1.REMARK, 2).Equals("A"))
{
item.GROUP_NO = group_no_jx;
}
}黏贴进去试试?foreach 本身就不允许修改其 item
肯定不行呀,你只是修改了item的值,集合中的没有修改呀
那就按下面方法试试:
METER DB = new METER(); //struct
List <METER> DBs = new List <METER>();
List <METER> NewDBs = new List <METER>();
i = 0;
for(int j = 0;j < DBs.Count; j++)
{
i++;
METER item = DBs[j];
if (RightStr(item1.REMARK, 2).Equals("A"))
{
item.GROUP_NO = group_no_jx; //提示 item是迭代变量,无法修改其成员
}
NewDBs.Add(item);
}NewDBs就是修改后的泛型
public struct MyStruct
{
public int Age;
}
private void button1_Click(object sender, EventArgs e)
{
MyStruct[] my = new MyStruct[5]; my[0] = (new MyStruct());
my[1] = (new MyStruct());
my[2] = (new MyStruct());
my[3] = (new MyStruct());
my[4] = (new MyStruct());
for (int i = 0; i < my.Length; i++)
{
my[i].Age = i;
}
string str = "";
foreach (var item in my)
{
str += item.Age.ToString() + "\r\n";
}
MessageBox.Show(str);
这样吗?
int i = 0;
while (i < DBs.Count)
{
if (DBs[i].REMARK.Equals("A"))
{
DB = DBs[i];
DB.GROUP_NO = 66; //赋新值 DBs.Remove(DBs[i]);
DBs.Add(DB);
break;
}
i++;
}
using System;
using System.Text;
using System.Collections.Generic;public struct METER
{
public int GROUP_NO;
}class MyClass
{
static void Main(string[] args)
{
METER DB = new METER(); //struct
List<METER> DBs = new List<METER>();
DBs = new List<METER> { new METER { GROUP_NO = 1 }, new METER { GROUP_NO = 2 }, new METER { GROUP_NO = 3 }, new METER { GROUP_NO = 4 }, new METER { GROUP_NO = 5 }, };
int i = 0;
foreach (METER item in DBs)
{
i++;
item.GROUP_NO = 100;
}
}
}[2]:改class,OK。
using System;
using System.Text;
using System.Collections.Generic;public class METER
{
public int GROUP_NO;
}class MyClass
{
static void Main(string[] args)
{
METER DB = new METER(); //struct
List<METER> DBs = new List<METER>();
DBs = new List<METER> { new METER { GROUP_NO = 1 }, new METER { GROUP_NO = 2 }, new METER { GROUP_NO = 3 }, new METER { GROUP_NO = 4 }, new METER { GROUP_NO = 5 }, };
int i = 0;
foreach (METER item in DBs)
{
i++;
item.GROUP_NO = 100;
}
}
}[3]:无法修改“System.Collections.Generic.List<METER>.this[int]”的返回值,因为它不是变量3
using System;
using System.Text;
using System.Collections.Generic;public struct METER
{
public int GROUP_NO;
}class MyClass
{
static void Main(string[] args)
{
METER DB = new METER(); //struct
List<METER> DBs = new List<METER>();
DBs = new List<METER> { new METER { GROUP_NO = 1 }, new METER { GROUP_NO = 2 }, new METER { GROUP_NO = 3 }, new METER { GROUP_NO = 4 }, new METER { GROUP_NO = 5 }, };
int i = 0;
int length = DBs.Count;
for (int j = 0; j < length; j++)
{
i++;
DBs[j].GROUP_NO = 100;
}
}
}总结:
[1]class中没有任何限制
[2]struct不能在循环中随意修改其值。其实[2]的原因,现在想来也很好理解。
《大家一起来找碴,这两个结构体,有什么不同。》第23楼:
http://topic.csdn.net/u/20090824/12/67d3acfd-a6e5-4ebe-8261-c16fd28a4d0d.html因为:
[1]引用型的hashcode是一个引用地址
[2]结构型的hashcode是结构的第一个字段
for/foreach是通过接口来引用hashcode
所以在{}中不能去改变其hashcode,也不能直接去改变字段了。
当然还有一些别的细节,不改第一个,改第二个行不行。
http://www.cnblogs.com/JimmyZhang/archive/2008/05/30/1210376.html