2016-05-13 21 views
-6

請記住,這是我在這裏的第一個問題,我也是C#的新手。如何在Visual Studio中構建此代碼?

我將代碼從this YouTube video複製到Visual Studio,但它不會生成。我無法將項目添加到empList。

更新

我真的很感謝你的幫助,但它仍然沒有,儘管你的建議建設(例如我確實有使用System.Collections.Generic命名空間)。我甚至完全複製了Rion的代碼,但仍然無法構建。這些行有錯誤:

)預計「,」;預計

無效表達術語「」

List<Employee> empList = new List<Employee>(); 
empList.Add(new Employee() {ID = 101, Name = "Mary", Salary = 5000, Experience = 5}); 
empList.Add(new Employee() {ID = 102, Name = "Maria", Salary = 6000, Experience = 2}); 
empList.Add(new Employee() {ID = 106, Name = "Mike", Salary = 4000, Experience = 4}); 
empList.Add(new Employee() {ID = 104, Name = "John", Salary = 5000, Experience = 7}); 
Employee.PromoteEmployee(empList); 

它不完全一樣在視頻中的代碼,因爲我還是改了一點,但它不會建立。

class Program 
{ 
    public static void Main(string[] args) 
    { 
     List<Employee> empList = new List<Employee>(); 


     empList.Add(new Employee() {ID = 101, Name = "Mary", Salary = 5000, Experience = 5}); 
     empList.Add(new Employee() {ID = 102, Name = "Maria", Salary = 6000, Experience = 2}); 
     empList.Add(new Employee() {ID = 106, Name = "Mike", Salary = 4000, Experience = 4}); 
     empList.Add(new Employee() {ID = 104, Name = "John", Salary = 5000, Experience = 7}); 

     Employee.PromoteEmployee(empList); 
    } 


} 

class Employee 
{ 
    public int ID; 
    public string Name; 
    public int Salary; 
    public int Experience; 

    public static void PromoteEmployee(List<Employee> employeeList) 
    { 
     foreach (Employee employee in employeeList) 
     { 
      if(employee.Experience >=5) 
      { 
       Console.WriteLine(employee.Name + " promoted"); 
       Console.ReadLine(); 
      } 
     } 
    } 
} 
+6

如果它「不會構建」,那麼想必你會得到一個編譯器錯誤。您必須努力盡可能多地發佈有關您問題的信息。那麼這個錯誤信息是什麼? –

+5

我在這段代碼中看不到任何錯誤。正如已經問到的,請解釋您的問題和收到的任何錯誤消息。 – Steve

+0

這些類在同一個命名空間中嗎? – Master117

回答

1

將項目

如果您需要任何項目添加到列表中,您考慮加入另一行:如果你想通過編程方式處理這個

empList.Add(new Employee() {ID = 42, Name = "Foo", Salary = 42, Experience = 0}); 

控制檯,您可以考慮請求用戶需要的每個值,構建一個Employee對象並將其存儲在您的empList

// Building a new employee 
var newEmployee = new Employee(); 
// Request the ID 
Console.WriteLine("What is the Employee's ID?"); 
newEmployee.ID = Int32.Parse(Console.ReadLine()); 
// Request the Name 
Console.WriteLine("What is the Employee's Name?"); 
newEmployee.Name = Console.ReadLine(); 
// Continue for other properties here 

// After you have what you need, simply add it to the list 
empList.Add(newEmployee); 

檢查參考

既然你沒有明確張貼您可能使用的任何引用的命名空間,確保您使用的語句,才能正常使用List集合以下幾點:

using System.Collections.Generic; 

可能等待輸入

假設如果要輸出每個匹配的員工,那麼它應該是work as expected,但是代碼中的Console.ReadLine()可能等待用戶輸入,而不是完成遍歷對象的迭代。

嘗試將其放置您foreach循環之外的Employee.PromoteEmployee方法中:

public static void PromoteEmployee(List<Employee> employeeList) 
{ 
    foreach (Employee employee in employeeList) 
    { 
     if(employee.Experience >=5) 
     { 
      Console.WriteLine(employee.Name + " promoted"); 
     } 
    } 
    Console.ReadLine(); 
} 

enter image description here

你可以see a complete working example here,上面的例子中的輸出和低於適用代碼:

using System; 
using System.Collections.Generic; 

public class Program 
{ 
    public static void Main() 
    { 
     List<Employee> empList = new List<Employee>(); 
     empList.Add(new Employee() {ID = 101, Name = "Mary", Salary = 5000, Experience = 5}); 
     empList.Add(new Employee() {ID = 102, Name = "Maria", Salary = 6000, Experience = 2}); 
     empList.Add(new Employee() {ID = 106, Name = "Mike", Salary = 4000, Experience = 4}); 
     empList.Add(new Employee() {ID = 104, Name = "John", Salary = 5000, Experience = 7}); 

     Employee.PromoteEmployee(empList); 
    } 
} 

class Employee 
{ 
    public int ID; 
    public string Name; 
    public int Salary; 
    public int Experience; 

    public static void PromoteEmployee(List<Employee> employeeList) 
    { 
     foreach (Employee employee in employeeList) 
     { 
      if(employee.Experience >=5) 
      { 
       Console.WriteLine(employee.Name + " promoted"); 

      } 
     } 
     Console.ReadLine(); 
    } 
} 
+0

這可能是OP只希望能夠在看到列表中的下一名員工之前點擊按鈕?這只是在黑暗中刺傷。 OP特別提到「構建錯誤」,所以除非他們不明白這個意思,否則我不確定這應該是一個答案。 – user1666620

+0

我同意,它也可能僅僅是因爲他們缺少必要的'使用System.Collections.Generic;'來訪問列表。我還提供了有關如何將用戶添加到列表中的信息。 –

相關問題