I have a list with Customer objects and I’m trying to search for a name in each object. In the Customer object there is a properties, ContactData, that return a Contact object. And inside the Contact object I have a method that returns a string of a name. It is that name I’m trying to compare with, but it’s not working properly, because I get a hit every time, despite I search for a name that don’t exist in any of the Customer object. The code I’m using:
foreach (Customer name in m_customers)
{
if (name.ContactData.FullName == "Anna")
{
MessageBox.Show(string.Format("Yes"), "Test!", MessageBoxButtons.OK, MessageBoxIcon.Information); // Just for testing
}
}
I have also tested to use a simple for loop, but I get the same result. I’m not sure I’m doing the right thing and would preciate some help! Thanks!
Try this, it might be that the ‘FullName’ is somehow being set to “Anna” for all customers somewhere in your code, or in the database, or in the stored proc…
If that’s the case, do a global search for the keyword ‘Anna’ and see what pops up.
The other possibility is that you’re not creating a new ContactData object with each Customer and simply reusing a variable. This might be updating all Customers with the SAME ContactData object because a pointer is being used. Instead, with each Customer, create a brand new ContactData object instead.
If these two possibilities don’t help, you’ll have to post more code to troubleshoot.