![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2006
Location: Sweden
Posts: 13
Rep Power: 0
![]() |
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! |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
If it's public, then one can access it via "frmMain.x".
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Apr 2006
Location: Sweden
Posts: 13
Rep Power: 0
![]() |
|
|
|
|
|
|
#4 |
|
Expert Programmer
|
could you please post your code so we could see where you have put it
__________________
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Apr 2006
Location: Sweden
Posts: 13
Rep Power: 0
![]() |
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. Last edited by MisterPoppy; Mar 24th, 2007 at 5:02 PM. |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 | |
|
Newbie
Join Date: Apr 2006
Location: Sweden
Posts: 13
Rep Power: 0
![]() |
Quote:
![]() |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| way to make a function execute at time 'x' | badbasser98 | C++ | 10 | Apr 5th, 2006 8:14 AM |
| Visual forms in C++? | gj15987 | C++ | 2 | Aug 20th, 2005 9:31 PM |
| major class project | Dark Flare Knight | Java | 9 | Apr 29th, 2005 3:45 PM |
| drop down menus, forms and php | mitch_j | PHP | 3 | Apr 8th, 2005 12:59 AM |