Home All Groups Group Topic Archive Search About

static class Program in Program.cs in VS 2005

Author
2 Mar 2006 3:25 PM
Michael.Suarez
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?

Author
2 Mar 2006 3:41 PM
David White
Michael.Sua***@gmail.com wrote:

> 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.
Author
2 Mar 2006 3:55 PM
Michael.Suarez
Thanks for the reply.

Any specific reason you wouldnt recommend this?
Author
2 Mar 2006 4:23 PM
Nick Hounsome
<Michael.Sua***@gmail.com> wrote in message
news:1141313140.948692.314450@i39g2000cwa.googlegroups.com...
> 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.
Author
2 Mar 2006 10:22 PM
William Stacey [MVP]
| 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.

AddThis Social Bookmark Button