none
有关C#中List<T>方面的问题,期待老师们帮助解决。 RRS feed

  • 问题

  • 编程环境为:VC#2010+Access2010。编程中定义了如下类:

    public class  ZlGcl_XtCs

    {

    public string    Mc;

          public int      GsGjx;                                                                     

          public int      GjxZ;                                                                      

          public int[]     GjxG = new int[101];                                                         

          public int[]     GjxC = new int[101];                                                       

          public double[]  GjxSl = new double[101];                                                    

          public int       GsGjxF;                                                                    

          public int       GjxFZ;                                                                     

          public int[]      GjxFG = new int[101];                                                       

          public int[]      GjxFC = new int[101];                                                      

          public double[]   GjxFSl = new double[101];                                                  

          public double[]   PGJ1 = new double[6];                                                      

          public double[]   PGJ2 = new double[6];                                                      

          public double    Lwg;                                                                      

          public double    Qtgj;                                                                     

          public double    Gb;                                                                    

          public int       Tlx;                                                                     

          public double[]  TSL = new double[6];                                                   

          public double[]  BwgSl = new double[12];                                                 

          public double[]  TpgSl = new double[12];                                               

          public long[]    BbwgSl = new long[4];                                                     

          public long[]    OvmSl = new long[30];                                                         

          public double[]  BmSl = new double[4];                                                     

          public long      Ygm;                                                                       

          public int       TFZMS;                                                                     

          public double[]   FzmsSl = new double[5];                                                     

          public int        PzTlx;                                                                     

          public double[]   QmPz = new double[3];                                                        

     }       

     

    public static List<ZlGcl_XtCs> Data_ZlGcl = new List<ZlGcl_XtCs>();

     

    程序片段:

     

    for (j = 1; j < 5; j++)

    {

        ZlGcl_XtCs  zlgcl = Data_ZlGcl[j];                 // Data_ZlGcl[j]已在前面使用Add进行了赋值处理

        dr.Read();                                         //Access2010数据库读取数据

        zlgcl.PzTlx = Convert.ToInt16(dr["数据"]);         //"数据"Access2010数据库字段名

        dr.Read();

        zlgcl.QmPz[0] = Convert.ToDouble(dr["数据"]);

        dr.Read();

        zlgcl.QmPz[1] = Convert.ToDouble(dr["数据"]);

        dr.Read();

        zlgcl.QmPz[2] = Convert.ToDouble(dr["数据"]);

        Data_ZlGcl[j] = zlgcl;                   

    }     

     

    现在奇怪的是:当循环体只一次时(即j=1,或者该语句片段不在循环体内),Data_ZlGcl[j] = zlgcl; 语句不会出现异常。当循环次数超过二次以上时,则最后一次循环时的zlgcl值语句会把前面几次的Data_ZlGcl[j] 值都赋予与末次zlgcl值相同。 我初次使用C#语言编程,想了又想,一直没找出原因。请哪位路过老师见到了给予指点,不胜感激!


    guo302
    • 已编辑 guo302 2011年9月30日 14:18
    2011年9月30日 7:57

答案

  • 您可以试一下这种方法。

       List<ZlGcl_XtCs> lca = new List<ZlGcl_XtCs>();
                ZlGcl_XtCs[] clsss = new ZlGcl_XtCs[5];//初始化的大小
                int i = 0;
                while (dr.Read())
                {
                    clsss[i] = new ZlGcl_XtCs();
                    clsss[i].PzTlx= = Convert.ToInt16(dr["数据"]);  
                    lca.Add(clsss[i]);
                    i++;
                }
    


    Best Regards,
    Rocky Yue[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 guo302 2011年10月3日 10:27
    2011年10月3日 4:45
    版主

全部回复

  • j=1开始?故意的吗?Data_ZlGcl[0]已经特殊处理过了?

     

    对于你的问题我是这样理解的:

    假设循环3次,第二次循环后数据是

    Data_ZlGcl[1].PzTlx=1

    Data_ZlGcl[1].QmPz={1,2,3}

    Data_ZlGcl[2].PzTlx=2

    Data_ZlGcl[2].QmPz={0,1,2}

    第三次循环后,

    Data_ZlGcl[1].PzTlx=3

    Data_ZlGcl[1].QmPz={5,5,5}

    Data_ZlGcl[2].PzTlx=3

    Data_ZlGcl[2].QmPz={5,5,5}

    Data_ZlGcl[3].PzTlx=3

    Data_ZlGcl[3].QmPz={5,5,5}

    即之前的数据都被覆盖了。

     

    你可以试试不用 zlgc变量,直接 Data_ZlGcl[j].QmPz=×××这样来操作,看看有没有问题。

    2011年10月1日 13:39
  • dear

    因为你的执行个体被你改掉了,用等号并不会直接建立新的执行个体

    请参考

    http://www.dotblogs.com.tw/yc421206/archive/2011/06/17/28785.aspx


    秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/
    2011年10月1日 16:16
  • 谢谢“爱让一切都对了”和余小章俩先生在百忙之中予以解答,但“爱让一切都对了”方法肯定不行。余老师所言错误应该是对的,由于我是初次涉及C#环境,困惑之处就是具体如何实现。还请老师指点啊。


    guo302
    2011年10月3日 3:55
  • 您可以试一下这种方法。

       List<ZlGcl_XtCs> lca = new List<ZlGcl_XtCs>();
                ZlGcl_XtCs[] clsss = new ZlGcl_XtCs[5];//初始化的大小
                int i = 0;
                while (dr.Read())
                {
                    clsss[i] = new ZlGcl_XtCs();
                    clsss[i].PzTlx= = Convert.ToInt16(dr["数据"]);  
                    lca.Add(clsss[i]);
                    i++;
                }
    


    Best Regards,
    Rocky Yue[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 guo302 2011年10月3日 10:27
    2011年10月3日 4:45
    版主
  • Tank You,Mr. Rocky Yue!OK!


    guo302
    2011年10月3日 10:32
  • Tank You,Mr. Rocky Yue!OK!


    guo302

    You are welcome.
    Best Regards,
    Rocky Yue[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年10月4日 1:24
    版主