【问题标题】:Adding contents of object to list rather then parameter type?将对象的内容添加到列表而不是参数类型?
【发布时间】:2013-04-14 22:18:47
【问题描述】:

希望我描述得足够准确和清楚。我正在尝试保存用户输入的多个详细信息并将它们保存到列表中。然而,我目前这样做的方式是只保存对象类型/名称而不是数据。下面是我的代码,我将如何保存对象数据而不是对象的名称?

Student stud = new Student();
stud.Enter_Student();
_studentList.Add(stud);

输入学生

class Student : Person
{
    public string StudentId { get; private set; }
    public string Subject { get; private set; }

    //string[] _studentdb = new string[4];

    public Student()
    {
        StudentId = "abc123";
        Subject = "Building Subject";
    }

    public void Enter_Student()
    {
            this.Person_Prompt_Print(); //Prompts for user
            this.Address_Prompt_Print();
            this.Contact_Prompt_Print();
            Console.SetCursorPosition(4, 18);
            Console.WriteLine("Student ID:");

            this.Enter_Person(); // Inputs from user
            this.Enter_Address();
            this.Enter_Contacts();
            StudentId = Console.ReadLine();
            Console.SetCursorPosition(30, 18);
    }
}

人物类的日期样本

class Person
{

    Tidyup tidy = new Tidyup();

    public string FirstName { get; private set; }
    public string Surname { get; private set; }
    public string MiddleName { get; private set; }
    public string AddressLine1 { get; private set; }
    public string AddressLine2 { get; private set; }
    public string Town { get; private set; }
    public string Postcode { get; private set; }
    public string Email { get; private set; }
    public string Telephone { get; private set; }



    public Person()
    {
        FirstName = "Name";
        Surname = "Surname";
        MiddleName = "Middle Name";
        AddressLine1 = "Address";
        AddressLine2 = "Address Ln2";
        Town = "Town";
        Postcode = "<xxx>/<xxx>";
        Email = "name@buildright.ac.uk";
        Telephone = "0800 0000000";
    }

    public void Person_Prompt_Print()
    {
        // Program Frame
        tidy.Line_Top();
        tidy.Line_Base();
        tidy.Sides_Left();
        tidy.Sides_Right();

        Console.SetCursorPosition(4, 2); //Prompts for user
        Console.WriteLine("FirstName:");
        Console.SetCursorPosition(4, 4);
        Console.WriteLine("Surname:");
        Console.SetCursorPosition(4, 6);
        Console.WriteLine("Middle Name:");
    }

    public void Address_Prompt_Print()
    {
        Console.SetCursorPosition(4, 8); //Prompts for user
        Console.WriteLine("House Number/Name:");
        Console.SetCursorPosition(4, 10);
        Console.WriteLine("Street:");
        Console.SetCursorPosition(4, 12);
        Console.WriteLine("Town:");
        Console.SetCursorPosition(4, 14);
        Console.WriteLine("Post Code:");
    }

    public void Contact_Prompt_Print()
    {
        Console.SetCursorPosition(4, 16);
        Console.WriteLine("Email:");
        Console.SetCursorPosition(4, 18);
        Console.WriteLine("Telephone:");
    }

    public void Enter_Person()
    {

        Console.SetCursorPosition(30, 2); // Inputs from user
        FirstName = Console.ReadLine();
        Console.SetCursorPosition(30, 4);
        Surname = Console.ReadLine();
        Console.SetCursorPosition(30, 6);
        MiddleName = Console.ReadLine();
    }

    public void Enter_Address()
    {

        Console.SetCursorPosition(30, 8);  // Inputs from user
        AddressLine1 = Console.ReadLine();
        Console.SetCursorPosition(30, 10);
        AddressLine2 = Console.ReadLine();
        Console.SetCursorPosition(30, 12);
        Town = Console.ReadLine();
        Console.SetCursorPosition(30, 14);
        Postcode = Console.ReadLine();
    }

    public void Enter_Contacts()
    {
        Console.SetCursorPosition(30, 16);
        Email = Console.ReadLine();
        Console.SetCursorPosition(30, 18);
        Telephone = Console.ReadLine();
    }

} // End of Class

最后我通过一个简单的嵌套 foreach 循环打印出来

public void Print_all_student()
{
    Console.Clear();

    foreach (Student t in _studentList)
    {
        // print another list items.
        foreach (Student t1 in _studentList)
        {
            Console.WriteLine("/" + t + "/" + t1);
        }
    }
    Console.ReadKey();
}

如果有人可以帮助我了解我缺少什么以及如何访问数据以打印出来,我将不胜感激。提前感谢您提供的任何帮助。

【问题讨论】:

  • 为什么要打印 2 个学生的组合? t 和 t1?为什么要打印课程而不是其内部内容?另外,您所说的“节省”是什么意思?您是否有某种序列化,或者您的意思是您将这些信息放在程序的内存中?
  • 我的错误解释,我只是想打印内部内容,但不确定如何访问。
  • 您可以使用“.”访问对象成员...正如 devdigital 所说。 myStudent.FirstName。这很令人惊讶,因为你到处都在使用它……比如this.FirstNameConsole.WriteLine()。一个是属性,另一个是方法,但都是类的成员。
  • 这是我的愚蠢,现在感觉很愚蠢
  • 发生在我们最好的人身上。 :)

标签: c# list object


【解决方案1】:

这里有很多问题,但是在你的Console.WriteLine调用中,你只显示Student类型,所以Student类型上的ToString方法会被调用,默认情况下会显示输入名称。

您想显示Student 类型的各个属性,例如

foreach (Student student in studentList)
{
    Console.WriteLine(student.FirstName);
    Console.WriteLine(student.Surname);
    // etc
}

请记住,Student 派生自 Person,因此所有公共属性都可以从 Student 引用中访问,因为 StudentPerson

还有:

  1. 您有一个冗余循环 - 您只需要一个循环来枚举 studentList
  2. 这里确实存在各种问题。您的 StudentPerson 类型不应与 UI 相关(即与 Console 调用有关的任何事情)
  3. 方法名称使用 PascalCase(又名 UpperCamelCase),不要使用下划线

【讨论】:

  • 谢谢你,关于你关于使用 PascalCase 的第三点的一个问题,我安装的 resharper 建议了 _ 方法名称,我认为它向我展示了正确的做法,我之前将它们命名为你有建议,这是已知的标准吗?
  • 这取决于您的特定 ReSharper 配置,但最好遵循的标准是 StyleCop (stylecop.codeplex.com)。 StyleCop 附带一个 ReSharper 插件,该插件应根据您运行的 ReSharper 版本工作。
  • 感谢您的链接和您的支持我已将扩展安装到我的复制 resharper,希望至少我的方法名称现在是正确的。
  • 实际上,我刚刚看过,令人惊讶的是,方法命名不是 StyleCop 规则,而是由 ReSharper 默认强制执行,因此您必须在某个地方更改了默认 ReSharper 设置。
猜你喜欢
  • 2014-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-13
  • 1970-01-01
  • 1970-01-01
  • 2012-02-16
  • 1970-01-01
相关资源
最近更新 更多