如何在列表中打印对象的属性?

时间:2016-12-03 18:30:56

标签: c#

我是c#的初学者。 我的问题是,我想创建一个对象列表,我可以动态添加对象,然后打印他们的属性(我想去我想要的每个对象,只打印他的属性)。 我环顾互联网并没有找到一个好的答案,这将有助于我理解如何正确地做到这一点。 我添加了一个尝试...捕获以了解问题,但我得到的解释是我没有添加实例来打印他的属性,即使我完全做到了。

我真的迷失了所以任何帮助都会受到赞赏。

我的代码:

类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using APPcLASS_2;
using System.Collections;

namespace EmployeesBooks
{
    public class EMpLOYcLaSS
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int PhoneNumber { get; set; }
        public string Adress { get; set; }
        public int Days { get; set; }
        public int Mounths { get; set; }
        public int Years { get; set; }
    }
}

主程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using APPcLASS_2;
using EmployeesBooks;

namespace EmployeesBooks
{

    class Program
    {
        static void Main(string[] args)
        {
            EMpLOYcLaSS Employ = new EMpLOYcLaSS();

            List<EMpLOYcLaSS> ListOfObjects = new List<EMpLOYcLaSS>();
            string FirstNameVar, LastNameVar, AdressVar;
            int PhoneNumberVar, DayVar, MounthVar, YearVar;
            while(true)
            {
                Console.Clear();
                Console.WriteLine("Enter your choise:");
                Console.WriteLine("1-Add an employee");
                Console.WriteLine("2-Earase employee");
                Console.WriteLine("3-Show reports");
                var choise=int.Parse(Console.ReadLine());

                switch(choise)
                {
                    case 1:
                        Console.WriteLine("Enter First Name:",Employ.FirstName);
                        FirstNameVar = Console.ReadLine();
                        Employ.FirstName = FirstNameVar;
                        Console.WriteLine("Enter Last Name:");
                        LastNameVar = Console.ReadLine();
                        Employ.LastName = LastNameVar;
                        Console.WriteLine("Enter Phone Number:");
                        PhoneNumberVar =int.Parse(Console.ReadLine());
                        Employ.PhoneNumber = PhoneNumberVar;
                        Console.WriteLine("Enter Address:");
                        AdressVar = Console.ReadLine();
                        Employ.Adress = AdressVar;
                        Console.WriteLine("Enter Birthday:");
                        Console.WriteLine("Day:");
                        DayVar =int.Parse(Console.ReadLine());
                        Employ.Days = DayVar;
                        Console.WriteLine("Mounth:");
                        MounthVar = int.Parse(Console.ReadLine());
                        Employ.Mounths = MounthVar;
                        Console.WriteLine("Year:");
                        YearVar = int.Parse(Console.ReadLine());
                        Employ.Years = YearVar;
                        ListOfObjects.Add(new EMpLOYcLaSS());
                        break;

                    case 3:

                        try
                        {
                            Console.WriteLine("enter a number of employee:(1,2,3,4...)");
                            var EmployeeNumberForPrinting = int.Parse(Console.ReadLine());
                            if (ListOfObjects[EmployeeNumberForPrinting] != null)
                                Console.WriteLine("{0}", ListOfObjects[EmployeeNumberForPrinting].FirstName.ToString());
                            else
                                Console.WriteLine("Don't Exist!");
                            Console.WriteLine("Press any key to proceed");
                            Console.ReadKey();
                            break;
                        }
                        catch(Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                            break;
                        }


                }
            }
        } 
    }
}

1 个答案:

答案 0 :(得分:0)

首先,您不要将员工添加到列表中,而是添加一个新员工。

更改

ListOfObjects.Add(new EMpLOYcLaSS());

ListOfObjects.Add(Employ);

这会将您创建的员工添加到列表中,现在可以打印每个员工的姓名。

foreach(var e in ListOfObjects)
{
    Console.WriteLine(e.FirstName + " " + e.LastName);
}

显然,您可以根据需要添加更多属性,这只是遍历所有对象并打印其每个名称。您打印预定员工的代码现在应该可以使用,只需删除ToString(),因为它已经是一个字符串。只需注意,记住0是列表中的第一个索引。我建议您将可用性添加到EmployeeNumberForPrinting,因为这是您的决定。