Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   How to make a object/variable accessible by all forms? (http://www.programmingforums.org/showthread.php?t=12865)

MisterPoppy Mar 24th, 2007 2:16 PM

How to make a object/variable accessible by all forms?
 
Hello everyone!

I would like to know how to make a object or a variable accessible by all forms in a c# application.

For example:

If you write: public int x = 1; in frmMain for example. Then how do I access x in, for example, a form called frmSettings?

Thanks in advance!

Arevos Mar 24th, 2007 3:28 PM

If it's public, then one can access it via "frmMain.x".

MisterPoppy Mar 24th, 2007 4:22 PM

Quote:

Originally Posted by Arevos (Post 125671)
If it's public, then one can access it via "frmMain.x".

Thanks for your answer, however, that doesn't work for some reason. I thought it would to, but when I write, as you suggested, the variable doesn't show up.

crawforddavid2006 Mar 24th, 2007 4:26 PM

could you please post your code so we could see where you have put it

MisterPoppy Mar 24th, 2007 4:51 PM

Thanks for your replies guys, but I found a way to get it to work. I just hade to include the namespace and then I could access the variables. HOWEVER, the downside with doing it the way I just described is that I have to declear all variables I have to share as static which I'm not very fond of. So if anyone has a better idea I'd prefer to hear it.

However, I do have another question as well. It does not regard the problem this topic was made for, but I'll post it here anyways.

If I have two forms, one with just a button that opens form2. If I open form2 and write something in the textbox (which I've put there), after I've closed the form, the data (the text i wrote in the textbox) is deleted. My question is: How do I get the data to stay even if I close the form and open it later?

Thanks once again.

P.S
Sorry for my english, I'm not from an english speaking country and I'm very tired which makes it hard for me to write grammatically correct english. Please bear with me.

DaWei Mar 24th, 2007 5:04 PM

Mr. Poppy, we 'native English speakers' should have such good grammar, don't worry about it. If you have data you've gathered from a form that you want to keep around, just save it in a place that isn't dependent on the form for its existence.

MisterPoppy Mar 24th, 2007 5:29 PM

Quote:

Originally Posted by DaWei (Post 125681)
Mr. Poppy, we 'native English speakers' should have such good grammar, don't worry about it. If you have data you've gathered from a form that you want to keep around, just save it in a place that isn't dependent on the form for its existence.

Aah so simple, thanks for the help! :)


All times are GMT -5. The time now is 2:08 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC