首页 新闻 会员 周边

求高手帮看一个问题.NET问题

0
悬赏园豆:5 [已解决问题] 解决于 2012-08-01 09:45
 1                     alist = new List<ChartMetaData>();
 2                     blist = new List<ChartMetaData>();
 3                     clist = new List<ChartMetaData>();
 4                     dlist = new List<ChartMetaData>();
 5 
 6                     for (DateTime bDateTime = beginDatetime; bDateTime < endDatetime; bDateTime = bDateTime.AddMinutes(Convert.ToDouble(timeInterval)))
 7                     {
 8                         tempData = new ChartMetaData();
 9                         tempData.GetDate = bDateTime;
10                         if (Aitem=="1")
11                             alist.Add(tempData);
12                         if (Bitem == "1")
13                             blist.Add(tempData);
14                         listCount++;
15                     }
16                     
17 
18                     onlinePowerList = createPower.GetList(seeid, beginDatetime, endDatetime);
19                     if (onlinePowerList.Count > 0)
20                     {
21                         double[] pe = createWarnSet.GetBestValue(seeid, WarningType.P);
22                         double[] zpe = createWarnSet.GetBestValue(seeid, WarningType.ZP);
23 
24                         ct = createSee.GetCt(seeid);
25                         pt = createSee.GetPt(seeid);
26                         foreach (OnlinePower model in onlinePowerList)
27                         {
28                             for (int i = 0; i < listCount; i++)
29                             {
30                                 if (Aitem == "1" && alist[i].GetDate==model.GetTime)
31                                 {
32                                     alist[i].Value = model.PA * ct * pt;
33                                     alist[i].StrValue = alist[i].Value.ToString();
34                                     alist[i].MaxValue = pe[1];
35                                     alist[i].MinValue = pe[2];
36                                  }
37 
38                                 if (Bitem == "1" && blist[i].GetDate == model.GetTime)
39                                 {
40                                     blist[i].Value = model.PB * ct * pt;
41                                     blist[i].StrValue = blist[i].Value.ToString();
42                                     blist[i].MaxValue = pe[1];
43                                     blist[i].MinValue = pe[2];
44                                }
45 
46                              }
47                         }
48                     }

 


调试时 当调试到alist[i].Value被赋值为155时,监视一下下面的blist[i].Value值也会等于155 调试到下面给 blist[i].Value赋值为140时,监视一下上面的alist[i].Value的值也跟着变成了140,这个是什么情况,求高手帮忙

赌东道道的主页 赌东道道 | 初学一级 | 园豆:194
提问于:2012-07-31 16:11
< >
分享
最佳答案
0

因为你代码开始插入的时候,向两个集合插入的是同一对象 :tempData = new ChartMetaData();

收获园豆:5
Launcher | 高人七级 |园豆:45045 | 2012-07-31 16:18

是啊,for循环插入两个集合同样的数据,然后再分别根据条件修行单独修改两个各集合的值,另外一个也会跟着变啊?

赌东道道 | 园豆:194 (初学一级) | 2012-07-31 16:28

@赌东道道: 虽然是单独修改,但是却仍然是修改的同一个对象。另一个没有变,它只是显示了正确的结果,因为这里从来就没有出现过你分别修改了同一个对象的两个副本的情形。

Launcher | 园豆:45045 (高人七级) | 2012-07-31 16:34

@程序猿.码农: 

 1                                 if (Aitem == "1" && alist[i].GetDate==model.GetTime)
 2                                 {
 3                                     tempData = new ChartMetaData();
 4                                     tempData.Value = model.PA * ct * pt;
 5                                     tempData.StrValue = tempData.Value.ToString();
 6                                     tempData.GetDate = model.GetTime;
 7                                     tempData.MaxValue = pe[1];
 8                                     tempData.MinValue = pe[2];
 9                                     alist[i] = tempData;
10                                 }
11 
12                                 if (Bitem == "1" && blist[i].GetDate == model.GetTime)
13                                 {
14                                     tempData = new ChartMetaData();
15                                     tempData.Value = model.PB * ct * pt;
16                                     tempData.StrValue = tempData.Value.ToString();
17                                     tempData.GetDate = model.GetTime;
18                                     tempData.MaxValue = pe[1];
19                                     tempData.MinValue = pe[2];
20                                     blist[i] = tempData;
21                                 }

谢谢~我把代码改了一下,这样调试看了一下两个集合的值不会随另一个集合值改变而改变了,

这个代码还有没有其他更好的写法?

赌东道道 | 园豆:194 (初学一级) | 2012-07-31 17:04

@赌东道道: 但从这段代码看来,你可以封装一个 CreateChartMetaData 的方法,来把new ChartMetaData的代码封装起来。

Launcher | 园豆:45045 (高人七级) | 2012-07-31 17:31
其他回答(1)
0

在for循环时,每次都new 个对象出来,问题即可解决。

产生原因:你在for循环操作中都是操作同一个内存副本。

az235 | 园豆:8483 (大侠五级) | 2012-07-31 17:17

谢谢~

支持(0) 反对(0) 赌东道道 | 园豆:194 (初学一级) | 2012-08-01 09:44
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册