2013-07-10 38 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()方法)将确定应用程序启动时会发生什么。除此之外,你需要更清楚地回答你的问题。 –

相关问题