1

This is a noob question, I apologize. I have been trying for a while to figure out how to add an object to this array. I have an Employee class, and a Salary class that inherits from Emp, and an Hourly class that does. I created an array like this,

        public Employee[] _Employees;
        ...    
        Employee[] _Employees = new Employee[]{ 
        new Hourly("1000", "Harry","Potter", "L.", "Privet Drive", "201-9090", "40.00", "12.00"),
        new Salary("2201", "A.", "A.", "Dumbledore", "Hogewarts", "803-1230", "1200"),
        new Hourly("3330", "R.","Weasley", "R.", "The Burrow", "892-2000", "40", "10.00"),
        new Salary("4040", "R.", "R.", "Hagrid", "Hogwarts", "910-8765", "1000")
                                                    };

And now I want to add objects to the array that I have read in from a text file. I am doing it like this right now;

                      if(rstring == "H")
                {
                    string fullName = data.ReadLine();
                    string empNum = data.ReadLine();
                    string address = data.ReadLine();
                    string phoneNum = data.ReadLine();
                    string hrWorked = data.ReadLine();
                    string hrRate = data.ReadLine();

                    string[] splitName = fullName.Split(new Char[] { ' ' });

                    string fName = splitName[0];
                    string mName = splitName[1];
                    string lName = splitName[2];

                     _MyForm._Employees = new Employee[] {new Hourly ( empNum, fName, mName, lName, address, phoneNum, hrWorked, hrRate ) };

which doesn't give me any error, but when I look at what is stored in the _Employees class, it just has the info from above and nothing else. Let me know if I need to explain myself better. Just let me know if I need to go about it another way, or what I need to do to add the read info to this _Employees class.

tshepang
  • 12,111
  • 21
  • 91
  • 136
SirRupertIII
  • 12,324
  • 20
  • 72
  • 121

3 Answers3

2

Instead of having an array, you should consider using a List<Employee>. This will let you add any number of elements to the list, without recreating it yourself:

private List<Employee> employees = new List<Employee>();
public IList<Employee> Employees { get { return this.employees; } }

// ...
this.employees.Add(new Hourly("1000", "Harry","Potter", "L.", "Privet Drive", "201-9090", "40.00", "12.00"));
// .. Add all

Then, when you want to add a new one, you can use:

_MyForm.Employees.Add(new Hourly ( empNum, fName, mName, lName, address, phoneNum, hrWorked, hrRate));
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
2

You'd be better off using a List in this case:

IList<Employee> _Employees;

List has the advantage of having an Add method which can be used to add new objects to the end of the array.

_MyForm._Employees.Add(new Hourly(...))

If you wanted to insert objects at other points in the collection there's also the Insert method which takes an index along with the object to be added.

Jamie Dixon
  • 53,019
  • 19
  • 125
  • 162
1

Use the List class as others have pointed out and the Add method. What you have done here:

_MyForm._Employees = new Employee[] 

in your example code is you've re-assigned your reference of MyForm._Employees to a new object of type Employee[], not added to it. Your previous reference containing your other valuable employees will have most likely been garbage collected (if nothing else in scope is referencing it).

I would steer clear of arrays to be honest. See this for more information.

Community
  • 1
  • 1
Jeb
  • 3,689
  • 5
  • 28
  • 45
  • Thanks a bunch for the info, here is an article I found in the link you provided [article](http://blogs.msdn.com/b/ericlippert/archive/2008/09/22/arrays-considered-somewhat-harmful.aspx) – SirRupertIII Sep 25 '12 at 16:26