[.net] Global Variables

Started by
15 comments, last by Selacius 16 years, 10 months ago
Can i actually setup database queries before the application.run in the program.cs file? I just looked and I can, but I can't declare the required database variables anywhere because its a static class. This is turning out to be much more trouble than I thought.
Advertisement
What do you mean you can't declare them? Did you declare them as static?

It would probably be less of a pain to not use global variables at all...
It probably would be easier not ot use global variables, but i don't really want to run the same db query on multiple user controls to get the same information.
Quote:Original post by Selacius
It probably would be easier not ot use global variables, but i don't really want to run the same db query on multiple user controls to get the same information.


1. Memoize or cache results from database queries; refresh the data if the query (or one fetching the same piece of information, or an equivalent query -- depending on your exact needs) was last run over some threshold time ago. Maybe provide the ability to flush the cache for a result if necessary.
2. Maybe take a look at the observer pattern and delegates and events in C#. This is probably easier and less hacky than the above solution.
3.
a) Rethink how you're querying for the information. Why are you displaying duplicate information and how do you ensure that the duplicates are kept in sync? Otherwise, I fail to see what's wrong with jpetrie's solution.
b) Rethink how you're storing the information each control needs.
[TheUnbeliever]
Its not that I am displaying duplicate information. I will be displaying the players stats such as HP, MP, strength, etc, and then using these values in the battle formulas. I planned on using the global variables and get, set to ensure that the variables will always be up-todate. What I am ultimately trying to do is, when the player starts the game, all his/her data from the database gets "downloaded" into variables, arrays, etc. That way the amount of querying to the database would be a minimum and only necessary for none-player related queries such as whats in stock at stores, quest information, enemy information, etc. The global variables are working when I debug and run the game, but when I am in the editor, I am unable to modify my main windows form because one of the queries in the explore user control is sending back an error because it is not getting some of the global variables. I'm quite new at C# so while some of the suggestion may do things an easier and more effective way, I'm unsure of the speak that is being used. I learn by trial and error and now via tutorials.
You should put all the variables, arrays that are filled from the DB into a separate class, like a PlayerStats class. Then you create that object once when the program starts, and pass that single object to each UserControl. There's no duplicate database queries to worry about that way.
I've done that like you had suggested earlier. I have a class named global where all the variables are declared. So I guess now its just to figure out how to create that object first, then the windows form, and then after that is done the user controls. It needs to be initialized this way because the windows form is where all the variables declared in the global class are filled with their values.

This topic is closed to new replies.

Advertisement