Jump to content
  • Advertisement
Sign in to follow this  
Selacius

[.net] Global Variables

This topic is 4123 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

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
Advertisement
Pass the "database" object to the user controls via their constructors or some other kind of initialization function.

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
Assuming the user controls are created after the form, set the global in the form's constructor.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!