Luxx
Luxx

Reputation: 23

console.writeline not displaying list elements

I am an absolute begginer to c#, I'm stuck on my first console app on something very basic, but I can't get past it.

I want to be able to take a users input, write it to a list, and have the elements in the list displayed back to me.

This is how I have created my list, as I understand it, it is initilised with no values in it.

class PersonRegistry
    {
        // Create a registry of Persons
        public static List<Person> Registry = new List<Person>();
    }

I've constructed my person class:

class Person
    {
        private Guid Guid {get; set;}
        private string FirstName {get; set;}
        private string LastName {get; set;}
        private double SalaryPerHour {get; set;}
        private double SalaryPerDay {get; set;}
        private double RechargePerHour {get; set;}
        private double RechargePerDay {get; set;}

       public Person(string firstName, string lastName, double salaryPerHour, double salaryPerDay, double rechargePerHour, double rechargePerDay)
        {
            this.Guid = new Guid();  // set Guid when object is created
            this.FirstName = firstName;
            this.LastName = lastName;
            this.SalaryPerHour = salaryPerHour;
            this.SalaryPerDay = salaryPerDay;
            this.RechargePerHour = rechargePerHour;
            this.RechargePerDay = rechargePerDay;
        }
    }

I accept user input:

class Program
    {
        static void Main() 
        {  
            Console.WriteLine("Please enter your first name");
            string firstName = Console.ReadLine();

            Console.WriteLine("Please enter your last name");
            string lastName = Console.ReadLine();

            Console.WriteLine("Please enter your salary per hour");
            double salaryPerHour = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine("Please enter your salary per day");
            double salaryPerDay = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine("Please enter your recharge per hour");
            double rechargePerHour = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine("Please enter your recharge per day");
            double rechargePerDay = Convert.ToDouble(Console.ReadLine());

And here is where I am writing to the list:

            PersonRegistry.Registry.Add(new Person(firstName, lastName, salaryPerHour, salaryPerDay, rechargePerHour, rechargePerDay));

I tried this first to then have the program print the elements in the list, but which didn't work:

            //PersonRegistry.Registry.ForEach(Console.WriteLine); - doesn't work as expected, us for loop?

And then I tried using a for loop, which also doesn;t work:

        for (int i = 0; i < PersonRegistry.Registry.Count; i++)
        {
        Console.WriteLine(PersonRegistry.Registry[i]);
        }
    }
}



Upvotes: 0

Views: 978

Answers (2)

Joma
Joma

Reputation: 3859

You need to override ToString method in Person class

class PersonRegistry
{
    public static List<Person> Registry = new List<Person>();

    static void AddPersonToRegistry()
    {
        Person person = new Person();
        Console.WriteLine("Please enter your first name");
        person.FirstName = Console.ReadLine();

        Console.WriteLine("Please enter your last name");
        person.LastName = Console.ReadLine();

        Console.WriteLine("Please enter your salary per hour");
       person.SalaryPerHour = Convert.ToDouble(Console.ReadLine());

        Console.WriteLine("Please enter your salary per day");
        person. SalaryPerDay = Convert.ToDouble(Console.ReadLine());

        Console.WriteLine("Please enter your recharge per hour");
        person.RechargePerHour = Convert.ToDouble(Console.ReadLine());

        Console.WriteLine("Please enter your recharge per day");
        person.RechargePerDay = Convert.ToDouble(Console.ReadLine());

        Registry.Add(person);
    }

    static void PrintRegistry()
    {
        Registry.ForEach(element =>
        {
            Console.WriteLine(element.ToString());
        });
    }
}

class Person
{

    public string Id { get; set; } = Guid.NewGuid().ToString();
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public double SalaryPerHour { get; set; }
    public double SalaryPerDay { get; set; }
    public double RechargePerHour { get; set; }
    public double RechargePerDay { get; set; }

    public Person()
    {

    }
    public Person(string firstName, string lastName, double salaryPerHour, double salaryPerDay, double rechargePerHour, double rechargePerDay)
    {
        this.FirstName = firstName;
        this.LastName = lastName;
        this.SalaryPerHour = salaryPerHour;
        this.SalaryPerDay = salaryPerDay;
        this.RechargePerHour = rechargePerHour;
        this.RechargePerDay = rechargePerDay;
    }

    public override string ToString()
    {
        return $"{nameof(Id)}: {Id}{Environment.NewLine}" +
            $"{nameof(FirstName)}: {FirstName}{Environment.NewLine}" +
            $"{nameof(LastName)}: {LastName}{Environment.NewLine}" +
            $"{nameof(SalaryPerHour)}: {SalaryPerHour}{Environment.NewLine}" +
            $"{nameof(SalaryPerDay)}: {SalaryPerDay}{Environment.NewLine}" +
            $"{nameof(RechargePerHour)}: {RechargePerHour}{Environment.NewLine}" +
            $"{nameof(RechargePerDay)}: {RechargePerDay}";
    }

}

References

Fields
Properties
override

Upvotes: 0

curiousBoy
curiousBoy

Reputation: 6834

First of all change your Person class' properties to public so you can access them.

class Person
{
    public Guid Guid {get; set;}
    public string FirstName {get; set;}
    public string LastName {get; set;}
    public double SalaryPerHour {get; set;}
    public double SalaryPerDay {get; set;}
    public double RechargePerHour {get; set;}
    public double RechargePerDay {get; set;}

    public Person(string firstName, string lastName, double salaryPerHour, double salaryPerDay, double rechargePerHour, double rechargePerDay)
    {
        this.Guid = new Guid();  // set Guid when object is created
        this.FirstName = firstName;
        this.LastName = lastName;
        this.SalaryPerHour = salaryPerHour;
        this.SalaryPerDay = salaryPerDay;
        this.RechargePerHour = rechargePerHour;
        this.RechargePerDay = rechargePerDay;
    }
}

Second is, you are trying to print the object, not the properties.

  Console.WriteLine(PersonRegistry.Registry[i]); // this is a Person object you are trying to print.

 foreach(var person in PersonRegistry.Registry) {
  Console.WriteLine($ "FirstName: {person.FirstName}");
  Console.WriteLine($ "LastName: {person.LastName}");
  Console.WriteLine($ "SalaryPerHour: {person.SalaryPerHour}");
  Console.WriteLine($ "SalaryPerDay: {person.SalaryPerDay}");
  Console.WriteLine($ "RechargePerDay: {person.RechargePerDay}");
 }

 Console.ReadLine();

Or if you want it in a more dynamic way, than you can use like below:

 foreach(var person in PersonRegistry.Registry) {
  Type t = person.GetType(); // Where person is object whose properties you need.
  PropertyInfo[] pi = t.GetProperties();
  foreach(PropertyInfo p in pi) {
   System.Console.WriteLine(p.Name + " : " + p.GetValue(person));
  }
 }

Console.ReadLine();

Upvotes: 3

Related Questions