积极答复者
有关C#中List<T>方面的问题,期待老师们帮助解决。

问题
-
编程环境为: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
答案
-
您可以试一下这种方法。
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
全部回复
-
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=×××这样来操作,看看有没有问题。
-
dear
因为你的执行个体被你改掉了,用等号并不会直接建立新的执行个体
请参考
http://www.dotblogs.com.tw/yc421206/archive/2011/06/17/28785.aspx
秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/ -
您可以试一下这种方法。
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
-
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.