skip to Main Content

What am I doing wrong? The second Employee on the second to last row is what is causing this error.

`private void DisplayButton_Click(object sender, EventArgs e)
    {
        // clear the Employee listbox
        EmployeesListBox.Items.Clear();

        // read employees from the file
        string fileName = "Employees.csv";
        StreamReader sr = new StreamReader(fileName);

        while ( sr.Peek() > -1 )
        {
            string line = sr.ReadLine();
            string[] parts = line.Split(',');
            string firstName = parts[0];
            string lastName = parts[1];
            string ssn = parts[2];
            DateTime hireDate = DateTime.Parse(parts[3]);
            Employee emp = Employee(firstName, lastName, ssn, hireDate);
            EmployeesListBox.Items.Add(emp);`
        }
        
       
        

I have rewrote my code a million times and followed my instructors instructions word for word.

2

Answers


  1. Chosen as BEST ANSWER

    You guys are life savers. I have had issue after issue for 4 days now. Thank you for the help.


  2. The Employee class is a reference type, which means that you need to use the new keyword when creating a new instance.

    Employee emp = new Employee(firstName, lastName, ssn, hireDate);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search