What am I doing wrong?? it loads info into the list. only allows one png file for you to see the load

Natale J Montalto 0 Reputation points
2025-10-09T12:57:57.0966667+00:00
Developer technologies | .NET | .NET MAUI
{count} votes

3 answers

Sort by: Most helpful
  1. Michael Le (WICLOUD CORPORATION) 3,495 Reputation points Microsoft External Staff
    2025-10-10T02:46:33.8233333+00:00

    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.

    1 person found this answer helpful.

  2. Starry Night 600 Reputation points
    2025-10-10T02:10:35.4833333+00:00

    Hi @Natale J Montalto

    The static keyword in C# is used to declare members that belong to the type itself rather than to a specific object. This keyword can be applied to classes, variables, methods, constructors, properties, events, and operators. When a member is declared static, it can be accessed directly using the class name without creating an instance of the class.

    Static Class

    A static class is declared using the static keyword. It can only contain static members and cannot be instantiated. Static classes are sealed, meaning they cannot be inherited. Here's an example of a static class:

    static class CompanyEmployee
    {
    public static void DoSomething() { /*...*/ }
    public static void DoSomethingElse() { /*...*/ }
    }
    
    

    In this example, the CompanyEmployee class contains only static methods and cannot be instantiated.

    Static Variable

    A static variable is shared among all instances of a class. It is declared using the static keyword and can be accessed directly using the class name. Here's an example:

    class Vehicle
    {
    public static string Model_color = "Black";
    }
    class Program
    {
    static void Main()
    {
    Console.WriteLine("Color of XY model is: {0}", Vehicle.Model_color);
    }
    }
    

    In this example, the Model_color variable is shared among all instances of the Vehicle class .

    Static Constructor

    A static constructor is used to initialize static fields or data of the class and is executed only once. It is declared using the static keyword and does not take any parameters. Here's an example:

    class G1
    {
       static G1()
    {
       Console.WriteLine("Example of Static Constructor");
    }
    public G1(int j)
    {
       Console.WriteLine("Instance Constructor " + j);
    }
    public string g1_detail(string name, string branch)
    {
       return "Name: " + name + " Branch: " + branch;
    }
    public static void Main()
    {
       G1 obj = new G1(1);
       Console.WriteLine(obj.g1_detail("Sunil", "CSE"));
       G1 ob = new G1(2);
       Console.WriteLine(ob.g1_detail("Sweta", "ECE"));
    }
    }
    

    In this example, the static constructor is called before the first instance constructor is run.

    Note:

    • Static classes cannot be instantiated or inherited.
    • Static members are shared across all instances of a class.
    • Static methods can only access static members directly.
    • Static constructors are called automatically before the first instance is created or any static members are referenced.

    For your problem, you can try to remove the static key word before the three fields of class Client .

    For example:

        public class Client
        {
            public  string ClientId { get; set; } = string.Empty;
            public string ClientName { get; set; } = string.Empty;
            public string ClientAddr1 { get; set; } = string.Empty;
        }
    
    

    And by using the following code, I could get three different instances of class Client:

             Clients.ClienList.Add(new Client { ClientId = "001",ClientName ="Name1",ClientAddr1="Address 1"});
              Clients.ClienList.Add(new Client { ClientId = "002",ClientName ="Name2",ClientAddr1="Address 2"});
              Clients.ClienList.Add(new Client { ClientId = "003",ClientName ="Name3",ClientAddr1="Address 3"});
             for (int i =0; i < Clients.ClienList.Count;i++ ) { 
                
                 System.Diagnostics.Debug.WriteLine("ClientId = " + Clients.ClienList[i].ClientId + "<--> ClientName = " + Clients.ClienList[i].ClientName);
                }
    
    0 comments No comments

  3. Natale J Montalto 0 Reputation points
    2025-10-10T16:10:56.98+00:00

    i have removed the class from static to just public the underlying variables when changed to removing the static then the code can not find them at all. I change the set get and it blows up with stack overflow in a trimmed sting. So, I have tried sever approaches.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.