Winform:列表中最后一个元素的值将覆盖其他元素

时间:2018-04-04 08:51:25

标签: c# winforms

我有一个这样的方法:

udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];

然而,cusList [last] .Ghe的值覆盖了所有customer.Ghe在cusList.Where我错了?

更新

这是我的完整代码:

Customer.cs:

List<Customer> cusList = new List<Customer>();
private void get_customers_info()
{
   cusList.Clear();
   cusList = busCus.select(movie_id);// return a list of customers

   for (int i = 0; i < cusList.Count; i++)
   {
       cusList[i].Ghe = busCus.listGhe(cusList[i].Ten, movie_id);
   }
}

这是busCus.select方法:

public class Customer
    {
        public Customer()
        {
            Ghe = new List<string>();
        }
        public int Counted_ghe { get; set; }
        public string Ten { get; set; }
        public string SDT { get; set; }
        public List<string> Ghe { get; set; }
        public int TinhTien
        {
            get
            {
                int cost = 0;
                foreach (string ghe in Ghe)
                {
                    char[] az = Enumerable.Range('D', 'F' - 'D' + 1).Select(x => (Char)x).ToArray();
                    char gheRow = ghe[0];
                    int gheNum = Int32.Parse(ghe.Substring(1));
                    if (az.Contains(gheRow) &&  4 <= gheNum && gheNum <= 7)
                    {
                        cost += 85000;
                    }
                    else
                    {
                        cost += 70000;
                    }
                }
                return cost;
            }
        }
        public DateTime GioDat { get; set; }
        public override string ToString()
        {
            return Ten + " - " + Counted_ghe + " ghế";
        }
    }
}

这是busCus.listGhe方法:

public List<Customer> select(int movie_id)
        {
            cusList.Clear();
            dbConnection.Open();
            try
            {
                string sql = "Select c.name, c.phone, COUNT(b.Ghe_num) AS SL_ghe from Customers c inner JOIN bookings b ON b.cus_id = c.id and movie_id = " + movie_id + " GROUP BY c.id";
                MySqlCommand cmd = new MySqlCommand();
                cmd.Connection = dbConnection;
                cmd.CommandText = sql;
                using (DbDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            int cusNameIndex = reader.GetOrdinal("name");
                            string cusName = reader.GetString(cusNameIndex);
                            int cus_phoneIndex = reader.GetOrdinal("phone");
                            string cus_phone = reader.GetString(cus_phoneIndex);
                            int cus_gheIndex = reader.GetOrdinal("SL_ghe");
                            int cus_ghe_count = reader.GetInt32(cus_gheIndex);
                            Customer cus = new Customer();
                            cus.Ten = cusName;
                            cus.SDT = cus_phone;
                            cus.Counted_ghe = cus_ghe_count;
                            cusList.Add(cus);

                        }
                    }
                }
            }
            catch
            {

            }
            finally
            {
                dbConnection.Close();
                dbConnection.Dispose();
                //dbConnection = null;
            }

            return cusList;
        }

对不起伙计们。我已经更新了更多代码。我的英语很糟糕,所以我不知道如何更清楚地解释我的问题。

1 个答案:

答案 0 :(得分:0)

在不了解您的代码的情况下,我假设您以某种方式获取对象的引用而不是副本(我假设Ghe和Ten是您的客户类的属性)。