When you create a new windows application in VS 2005, you have a
Program.cs file which declares the class Program as static. Is there
any reason that this MUST be static? I want to take the work static out
so that I can declare a variable that is a member of Program so that it
can be accessed globally by all other forms in the project. Any
forseeable problems in doing this?
David White - 02 Mar 2006 15:41 GMT
> When you create a new windows application in VS 2005, you have a
> Program.cs file which declares the class Program as static. Is there
> any reason that this MUST be static? I want to take the work static out
> so that I can declare a variable that is a member of Program so that it
> can be accessed globally by all other forms in the project. Any
> forseeable problems in doing this?
I have removed the static attribute from this class and see no problems with
doing so. The main() method must remain static, I believe.
Of course, while I don't recommend the practice, you should be able to simply
add a static variable to the class as-is. With the correct scope, it could be
visible by callers in your application.
Michael.Suarez@gmail.com - 02 Mar 2006 15:55 GMT
Thanks for the reply.
Any specific reason you wouldnt recommend this?
Nick Hounsome - 02 Mar 2006 16:23 GMT
> When you create a new windows application in VS 2005, you have a
> Program.cs file which declares the class Program as static. Is there
> any reason that this MUST be static? I want to take the work static out
> so that I can declare a variable that is a member of Program so that it
> can be accessed globally by all other forms in the project. Any
> forseeable problems in doing this?
You can do what you want with it as it is never regenerated and there is no
magic about it.
I would suggest however that instead you create your own class and view
Program as a bit of template magic. That way, if MS, should decide to put
anything else in there in the future it will not cause a problem.
William Stacey [MVP] - 02 Mar 2006 22:22 GMT
| I would suggest however that instead you create your own class and view
| Program as a bit of template magic. That way, if MS, should decide to put
| anything else in there in the future it will not cause a problem.
Agree.