Home > OS >  Can I do an ID foreach instance of class?
Can I do an ID foreach instance of class?

Time:07-18

I'm a newbie in c#. I am just trying to do an ID for each instance in class Person.

class Person
    {
        public Person(string name, int age)
        {
            countPersons  ;
            this.name = name;
            this.age = age;
        }

        public string name { private get; set; }
        public int age { private get; set;}
        private static int countPersons { get; set; }
        
        public void GetNameAndAge()
        {
            Console.WriteLine($"Player name: {name ?? "unknown"} | Player age: {age}");
        }
        public void GetInfo()
        {
            Console.WriteLine($"Player name: {name ?? "unknown"} | Player age: {age}\nId: {countPersons}");
        }

    }

I want every class instance to have an id that starts from one (one, two, three, etc.)

class Program
    {
        public static void Main(string[] args)
        {
            var player1 = new Person("David", 31);
            var player2 = new Person("Brad", 23);
            var player3 = new Person("Michael", 26);

            player1.GetNameAndAge();
            player1.GetInfo();
            player2.GetInfo();
            player3.GetInfo();
        }
    }

But at the end of the program, the ID for everyone is 3... (The variable countPersons is my ID)

CodePudding user response:

You have to hold that in variable or property.

class Person
    {
        public Person(string name, int age)
        {
            id = countPersons  ;
            this.name = name;
            this.age = age;
        }
        int id;
        public string name { private get; set; }
        public int age { private get; set;}
        private static int countPersons { get; set; }
        
        public void GetNameAndAge()
        {
            Console.WriteLine($"Player name: {name ?? "unknown"} | Player age: {age}");
        }
        public void GetInfo()
        {
            Console.WriteLine($"Player name: {name ?? "unknown"} | Player age: {age}\nId: {id}");
        }

    }

Note:

  • Static will increase with each instance creation.
  • id will hold that vlaue.

CodePudding user response:

You need to store the id as property like

public class Person
{
    public Person(int id, string name, int age)
    {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int id { private get; set; }
    public string name { private get; set; }
    public int age { private get; set; }
    private static int countPersons { get; set; }

    public void GetNameAndAge()
    {
        Console.WriteLine($"Player name: {name ?? "unknown"} | Player age: {age}");
    }
    public void GetInfo()
    {
        Console.WriteLine($"Player name: {name ?? "unknown"} | Player age: {age}\nId: {countPersons}");
    }

}

CodePudding user response:

This is happening because you are using a static countPersons and assigning to that.

Instead you could use a for loop in the main function and pass that index in to the Person constructor. This way you would have IDs like 0,1,2,…

  • Related