Hello @Natale J Montalto ,
The problem is that your Client class properties (ClientId, ClientName, ClientAddr1) are declared as static.
public class Client
{
// The 'static' keyword is the problem here
public static string ClientId { get; set; } = string.Empty;
public static string ClientName { get; set; } = string.Empty;
public static string ClientAddr1 { get; set; } = string.Empty;
}
When a property is static, it belongs to the class itself, not to an instance of the class. This means that every Client object you create will share the exact same values for these properties. When you add a new client and set its properties, you are overwriting the values for all other clients in your list.
The solution is simple. You just need to remove the static keyword from the properties in your Client class. This will make them instance properties, meaning each Client object will have its own separate set of values.
Corrected code
public class Client
{
public string ClientId { get; set; } = string.Empty;
public string ClientName { get; set; } = string.Empty;
public string ClientAddr1 { get; set; } = string.Empty;
}
Example test
You can verify the fix with the following code. Each client will now correctly display its own information.
Clients.ClientList.Add(new Client { ClientId = "001", ClientName = "Name1", ClientAddr1 = "Address1" });
Clients.ClientList.Add(new Client { ClientId = "002", ClientName = "Name2", ClientAddr1 = "Address2" });
Clients.ClientList.Add(new Client { ClientId = "003", ClientName = "Name3", ClientAddr1 = "Address3" });
foreach (var client in Clients.ClientList)
{
// This will now print the unique details for each client
Console.WriteLine($"ID: {client.ClientId}, Name: {client.ClientName}, Address: {client.ClientAddr1}");
}
I hope this explanation is clear and helps you resolve the issue. Let me know if you have any other questions.
Thank you.