OmegaNine
OmegaNine

Reputation: 371

Can I pass a List of objects from one Form to another in C#?

I have setup an Class that holds names/Numbers/ect, and created a List of objects to hold them. I want to display them on a second Form in a ListView. Right now I am using a getter like...

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

Then in the second Form's constructor I am using the Form1.GetEmpList() like...

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

I am getting and error saying "an object reference is required for the nonstatic field". The method I am calling is non-static, and returns a reference to the List in the Form1 class. I have even tried to just make the List public and calling using Form1.List but it still gives me the same error.

Is there a way to pass a List between Form classes or is it impossible?

EDIT: Poeple said that more information was needed. I didn't want to copy paste all the code here, but I am going to put a good chunk just because I am new and not quite sure what is relevant and what is not. (I am taking a class, but remotely, and my teacher is...well a remote teacher, useless. She actually told me to ask here)

I guess I am missing how to instatize a method, I thought that when the object was created from the class the method became part of the object. The Method is part for the Form1 (Renamed but thats what it is) class/object. I will dumb the code under here, I don't know if that is frowned upon; if so I am sorry.

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();
        }
    }    
}

This is the first Form class to load...

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();
        }
    }
}

After relooking at the code I think I might see what you are saying about calling it from a static class, not an object. Is there a way to find the name of the Object that the Compiler creates from the first Form?

Upvotes: 1

Views: 4742

Answers (2)

Craig Suchanec
Craig Suchanec

Reputation: 10804

The first problem you are having is you are treating the GetEmpList() function as an static method. It is not static and probably shouldn't be static. That is why people are saying you need to create an instance of it. However, looking at what you are asking the way you are going about this is probably flawed in a more fundamental way that just creating a new version of the form will solve. The problem is you want to pass model data between forms. Without more information it is really hard to tell how you want this all to go together. However you probably already have an instance of the EmployeeAddition form and don't need to create another instance inside the constructor of your DisplayForm. Instead what you should do is make the LoadEmployees method public and pass the results of GetEmpList() into that.

So your structure would be more like

public class EmployeeAddition : Form {

...

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

...

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

...

}

Of course this structure may be slightly off as I'm guessing at which form will own which form but this is closer to what you want.

Upvotes: 2

user2530748
user2530748

Reputation: 260

try use "new"

DisplayForm{
InitializeComponent();

EmployeeAddition = new EmployeeAdditionClass();

LoadEmployees(EmployeeAddition.GetList()); 
}

Upvotes: 1

Related Questions