I can not access objects that I saved in a list in C #

2

I was recently trying to learn C # and I found this problem. - Create a class called person with some attributes. What I want to do is save all the "people" that the user loads the system into a list to then do things with this information, but when I try to go through this list to show all the people loaded the only thing that throws the system is as follows : "the namespace of the project" .Person

Here is the code

using System;
using System.Collections.Generic;

namespace Clases_csharp
{
    class Program
    {
        public static void Main(string[] args)
        {

            Person.Carga_Persona();
            Console.WriteLine("Total:" + Person.count);






        Console.ReadKey();
        }
    }

    class Person
    {
        public static int count;

        public string date_of_birth;
        public string name,lastname;
        public string obs;
        public double CIndex;

        public Person (string _date_Of_birth,string _name,string _lastname,string _Obs,double _CIndex)
        {

            date_of_birth = _date_Of_birth;
            name = _name;
            lastname = _lastname;
            obs = _Obs;
            CIndex = _CIndex;

        }

        public static void Carga_Persona ()
        {
            List<Person> todos = new List<Person>();
                        Console.WriteLine("enter 1 to register a new person, 2 to stop");
            int des = Convert.ToInt32(Console.ReadLine());
            while (des ==1) 
            {


                Console.WriteLine("Enter the Date of birth of the person");
                string date = Console.ReadLine();
                Console.WriteLine("Enter the name");
                string name = Console.ReadLine();
                Console.WriteLine("Enter the Last Name");
                string Last_Name = Console.ReadLine();
                Console.WriteLine("Enter the Observations about de person");
                string obs = Console.ReadLine();
                Console.WriteLine("Enter the Index");
                double Index = Convert.ToDouble(Console.ReadLine());

                Person person1 = new Person(date,name,Last_Name,obs,Index);
                count += 1 ;
                todos.Add(person1);
            Console.WriteLine("enter 1 to register a new person, 2 to stop");
                des = Convert.ToInt32(Console.ReadLine());
            }

            int large = todos.Count ;
            for (int i = 0; i < large; i++) {
                Console.WriteLine(todos[i]);

         }

        }

    }
}

When I run the debugger all the variables seem to have the correct values and the list has saved all the "People" that it loads.

Thank you!

    
asked by Martin_C_Sharp 01.10.2018 в 00:44
source

1 answer

1

Your code has several concept errors.

Let's solve the main problem, because you see "the namespace of the project" .Person?

This happens, because you are asking exactly that to your code.

When doing: Console.WriteLine(todos[i]); What you are asking is, transform the object that is in all [i] to a string, and put it on the screen. Your goal, it would seem that somehow you want to print the person's data.

But what is happening there, is that you are executing the ToString method of the object you are trying to access, which in this case is your Person class. As every class implicitly inherits from object, and your class does not have an overloaded ToString() method, then it calls the ToString() of the base object, which does nothing but print the type of the object (look here for the source code I'm talking about ).

So, what you need is to add a ToString method to your object ( this is stated by microsoft aca ), which tells you how to transform its properties to a printable string.

In your person class, add a method in the following way:

public override string ToString()
{
    return name+" "+lastname;
}  

And inside it, return a string as you want it to be printed. In the example above, return the name, a space and the last name.

Notes

  • Does your person class have almost all the code for the person? that is, have the main code of your program? Person_load should not be there in.

  • The main returns the count, of an object that never grows (or lost it in the code)?

answered by 01.10.2018 в 01:12