M.kazem Akhgary
M.kazem Akhgary

Reputation: 19149

unknown behaviour in C# windows form application

I have one class Product in several properties. The problem is when I set the Count of Product for Customer property the Count of Product for Supermarket would be changed too.

I have these classes and properties:

class Supermarket
{
    string _name;
    string _address;
    string _phoneNumber;
    List<Product> _products = new List<Product>{ };
    List<Product> _soldProducts = new List<Product>{ };
    List<Customer> _customers = new List<Customer>{ };
    private int _customersCount = 0;
 }

class Customer:Human
{
    int _customerId;
    string _bankId;
    List<Product> _purchased = new List<Product> { };
    List<Product> _purchaselist = new List<Product> { };
    float _discount;
}

class Product
{
    string _id;
    string _name;
    DateTime _expireDate;
    int _cost;
    int _count;
}

with debug I find out that this part will change the Count of Product in Supermarket BUT i dont understand why.

                supermarket.Customers[customerIndex].Purchaselist.Add(product);
                supermarket.Customers[customerIndex].Purchaselist.Last().Count=productCount;

also i removed the Setter Property for Products in Supermarket but the problem still exists.

for adding products I used .Add(...);

Upvotes: 0

Views: 132

Answers (2)

FlySoFast
FlySoFast

Reputation: 1922

For every time adding an object into a List, you should "new" (create new instance of) that object before calling the Add() method.

Product product = new Product();
product._id="original value";
productList1.Add(product);
productList2.Add(product);

product._id="new value"; // this will change both object instances that you have added to the 2 lists above.

Another example:

   Product product = new Product();
   for(int i=0;i<3;i++){
       product._id=i.ToString();
       productList.Add(product);
   }
   //EXPECTED: 0 1 2
   //RESULT: 2 2 2

Instead, you should do this:

UPDATE

        Product product = new Product();
        product._id = "fisrt value";

        List<Product> productList1 = new List<Product>();
        List<Product> productList2 = new List<Product>();

        productList1.Add(product);
        product = new Product(); // initialize a new instance
        product._id = "second value";
        productList2.Add(product);

        product = new Product();// initialize another new instance
        product._id = "new value";

        Console.WriteLine("List 1:");
        foreach (var p in productList1)
        {
            Console.WriteLine(p._id + " ");
        }

        Console.WriteLine("List 2:");

        foreach (var p in productList2)
        {
            Console.WriteLine(p._id + " ");
        }

        Console.WriteLine("Last value: " + product._id);

        Console.ReadKey();

      //RESULT: List1: first value
      //        List2: second value
      //        Last value: new value

Upvotes: 1

Null Pointer
Null Pointer

Reputation: 9289

I am assuming you are using same instance of Product class in supermarket.Customers and supermarket.Supermarket .

Make sure you are creating a new instance of Product while adding a product to purchased list

Upvotes: 1

Related Questions