Global Collection access

Cags

Contributor
Joined
Feb 19, 2004
Messages
695
Location
Melton Mowbray, England
As anybody that reads all threads will know I recently decided to build a food calculator. This application has two main collection in it, a collection of recipes and a collection of consumables. The app currently consists of 3 forms.

MainForm
AddConsumableForm
AddRecipeForm

My question is, would it be better to store the collections in a public static class that would be accessible from all forms, or would it be better to pass a reference to the AddForms whenever they are Shown?
 
Cags said:
...would it be better to store the collections in a public static class that would be accessible from all forms...
public static class = global = :(

You want to reduce coupling as much as possible between your data structures and your UI. The best situation, in most cases, is one where your add form doesn't really add anything, it just collects informaiton and passes it on to something that knows how to add stuff to your collection that is external to the UI. That way, say you want to take this to the web or command line (or a better GUI in the future). All you have to do is write the web/command-line interface and pitch the correct information to the classes. If you were to have the AddForms do the adding, then that would be one more thing you would need to write in you new UI. Your second option is definately the better of the two, but I don't know if it is the best becuase I don't know everything about your program. Decoupling is the the general idea.

PLUS, designing it nice and decoupled will let you test more parts of your system which is a big part of the pie when designing -- designing with an eye towards testing.
 
The way I have it working at the moment is similar to a FileDialog. The form is called with ShowDialog, it collects the information and creates an instance of either Consumable or Recipe and stores it in a variable which is accessible as a property. Then back on the main form i check the dialog result followed by the property.
C#:
AddConsumableForm addForm = new AddConsumableForm();
if(addForm.ShowDialog == DialogResult.Ok)
{
    _myConsumables.Add(addForm.CreatedConsumable);
}
 
Back
Top