添加到列表的新项目将覆盖上一个项目

添加到列表的新项目将覆盖上一个项目

本文介绍了添加到列表的新项目将覆盖上一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中使用List<>.我想在List中添加值.但是问题在于,第一项添加成功,但是当第二项将相同的值ovverride插入到第一个项中时,示例第一项abc成功插入,但是当第二项xyz到来时,它是ovveride abcxyz以及两个项显示xyz.这是我的代码.

I am using List<> in C# . I want to add values in the List . But problem is that first item add successfully but when second item insert the same value ovverride into the first one , example first item abc inserted succefully but when second item xyz came it ovveride abc to xyz and both items shows xyz.Here is my code.

DataTable dtbl3 = new DataTable();
List<CartItems> lst = (List<CartItems>)Session["mycart"];
dtbl3 = DAL.Get("Select * from mytable");
List<EmailClass>  lstCstmer = new List<EmailClass>();
for (int j = 0; j < lst.Count; j++)
{
    emailLst.__EmailcstName = dtbl3.Rows[0]["cstm_name"].ToString();
    emailLst.__EmailcstLName = dtbl3.Rows[0]["cstm_LName"].ToString();
    emailLst.__EmailcstAddress = dtbl3.Rows[0]["cstm_Addr"].ToString();
    emailLst.__EmailcstPhoneNo = dtbl3.Rows[0]["cstm_Phone"].ToString();
    emailLst.__EmailcstCellNo = dtbl3.Rows[0]["cstm_CellNo"].ToString();
    emailLst.__EmailcstskypId = dtbl3.Rows[0]["cstm_skypeId"].ToString();
    emailLst.__EmailcstEmail = dtbl3.Rows[0]["cstm_email"].ToString();
    emailLst.__EmailcstCountry = dtbl3.Rows[0]["cstm_country"].ToString();
    emailLst.__EmailcstCity = dtbl3.Rows[0]["cstm_City"].ToString();
    emailLst.__EmailcstZipcode =Convert.ToInt32( dtbl3.Rows[0]["cstm_ZipCode"].ToString());
    emailLst.__EmailcstRemarks = dtbl3.Rows[0]["cstm_remarks"].ToString();
    emailLst._EmailCartProdName = lst[j]._CartProdName;
    emailLst._EmailCartProdPrice = lst[j]._CartProdPrice;
    emailLst._EmailCartProdQnty = lst[j]._CartProdQnty;
    emailLst._EmailCartProdCode = lst[j]._CartProdName;
    emailLst._EmailTotalProdPrice = lst[j]._TotalProdPrice;
    lstCstmer.Add(emailLst);
}

推荐答案

您要一遍又一遍地添加相同的项目,并且由于它是引用类型,因此列表中的所有条目都指向同一EmailClass实例.

You're adding the same item over and over again, and because it's a reference type all entries in the list point to the same instance of EmailClass.

在每次循环迭代中创建一个新实例来解决该问题:

Create a new instance in every loop iteration to fix that:

for (int j = 0; j < lst.Count; j++)
{
    emailLst = new EmailClass();

    emailLst.__EmailcstName = dtbl3.Rows[0]["cstm_name"].ToString();
    emailLst.__EmailcstLName = dtbl3.Rows[0]["cstm_LName"].ToString();
    emailLst.__EmailcstAddress = dtbl3.Rows[0]["cstm_Addr"].ToString();
    emailLst.__EmailcstPhoneNo = dtbl3.Rows[0]["cstm_Phone"].ToString();
    emailLst.__EmailcstCellNo = dtbl3.Rows[0]["cstm_CellNo"].ToString();
    emailLst.__EmailcstskypId = dtbl3.Rows[0]["cstm_skypeId"].ToString();
    emailLst.__EmailcstEmail = dtbl3.Rows[0]["cstm_email"].ToString();
    emailLst.__EmailcstCountry = dtbl3.Rows[0]["cstm_country"].ToString();
    emailLst.__EmailcstCity = dtbl3.Rows[0]["cstm_City"].ToString();
    emailLst.__EmailcstZipcode =Convert.ToInt32( dtbl3.Rows[0]["cstm_ZipCode"].ToString());
    emailLst.__EmailcstRemarks = dtbl3.Rows[0]["cstm_remarks"].ToString();

    emailLst._EmailCartProdName = lst[j]._CartProdName;
    emailLst._EmailCartProdPrice = lst[j]._CartProdPrice;
    emailLst._EmailCartProdQnty = lst[j]._CartProdQnty;
    emailLst._EmailCartProdCode = lst[j]._CartProdName;
    emailLst._EmailTotalProdPrice = lst[j]._TotalProdPrice;

    lstCstmer.Add(emailLst);

}

这篇关于添加到列表的新项目将覆盖上一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 01:18