Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 24th, 2007, 2:16 PM   #1
MisterPoppy
Newbie
 
Join Date: Apr 2006
Location: Sweden
Posts: 13
Rep Power: 0 MisterPoppy is on a distinguished road
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!
MisterPoppy is offline   Reply With Quote
Old Mar 24th, 2007, 3:28 PM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
If it's public, then one can access it via "frmMain.x".
Arevos is offline   Reply With Quote
Old Mar 24th, 2007, 4:22 PM   #3
MisterPoppy
Newbie
 
Join Date: Apr 2006
Location: Sweden
Posts: 13
Rep Power: 0 MisterPoppy is on a distinguished road
Quote:
Originally Posted by Arevos View Post
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.
MisterPoppy is offline   Reply With Quote
Old Mar 24th, 2007, 4:26 PM   #4
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 597
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
could you please post your code so we could see where you have put it
__________________
Quote:
Originally Posted by DaWei View Post
Well, it's better than Pen Islands url....;)

crawforddavid2006 is offline   Reply With Quote
Old Mar 24th, 2007, 4:51 PM   #5
MisterPoppy
Newbie
 
Join Date: Apr 2006
Location: Sweden
Posts: 13
Rep Power: 0 MisterPoppy is on a distinguished road
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.
MisterPoppy is offline   Reply With Quote
Old Mar 24th, 2007, 5:04 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Mar 24th, 2007, 5:29 PM   #7
MisterPoppy
Newbie
 
Join Date: Apr 2006
Location: Sweden
Posts: 13
Rep Power: 0 MisterPoppy is on a distinguished road
Quote:
Originally Posted by DaWei View Post
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!
MisterPoppy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:13 AM.

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