Sign in to follow this  
Selacius

[.net] Global Variables

Recommended Posts

Alright, I am using a main form with a tab control. On each tab page is a user control. I'm trying to find a way to share databetween these usercontrols. How would I go about doing this. When I initialize the form, I get some basic stats for the player. These will be used in conjunction with other information later on each user control.

Share this post


Link to post
Share on other sites
I would make each one have a property like:

PlayerData data;
public PlayerData TheData { get { return data; } set { data = value; }}


And in your Form's constructor (or after creating/loading the data) set the property on each of your UserControls.

Share this post


Link to post
Share on other sites
You could create a class called Global and have variables defined as static there. Then you can access the variables from anywhere in your program as Global.MyVariable.

Eg.

class Global
{
public static int MyVariable;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Headkaze
You could create a class called Global and have variables defined as static there. Then you can access the variables from anywhere in your program as Global.MyVariable.

Eg.

class Global
{
public static int MyVariable;
}


OKey so that works when I do global.variable on my windows form, but for some reason it doesn't work when i try it on my user control. Any suggestions?

EDIT: I think this is because the value in the variable gets set from the windows form, so an empty value is being sent to the user control. How can I make it so that the form gets executed first so that the variables get their values and then the user control is executed.

Share this post


Link to post
Share on other sites
How can I check to see if the user control is made after the form? I think thats the exact problem I'm having, the user control is being made before or at the same time as the form.

Share this post


Link to post
Share on other sites
If you didn't write the code yourself to create the user controls, then they are likely created in the Form's constructor in the call to InitializeComponent(). So set the variable in the constructor before that call.

Share this post


Link to post
Share on other sites
Okey this is freaking pissing me off. I set all the values to the variables before I call the InitializeComponent(). Therefore once the user control gets initialized the variables should have their respective values. Well they aren't. I can add the control to the page only when I comment out the query which is giving me problems. Then I uncomment the query, debug and run and it works fine. Then once I close that and go back to editing, the error appears again. The error isn't preventing the solution from running, just stopping me from editing the main form. Here is the error message:

ERROR [23000] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE xvalue='-5' AND yvalue='-5'' at line 1
Hide

at System.Data.Odbc.OdbcConnection.HandleError(OdbcHandle hrHandle, RetCode retcode)
at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader, Object[] methodArguments, SQL_API odbcApiMethod)
at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader)
at System.Data.Odbc.OdbcCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Odbc.OdbcCommand.ExecuteReader()
at Arakaron_Game.explore..ctor() in C:\Documents and Settings\Ty\My Documents\Visual Studio 2005\Projects\Arakaron Game\Arakaron Game\explore.cs:line 50

Now the -5s should be instead the characters x and y coordinates -5. So for some odd reason, when I go back to editing the program, its giving me a problem, and setting the charx and chary values to 0.

I'm also getting a second error which is:
The variable 'explore' is either undeclared or was never assigned.
Hide Edit

at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.Error(IDesignerSerializationManager manager, String exceptionText, String helpLink)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager manager, String name, CodeExpression expression)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager manager, String name, CodeExpression expression)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeStatement(IDesignerSerializationManager manager, CodeStatement statement)

Now explore is the name of the user control, so I don't know wats up with that or how to fix it.

Share this post


Link to post
Share on other sites
The error is probably because the designer is calling the constructor on your Form, and the design-time environment is different so it has trouble connecting to the database. Your solution probably has a program.cs with the Main() entry point; you could move the code which loads the values to before the call to Application.Run(new Form1()).

A better solution would be to avoid making UserControls dependent on the state of global variables, by giving them a property (give it the [Browsable(false)] attribute) which you set with the data after you load it.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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