开发者

How To Add In List Array Using Loop I am Getting Error?

开发者 https://www.devze.com 2023-03-16 11:05 出处:网络
Hi all i am getting only one value in list array it is not adding rows in it.. how to add rows in list array ?

Hi all i am getting only one value in list array it is not adding rows in it.. how to add rows in list array ?

for(int a=0;a<_dt.Rows.Count;a++)
{     
    double PW =Convert.ToDouble(_dt.Rows[a]["POWER"]);
    int VOL =Convert.ToInt32(_dt.Rows[a]["VOLTAGE"]);
    double PV = PW * VOL;
    List<double> res = new List<double>();
    r开发者_如何学JAVAes.Add(PV);
}

hopes for your suggestions..

Regards,


You are adding the result to a List inside the loop, but you must declare it bevor the loop:

List<double> res = new List<double>();             
for(int a=0;a<_dt.Rows.Count;a++) {
    double PW =Convert.ToDouble(_dt.Rows[a]["POWER"]);             
    int VOL =Convert.ToInt32(_dt.Rows[a]["VOLTAGE"]);             
    double PV = PW * VOL;             
    res.Add(PV);         
} 


You seem to be remaking the list each intteration. While you havent said the error, perhaps this is the root cause of your problem. Make the list (aka res) before the loop.


Everytime in the loop you are creating new instance of the List. Please take it outside the loop.

  • Justin Samuel.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号