0

I am wondering which is a better way to store information? A central static class or in a parent class?

The code for the way I am storing it now. I instanciate a new class everytime.

Parent Class:

public partial class frmEmployeeManager: Form
    {
        List<Employee> lstEmployees = List<Employee>();
        public frmEmployeeManager()
        {
            InitializeComponent();
        }
        public void updatePay(float Pay, int ID)
        {
            //Where ID = ID change the Pay
            //(Omitted the foreach loop here for brevity)
        }
        private void btnDisplayData_Click(object sender, EventArgs e)
        {
             frmUpdatePay dlgUpdatePay = new frmUpdatePay(this);
             dlgUpdatePay.ShowDialog();
        }

     }

Child Class:

public partial class frmUpdatePay : Form
{
    private frmEmployeeManager ParentEmployeeManager;

    public frmUpdatePay(frmEmployeeManager EmployeeManager)
    {
        InitializeComponent();
        ParentEmployeeManager = EmployeeManager;
    }
            AddPersonParent.updatePay(fltPayInput, intID);
}
John
  • 13,197
  • 7
  • 51
  • 101
  • 1
    It depends, and I don't think you've sufficiently described your scenario to determine which is best. Use a static class if you have a good reason for having one and only one instance of the class. Otherwise, use an instance class. Whether or not a class is static seems an entirely different issue as to how data is updated or if members are public. Maybe an example would clarify things. – Jonathan Wood Jun 14 '11 at 15:51
  • 3
    ArrayLists? You know you tagged this .net-3.0, right? How about some generics? – Andrew Jun 14 '11 at 15:51
  • 1
    What is the problem you are trying to solve? Some context will make this easier to understand / solve? What do these arraylists hold? What are they used for? etc. – Becuzz Jun 14 '11 at 15:54
  • @Andrew [msdn](http://msdn.microsoft.com/en-us/library/system.collections.arraylist(v=vs.85).aspx) I was under the impression that ArrayLists were a feature of the .NET Framework 3.0 – John Jun 14 '11 at 15:56
  • 1
    They are a feature in that you can use them, but they're pretty much obsolete since List came out in 2.0. I can't think of any reason to use them in new code today. – recursive Jun 14 '11 at 16:05
  • @johnthexiii I think they are mostly there for legacy compatibility. I don't know of anything that you can do with an ArrayList that you cannot do at least as well with List. – Andrew Jun 14 '11 at 16:12

3 Answers3

3

Taking a stab in the dark (since I don't know exactly what you are trying to accomplish), I would make an instantiated class and use a singleton pattern.

Becuzz
  • 6,846
  • 26
  • 39
1

I have gotten myself into trouble before when I used a static list that held the "state" of thing, and I found myself adding static functions to "clear" or "update" the list, etc. So I learned to only use static classes or lists or variables for things that are, well, static-- non-changing.

If you are keeping objects in the list that can change, I would go the instantiated route.

Updated

Now that I see your list is an employee list, converting it to a static basically makes it's a global variable. Global variables are not good. I found this answer which summarizes it pretty well.

Community
  • 1
  • 1
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • And what, create a whole new object to represent the state every time you want to change it? If you need to clear or update the state with a static object, you'll need the same thing for your instantiated class too. Don't really see the point you're (I'm presuming) trying to make. – Blindy Jun 14 '11 at 16:02
  • @Blindy I took a stab in the dark at what the OP is using it for. He clearly needs to add some context to his question. His question is basically asking, which one is better, but that's assuming one way is better than the other every time, and that's just not true. It depends. – LarsTech Jun 14 '11 at 16:44
  • You understood the question would you care to elaborate on the trouble you could get into? – John Jun 15 '11 at 18:11
1

Personally I would (and do) use a central static class. Both choices break the OO principles, but at least the central static class approach doens't expose the inner workings of my forms to the outside.

Blindy
  • 65,249
  • 10
  • 91
  • 131
  • Out of curiosity could you elaborate on that? – John Jun 15 '11 at 18:08
  • Oh easy: both break encapsulation by exposing the object's inner workings to the outside world. I mentioned that. Not sure what more you need me to say on the topic. I suggest reading an in-depth article on object oriented programming. A quick Google search gave me http://codebetter.com/raymondlewallen/2005/07/19/4-major-principles-of-object-oriented-programming/ for example. – Blindy Jun 15 '11 at 19:18