U
uncleSally
Hi,
This post is partly inspired by Milop's recent post here, "Sharing
Data," where there was some mention of the use of static variables or
objects, but I feel the issue I want to raise is too OT to post on
that thread.
As a programmer I am well aware of the value of encapsulation, and the
dangers of "global" variables and objects. I'm also aware of
techniques for "injection" of references as needed into classes/
objects created at run-time.
However, I seem to come across many cases where there is clearly one-
and-only-one object or piece of data, or one set of methods, that
needs to be accessed by, for example, a host of UserControls, or a
bunch of "child" forms. I'm not really into "design patterns," but I
guess you could call that a "singleton pattern."
I seem to have no problem defining a static class in WinForms (C#),
and implementing something like public static dictionaries, methods,
etc.
I personally like the separating out of functionality from the "main
form" that comes from using one or more static classes to hold
logically related data sets or groups of functions.
I have experimented, for fun, in taking a WinForm app and going into
the "main form" program.cs file and changing the constructor like so :
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// formController is a public static class ... does not
inherit from Form ...
// its 'makeNewForm method creates a new Form, sets its
title from the
// string argument supplied, and make it visible via 'Show
formController.makeNewForm("the first form");
Application.Run();
so that it invokes a method in a Static class to create the "main
form" (of course you must then implement a way to make sure the
Application is closed when your final form open is closed which is
simple). To me I find such a technique useful if I have an app design
where I want multiple windows each one of which is an "equal" : i.e.,
no "main form."
I know such solutions are not appropriate for multi-user scenarios,
etc.
But, I am curious to know how you might view such use of static
classes.
thanks, Bill
This post is partly inspired by Milop's recent post here, "Sharing
Data," where there was some mention of the use of static variables or
objects, but I feel the issue I want to raise is too OT to post on
that thread.
As a programmer I am well aware of the value of encapsulation, and the
dangers of "global" variables and objects. I'm also aware of
techniques for "injection" of references as needed into classes/
objects created at run-time.
However, I seem to come across many cases where there is clearly one-
and-only-one object or piece of data, or one set of methods, that
needs to be accessed by, for example, a host of UserControls, or a
bunch of "child" forms. I'm not really into "design patterns," but I
guess you could call that a "singleton pattern."
I seem to have no problem defining a static class in WinForms (C#),
and implementing something like public static dictionaries, methods,
etc.
I personally like the separating out of functionality from the "main
form" that comes from using one or more static classes to hold
logically related data sets or groups of functions.
I have experimented, for fun, in taking a WinForm app and going into
the "main form" program.cs file and changing the constructor like so :
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// formController is a public static class ... does not
inherit from Form ...
// its 'makeNewForm method creates a new Form, sets its
title from the
// string argument supplied, and make it visible via 'Show
formController.makeNewForm("the first form");
Application.Run();
so that it invokes a method in a Static class to create the "main
form" (of course you must then implement a way to make sure the
Application is closed when your final form open is closed which is
simple). To me I find such a technique useful if I have an app design
where I want multiple windows each one of which is an "equal" : i.e.,
no "main form."
I know such solutions are not appropriate for multi-user scenarios,
etc.
But, I am curious to know how you might view such use of static
classes.
thanks, Bill