2017-11-03 65 views
-4

有沒有可能有一個方法,可以添加一個完整的對象的屬性,然後調用主函數內的方法?添加一個方法

我需要採取行動案例3使用我提供下面的列表。但是,我也不想把List<Student> st = new List<Student>();都在WriteToFile()AddStudent()

例如,我有一個列表:

static void Main(string[] args) 
    { 
     List<Student> st = new List<Student>(); 
     Student st1 = new Student(1, "Mike", "Freelancer", 9); 
     st1.CalculateSalary(); 
     Student st2 = new Student(2, "Bob", "CEO", 7); 
     st2.CalculateSalary(); 
     Student st3 = new Student(3, "Tina", "CEO", 3); 
     st3.CalculateSalary(); 

     do 
     { 
      Console.WriteLine("============================"); 
      Console.WriteLine("1. Write To File"); 
      Console.WriteLine("2. Read From File"); 
      Console.WriteLine("3. Add Student"); 
      Console.WriteLine("============================"); 
      answer = Convert.ToInt32(Console.ReadLine()); 

      switch (answer) 
      { 
       case 1: 
        WriteToFile(filePath); 
        Console.WriteLine("File Created!"); 
        break; 
       case 2: 
        ReadFromFile(filePath); 
        break; 
       case 3: 
        Console.WriteLine("Add Student"); 
        break; 
      } 
     } while (answer != 4); 
    } 

任何幫助將非常感激。

+0

爲什麼將此問題標記爲C++,如果它與C++沒有任何關係? –

+0

你是什麼意思由一個單獨的SO? – Kaloyan

+0

爲什麼你會在'WriteToFile()'或'AddStudent()'中列出'List st = new List ();一種方法可能會將現有的學生數據寫入文件,另一種方法會將新學生添加到列表中。你的問題沒有意義。 –

回答

1

當然,你可以做的是創建一個方法,它接收學生列表,從用戶收集有關新學生的信息,然後將新學生添加到列表中。它還需要確保如果傳入的列表爲空,那麼它將初始化列表。

我在猜你的一些學生屬性是什麼。您可以更改它們以匹配您的Student課程。

/// <summary> 
/// Gets student information from the user and adds a new student to the list 
/// </summary> 
/// <param name="existingStudents">The list of students to add to</param> 
private static void AddStudent(List<Student> existingStudents) 
{ 
    // Initialize the list if it's null 
    if (existingStudents == null) existingStudents = new List<Student>(); 

    int tempInt; 

    // Get student information from the user 
    Console.WriteLine("Enter new student information"); 

    do 
    { 
     Console.Write(" 1. Enter student Id: "); 
    } while (!int.TryParse(Console.ReadLine(), out tempInt)); 
    var id = tempInt; 

    Console.Write(" 2. Enter student Name: "); 
    var name = Console.ReadLine(); 

    Console.Write(" 3. Enter student Job Title: "); 
    var jobTitle = Console.ReadLine(); 

    do 
    { 
     Console.Write(" 4. Enter student years of service: "); 
    } while (!int.TryParse(Console.ReadLine(), out tempInt)); 
    var yrsOfService = tempInt; 

    // Add the new student to the list 
    existingStudents.Add(new Student(id, name, jobTitle, yrsOfService)); 
} 

然後,你可以從你喜歡的主要方法稱之爲:

case 3: 
    AddStudent(st); 
    break; 

注意,在你的Main方法的開始,你從不添加硬編碼的學生到您的列表。創建學生後,您可能需要添加如下內容:

st.Add(st1); 
st.Add(st2); 
st.Add(st3); 
+0

謝謝。我還有一個問題。我想從文件中讀取元素,當我自動讀取它們以便用文件中的數據填充列表時。我試過,但它沒有奏效:public static void showStudents(List existingStudents,string filePath) { ReadFromFile(filePath); (學生學生在現有學生中學習) { // existingStudents.Add(new Student(stud.id,stud.Name,stud.Position,stud.Internship)); foreach Console.WriteLine(stud.ToString()); } } – Kaloyan

+0

這裏是ReadFromFile函數:public static void ReadFromFile(string filePath) { StreamReader reader = new StreamReader(filePath); (!reader.EndOfStream) Console.WriteLine(reader.ReadLine()); } reader.Close(); } – Kaloyan

+0

好的。謝謝。我會分開發布。 – Kaloyan

相關問題