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是迭代变量,无法修改其成员
   }
}怎样修改这个泛型中的元素值呢?

解决方案 »

  1.   

    用 while 语句替换 foreach.
      

  2.   


    for(int i = 0;i < DBs.Length ; i++)
    {
     
    }
      

  3.   

    METER DB = new METER(); //struct 说明METER是一个结构,是一个值类型。
    建议把结构换成类来做方便点,否则实现起来比较麻烦
      

  4.   

    METER DB = new METER(); //struct 
    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
      

  5.   

    把METER从struct换成class后,再用for循环改值就可以了
      

  6.   


    肯定不行呀,你只是修改了item的值,集合中的没有修改呀
      

  7.   


    那就按下面方法试试:
    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就是修改后的泛型
      

  8.   


            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);
    这样吗?
      

  9.   

    谨慎使用struct。最好从来不用。
      

  10.   


                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++;  
                }
      

  11.   

    学习了:[1]://提示 item是迭代变量,无法修改其成员 
    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,也不能直接去改变字段了。
    当然还有一些别的细节,不改第一个,改第二个行不行。
      

  12.   

    struct这个东西是不好,你在改其值很烦,最好能不用就不要用
      

  13.   

    进一步深入理解struct的用法,可以看这里:《创建常量、原子性的值类型》
    http://www.cnblogs.com/JimmyZhang/archive/2008/05/30/1210376.html