Reputation: 165
I have a list of objects on my main form - GUI
- and while I can access that list using a foreach
loop for example foreach (Employee emp in employees)
this allows me to access the employees within the list.
On the other form I have some code that requires it to access the list of employees.
So far I have tried to copy the private List<Employee> employees;
but it gives me a null reference exception obviously meaning that there's nothing in the list that has been copied over.
I'll provide a view of my code so you can have something to base your solution off:
Code Main form
private List<Employee> employees;
public Form1()
{
InitializeComponent();
employees = new List<Employee>();
}
Employee e1 = new Employee(MemberJob.Employee, "Name", MemberSkills.CPlus);
Added this bit of code in case I need to send some variables into the form
private void btnAddJob_Click(object sender, EventArgs e)
{
CreateAJob newForm2 = new CreateAJob();
newForm2.ShowDialog();
}
**Additional Form code **
private string _jobName = "";
private string _jobDifficulty = "";
private string _skillRequired = "";
private int _shiftsLeft = 0;
private List<Employee> employees; // tried to copy this over but there's nothing in it
public CreateAJob()
{
InitializeComponent();
}
public CreateAJob(string _jobName, string _skillRequired, int _shiftsLeft)
{
this._jobName = JobName;
this._skillRequired = SkillRequired;
this._shiftsLeft = ShiftsLeft;
}
private void Distribute(string _jobName, int _shiftsLeft, string _skillsRequired)
{
foreach (Employee emp in employees)
{
while (emp.Busy == true)
{
if (emp.Busy == false && emp.Skills.ToString() == _skillRequired)
{
emp.EmployeeWorkload = _jobName;
emp.ShiftsLeft = _shiftsLeft;
}
... additional code to finish method
Upvotes: 0
Views: 45
Reputation: 205549
Create another constructor and pass the Employee
list like this
CreateAJob form:
internal CreateAJob(List<Employee> employees)
: this() // Make sure the normal constructor is executed
{
this.employees = employees;
}
Main form:
private void btnAddJob_Click(object sender, EventArgs e)
{
CreateAJob newForm2 = new CreateAJob(employees);
newForm2.ShowDialog();
}
Upvotes: 2