2013-04-14 35 views
0

希望我能夠正確,清楚地描述這一點。我正在嘗試保存用戶輸入的多個詳細信息並將其保存到列表中。然而目前我這樣做的方式只是保存對象類型/名稱而不是數據。下面是我的代碼,我將如何保存對象數據而不是對象的名稱?添加對象的內容而不是參數類型?

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 = "[email protected]"; 
     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(); 
} 

如果任何人都可以幫助我瞭解什麼我錯過了,以及如何訪問數據打印出來,我將不勝感激。預先感謝您提供的任何幫助。

+0

爲什麼你打印的2組合關注學生們? t和t1?你爲什麼要打印課程而不是內容?另外,「節省」是什麼意思?你有某種序列化,或者你的意思是你把這些信息放到程序的內存中? – LightStriker

+0

我的錯誤解釋是,我試圖打印內容但不確定如何訪問該內容。 –

+0

您可以使用「。」來訪問對象成員......正如devdigital所說的。 'myStudent.FirstName'。這是令人驚訝的,因爲你在任何地方都使用它......比如'this.FirstName'或'Console.WriteLine()'。一個是屬性,另一個是方法,但都是一個類的成員。 – LightStriker

回答

0

有很多的問題在這裏,但在你的Console.WriteLine電話,你只顯示Student類型,所以在Student類型ToString方法將被調用,默認情況下將顯示類型名稱。

您想要顯示Student類型的各個屬性,例如,

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

記住StudentPerson派生,因此,所有的公共屬性都從Student參考訪問,因爲StudentPerson

另外:

  1. 你有一個冗餘環路 - 你只需要一個循環,其列舉了studentList
  2. 還有就是這裏的擔憂真正的混合物。你StudentPerson類型不應與UI(即任何與Console電話)
  3. 棒與PascalCase(又名UpperCamelCase)爲您的方法名,沒有強調
+0

謝謝你,關於你的關於使用PascalCase的第三點的一個問題_方法名是由我安裝的resharper建議的,我假設它向我展示了正確的練習,我之前按照你的建議命名它們,這是已知的標準? –

+0

這取決於你特定的ReSharper配置,但最好遵循的標準是StyleCop(http:// stylecop。codeplex.com/)。 StyleCop帶有一個ReSharper插件,它應該根據你運行的ReSharper版本而工作。 – devdigital

+0

謝謝你的鏈接和你的支持我已經安裝了擴展到我的複製resharper,希望至少我的方法名稱現在是正確的。 –

相關問題