2013-07-10 39 views
1

我設置了一個包含名稱/數字/等的類,並創建了一個保存它們的對象列表。我想在ListView中的第二個窗體上顯示它們。現在,我使用的是吸氣像...我可以在C#中將對象列表從一個表單傳遞給另一個表單嗎?

public List<Employee> GetEmpList(){ 
    return EmployeeList; 
} 

然後在第二個窗體的構造我使用Form1.GetEmpList()之類......

DisplayForm{ 
InitializeComponent(); 
LoadEmployees(EmployeeAddition.GetList()); 
} 

我正在和錯誤說「非靜態字段需要對象引用」。我調用的方法是非靜態的,並返回Form1類中List的引用。我甚至試圖讓List公開並使用Form1.List調用,但它仍然給我同樣的錯誤。

有沒有辦法在表單類之間傳遞一個List或是不可能的?

編輯:Poeple說,需要更多的信息。我不想在這裏複製粘貼所有的代碼,但是我只是因爲我是新手而不太清楚什麼是相關的,哪些不相關。 (我正在上一堂課,但遠程學習,而我的老師是......好遠程老師,沒用的,她實際上告訴我在這裏問)

我想我缺少如何instatize方法,我認爲當從該類創建對象時,方法成爲對象的一部分。該方法是Form1(重命名,但多數民衆贊成是什麼)類/對象的一部分。我會在這裏愚弄這些代碼,我不知道這是否令人不悅。如果是這樣,我很抱歉。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace EmplyeeList 
{ 
    public partial class EmployeeDisplay : Form 
    { 
     public EmployeeDisplay() 
     { 
      InitializeComponent(); 
      LoadEmployees(EmployeeAddition.GetList()); 

     } 

     private void LoadEmployees(IList<CorpEmployee> emp) 
     { 
      foreach (CorpEmployee ce in emp) 
      { 
       ListViewItem lvi = new ListViewItem(); 
       lvi.SubItems.Add(ce.Name); 
       lvi.SubItems.Add(ce.Address); 
       lvi.SubItems.Add(ce.PhoneNumber); 
       lvi.SubItems.Add(ce.ServiceArea); 
       lvi.SubItems.Add(ce.EmplNumber.ToString()); 
       lvi.SubItems.Add(ce.RoomNumber.ToString()); 
       lvi.SubItems.Add(ce.PhoneExt.ToString()); 
       lvi.SubItems.Add(ce.email); 
       displayListView.Items.Add(lvi); 
      } 
     } 

     private void closeButton_Click(object sender, EventArgs e) 
     { 
      this.Close(); 
     } 
    }  
} 

這是第一個Form類加載...

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace EmplyeeList 
{ 
    public class EmployeeAddition : Form 
    { 
     //Create a list to hold CorpEmployee objects. 
     private List<CorpEmployee> CorpEmplList = new List<CorpEmployee>(); 
     public EmployeeAddition() 
     { 
      InitializeComponent(); 
     } 

     private void saveButton_Click(object sender, EventArgs e) 
     { 
      int testingNum;  //Used for output in parsing numbers 

      //If statments are used to make sure ints are ints, and nothing is blank. 
      if (Int32.TryParse(employeeNumTextBox.Text, out testingNum) || ! (employeeNumTextBox.Text == "")) 
      { 
       if (Int32.TryParse(roomNumTextBox.Text, out testingNum) || !(roomNumTextBox.Text == "")) 
       { 
        if (Int32.TryParse(phoneExtTextBox.Text, out testingNum) || !(phoneExtTextBox.Text == "")) 
        { 
         if (!(nameTextBox.Text == "") || !(addressTextBox.Text == "") || !(titleTextBox.Text == "") || !(phoneNumberTextBox.Text == "") || 
          !(serviceAreaTextBox.Text == "") || !(emailTextBox.Text == "")) 
         { 
          //If all fields are filled in right then we add the object to the List 
          CorpEmplList.Add(CreateCorpEmployee(nameTextBox.Text, addressTextBox.Text, titleTextBox.Text, 
          phoneNumberTextBox.Text, serviceAreaTextBox.Text, 
          Convert.ToInt32(employeeNumTextBox.Text), Convert.ToInt32(roomNumTextBox.Text), 
          Convert.ToInt32(phoneExtTextBox.Text), emailTextBox.Text)); 
          //Let the user know it was added 
          MessageBox.Show("Employee was added!"); 
          //Clear fields 
          ClearAllFields(); 
         } 
         else 
         { 
          MessageBox.Show("All fields must be filled in."); 
         } 

        } 
        else 
        { 
         MessageBox.Show("Phone Ext.# should be a number"); 
        } 

       } 
       else 
       { 
        MessageBox.Show("Check your Room# and try again."); 
       } 
      } 
      else 
      { 
       MessageBox.Show("Employee Number Should be a number."); 
      } 

     } 

     //This takes in all the employee fields and returns a contructed object 
     private CorpEmployee CreateCorpEmployee(String name, String address, String title, String phoneNumber, 
      String serviceArea, int emplNumber, int roomNumber, int phoneExt, String email) 
     { 
      CorpEmployee corpEmpObject = new CorpEmployee(name, address, title, phoneNumber, serviceArea, emplNumber, roomNumber, phoneExt, email); 

      return corpEmpObject; 
     } 

     //This just clears all the fiels 
     private void ClearAllFields() 
     { 
      nameTextBox.Text = ""; 
      addressTextBox.Text = ""; 
      titleTextBox.Text = ""; 
      phoneNumberTextBox.Text = ""; 
      serviceAreaTextBox.Text = ""; 
      employeeNumTextBox.Text = ""; 
      roomNumTextBox.Text = ""; 
      phoneExtTextBox.Text = ""; 
      emailTextBox.Text = ""; 

     } 

     //This returns the List of CorpEmployees 
     public List<CorpEmployee> GetList() 
     { 
      return CorpEmplList; 
     } 
     private void exitButton_Click(object sender, EventArgs e) 
     { 
      this.Close(); 
     } 

     private void clearButton_Click(object sender, EventArgs e) 
     { 
      ClearAllFields(); 
     } 

     private void showButton_Click(object sender, EventArgs e) 
     { 
      EmployeeDisplay ed = new EmployeeDisplay(); 

      ed.Show(); 
     } 
    } 
} 

在代碼中東歐之後,我想我可能會看到你說的話有關從靜態類,而不是一個對象調用它。有沒有辦法找到編譯器從第一個Form創建的Object的名稱?

+1

它應該是'EmployeeAddition.GetEmpList()' – sarwar026

回答

1

嘗試使用「新」

DisplayForm{ 
InitializeComponent(); 

EmployeeAddition = new EmployeeAdditionClass(); 

LoadEmployees(EmployeeAddition.GetList()); 
} 
+0

這是編譯器創建的第一個窗體。它在顯示時已經被實例化了嗎?有沒有辦法找到Visual Studio用來創建Form類的第一個實例的引用變量?因此,我可以從中調用該方法,而不是創建一個新方法,因爲它將具有我想要的List。或者在類中創建超載它的默認創建以某種方式? – OmegaNine

2

您有是您治療GetEmpList()函數作爲靜態方法的第一個問題。它不是靜態的,可能不應該是靜態的。這就是爲什麼人們說你需要創建它的一個實例。然而,看看你在問什麼問題的方式可能會以更基本的方式出現,只是創建一個新版本的表單就可以解決。問題是你想要在表單之間傳遞模型數據。沒有更多的信息,真的很難說出你希望如何一起走到一起。但是,您可能已經擁有EmployeeAddition表單的實例,並且不需要在DisplayForm的構造函數內創建另一個實例。相反,你應該做的是公開LoadEmployees方法,並將GetEmpList()的結果傳遞給該方法。

所以,你的結構會更喜歡

public class EmployeeAddition : Form { 

... 

public List<Employee> GetEmpList(){ 
    return EmployeeList; 
} 

... 

public void ShowDisplayForm(){ 
    var displayForm = new DisplayForm(); 
    displayForm.LoadEmployees(GetEmpList()); 
    displayForm.Show(); 
} 

... 

} 

當然,這種結構可能稍有偏差,我猜在該形式將自己的哪種形式,但是這是更接近你想要什麼。

+0

如果它是第一種形式,編譯器是爲我實例化對嗎?我無法找到什麼編譯器正在調用用於表單「EmployeeAddition:Form」的引用變量。這就是我實際上是從這個Class而不是Class來調用這個方法的意義。我查看了.Designer,但是我看到的唯一引用變量是用於標籤和其他工具,但是在表單中。 – OmegaNine

+0

您的問題揭示了代碼/ WinForms如何工作的缺陷。編譯器不會爲你調用任何東西。編譯器創建你的可執行文件,並且總是有東西要調用。我不知道我是否理解你的其餘問題。默認情況下,Program類(使用Main()方法)將確定應用程序啓動時會發生什麼。除此之外,你需要更清楚地回答你的問題。 –

相關問題