0

In my app I retrieve 3 rows of data from my database, I loop through the rows of data to assign the data to my customer object, then add the customer to a customer collection:

// new customer object to fill in loop and assign to collection
tracker.Customer myCustomer = new tracker.Customer();

// new customer collection object to fill later
Tracker.customerCollection  myCustomerCollection = new trackerCustomerCollection();

foreach (System.Data.DataRow drRow in dsResults.Tables[0].Rows) 
{
  myCustomer.CustomerID = Item["CustomerID"];
  myCustomer.FKBandID = Convert.ToInt32(drRow["FKBandID"]);
  myCustomer.FKSectorID = Convert.ToInt32(drRow["FKSectorID"]); 
  myCustomer.CustomerName = Convert.ToString(drRow["CustomerName"]);
  myCustomer.CustomerAddress = Convert.ToString(drRow["CustomerAddress"]);
  myCustomer.CustomerPhoneNumber = Convert.ToString(drRow["CustomerPhoneNumber"]);

  myCustomerCollection.Add(myCustomer);
}

The problem is that when I try to use the populated myCustomerCollection, the 3 Customer objects within the collection are all identical. Each instance of myCustomer is different as I iterate through the loop, but they change once added to myCustomerCollection. each item will be the same as the last item added.

If anyone can point me in the right direction, I'd be very grateful, I have used this principle in VB.NET with no issues, but I'm now forced to use C# and having real trouble finding the source of my problem.

2 Answers 2

2

This is because you're creating 1 instance of your Customer object, and adding it to the list 3 times (modifying it each time you loop).

If you move your new tracker.Customer() inside the loop, you'll create 3 individual customer objects.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the quick response. works a charm. I had previously thought that by assigning myCustomers attributes with new values, it would have been like a new object each time it was added to the collection
1

You need to create a new tracker.Customer in each iteration of the for loop instead of a single instance outside of the loop:

foreach (var drRow in dsResults.Tables[0].Rows) 
{
    // Create a brand new instance of a customer and set its properties
    var myCustomer = new tracker.Customer()
    {
        CustomerID = Item["CustomerID"];
        FKBandID = Convert.ToInt32(drRow["FKBandID"]);
        FKSectorID = Convert.ToInt32(drRow["FKSectorID"]); 
        CustomerName = Convert.ToString(drRow["CustomerName"]);
        CustomerAddress = Convert.ToString(drRow["CustomerAddress"]);
        CustomerPhoneNumber = Convert.ToString(drRow["CustomerPhoneNumber"])
    };
    // Add your new customer to the customer collection
    myCustomerCollection.Add(myCustomer);
}

What's happening right now is that your myCustomerCollection contains three references to the same myCustomer instance, because you aren't instantiating a new one for each record in your database. The values you're seeing now in myCustomer probably pertain to the last row of your data.

By creating a new instance in each iteration of the for loop, you will have three distinct tracker.Customer objects in your list.

This behavior would be exactly the same in VB.NET.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.