Is it better to store your information in a single parent class or a central static class
Pergunta
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);
}
Solução
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.
Outras dicas
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.
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.