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.