Sign in to follow this  

Using static variables in C#

This topic is 2491 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm using XNA for writing a game, and in the Game1 class I have a public static variable called screenWidth (and screenHeight).

I tend to use these quite often among other classes in my Solution, and whenever I call them I have to type Game1.screenWidth and such. Is there a way to sort of import them over? a using statement perhaps? I'd really like to be able to call all static variables from the Game1 class (and other classes) without having to first write the class name before it.

Cheers for your help!

Share this post


Link to post
Share on other sites
[quote name='dechorus' timestamp='1298322800' post='4777215']
I'm using XNA for writing a game, and in the Game1 class I have a public static variable called screenWidth (and screenHeight).

I tend to use these quite often among other classes in my Solution, and whenever I call them I have to type Game1.screenWidth and such. Is there a way to sort of import them over? a using statement perhaps? I'd really like to be able to call all static variables from the Game1 class (and other classes) without having to first write the class name before it.

Cheers for your help!
[/quote]

You're wanting global variables. Make a class, call it something like class globals. Make it a public static class and make all the members either static or const. Voila, everyone can access it now just use global.screenWidth or w/e (I use w and h for width and height cause I'm lazy XD).

Try not to make too many globals, it's bad practice.

Share this post


Link to post
Share on other sites
[quote name='slynk' timestamp='1298323550' post='4777222']
You're wanting global variables. Make a class, call it something like class globals. Make it a public static class and make all the members either static or const. Voila, everyone can access it now just use global.screenWidth or w/e (I use w and h for width and height cause I'm lazy XD).
[/quote]
I see! I don't think I've ever used static classes. Does it imply that you can't create an instance of it?

[quote name='slynk' timestamp='1298323550' post='4777222']
Try not to make too many globals, it's bad practice.
[/quote]
I'll keep that in mind. It would make sense to have screenwidth and height as globals though, hey?

And so I don't suppose there's a way of just writing screenWidth as opposed to writing globals.screenWidth ? Or globals.W :P

Share this post


Link to post
Share on other sites
[quote name='dechorus' timestamp='1298324394' post='4777232']
[quote name='slynk' timestamp='1298323550' post='4777222']
You're wanting global variables. Make a class, call it something like class globals. Make it a public static class and make all the members either static or const. Voila, everyone can access it now just use global.screenWidth or w/e (I use w and h for width and height cause I'm lazy XD).
[/quote]
I see! I don't think I've ever used static classes. Does it imply that you can't create an instance of it?

[quote name='slynk' timestamp='1298323550' post='4777222']
Try not to make too many globals, it's bad practice.
[/quote]
I'll keep that in mind. It would make sense to have screenwidth and height as globals though, hey?

And so I don't suppose there's a way of just writing screenWidth as opposed to writing globals.screenWidth ? Or globals.W :P
[/quote]

Static indicates that the variable or function is a class variable/function instead of a member variable/function. You could instantiate a static class but there is no point to it as all the instances will have the same values for their static variables all other variables of that class will be instance dependent.

There is no way to do that in C#, heck there isn't even a way to do that in C++ if the static var is encapsulated in a class. There are certain circumstances where a global makes more sense then an instance, another way round this problem is the use of a singleton in which case only one function needs to be static and thats to get the instance.

Share this post


Link to post
Share on other sites
[quote name='dechorus' timestamp='1298324394' post='4777232']I see! I don't think I've ever used static classes. Does it imply that you can't create an instance of it?[/quote]

Static means that from the time the program is loaded to the time you turn it off, that class will be in memory. It's always there (which is why anything in your project can see it).


[quote name='dechorus' timestamp='1298324394' post='4777232']
I'll keep that in mind. It would make sense to have screenwidth and height as globals though, hey?[/quote]

Yes, at least in XNA it makes since. Using graphics.Viewport.Width all the time is annoying.

I also keep my math functions I've made in the global class so that it doesn't litter up other classes.
And I keep things like, a flag that states whether I've set the screen ratio as well as the screen ratio itself.
I also just keep a global System.Random generator and a few constants.

Don't take what I said to mean"try to keep only 2-4 variables global", just think about it. Is it something EVERYTHING needs access to? Is it something that affects the whole program? Is it a constant? Do you have functions that don't belong as part of a class?

Sometimes it makes more since to put it in a global class.

[quote name='dechorus' timestamp='1298324394' post='4777232']And so I don't suppose there's a way of just writing screenWidth as opposed to writing globals.screenWidth ? Or globals.W :P
[/quote]

Nope, not in C#. At least, not that I've found. Make it short if you want call the class "g" lol then just type g.whatEverYouWant

Share this post


Link to post
Share on other sites
g.W is a lot better than writing Game1.screenWidth, or even worse getting it from the Viewport.

Thanks a lot for all the helpful information! I feel I can make cleaner code using the above methods.

Share this post


Link to post
Share on other sites

This topic is 2491 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this