1

名前/番号/ectを保持するクラスをセットアップし、それらを保持するオブジェクトのリストを作成しました。ListView の 2 番目のフォームにそれらを表示したいと考えています。現在、私は次のようなゲッターを使用しています...

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

次に、2番目のフォームのコンストラクターで Form1.GetEmpList() を使用しています...

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

「非静的フィールドにはオブジェクト参照が必要です」というエラーが表示されます。呼び出しているメソッドは静的ではなく、Form1 クラスの List への参照を返します。List を公開して Form1.List を使用して呼び出すことさえ試みましたが、それでも同じエラーが発生します。

Form クラス間で List を渡す方法はありますか、それとも不可能ですか?

編集: Poeple は、より多くの情報が必要であると述べました。ここにすべてのコードをコピーして貼り付けたくはありませんでしたが、私が新しく、何が関連していて何が関連していないのかよくわからないという理由だけで、良いチャンクを入れるつもりです。(私は授業を受けていますが、リモートで、私の先生は...リモートの先生で、役に立たない.彼女は実際に私にここで尋ねるように言いました)

メソッドをインスタンス化する方法が欠けていると思います。オブジェクトがクラスから作成されたときに、メソッドがオブジェクトの一部になったと思いました。メソッドは、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();
        }
    }
}

コードを再確認した後、オブジェクトではなく静的クラスから呼び出すことについてあなたが言っていることがわかると思います。コンパイラが最初のフォームから作成するオブジェクトの名前を見つける方法はありますか?

4

2 に答える 2

1

「新しい」を使用してみてください

DisplayForm{
InitializeComponent();

EmployeeAddition = new EmployeeAdditionClass();

LoadEmployees(EmployeeAddition.GetList()); 
}
于 2013-07-10T01:58:47.207 に答える