I want to share a variable between multiple forms in a Windows app. Where do
I declare the variable ?
I came up with one way that is to declare it as a member of the MainForm,
and then access it using
((MainForm)Appl ication.OpenFor ms[0]).<MyVariableNa me>.
Is there a more elegant or better way? Btw, the variable I'd like to share
is an open SqlConnection.
Thanks! 9 5525
Dear John,
There are few ways to share variables between multiple
forms but what i liked the most is that i made a Singleton class which
works as a Application session.
public class ApplicationSess ion : Hashtable
{
private static ApplicationSess ion instance = new
ApplicationSess ion();
private ApplicationSess ion()
{
}
public static ApplicationSess ion getInstance()
{
return instance;
}
public override void Add(object key, object value)
{
if (this.Contains( key) == true)
{
this.Remove(key );
}
base.Add(key, value);
}
}
It worked fine for my Project.
Regards,
Naveed Ahmad Bajwa http://bajoo.blogspot.com/
A nice solution, byt my advice for this specific problem would be to not
keep an open connection to the Sql server. It will scale horribly. Open
conn, do stuff, Close conn, repeat. ;)
--
Robert Jeppesen
Durius http://www.durius.com/
"Bajoo" <Na**********@g mail.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. . Dear John, There are few ways to share variables between multiple forms but what i liked the most is that i made a Singleton class which works as a Application session.
public class ApplicationSess ion : Hashtable { private static ApplicationSess ion instance = new ApplicationSess ion();
private ApplicationSess ion() { }
public static ApplicationSess ion getInstance() { return instance; }
public override void Add(object key, object value) { if (this.Contains( key) == true) { this.Remove(key ); } base.Add(key, value); } }
It worked fine for my Project.
Regards, Naveed Ahmad Bajwa http://bajoo.blogspot.com/
Yes robert you are Absolutly right. Connection should be closed. I was
mentioning how to share variables/Objects between forms. John I'll
advice you to use some kind of Block/Framework like
Microsoft.Appli cationBlock.Dat a. Its really good ,It handles a lot for
you.
Regards,
Naveed Ahmad Bajwa http://bajoo.blogspot.com/
Create a module, declare the variable Public. Better yet, create a class
object and put what you need in there as a property.
"John Smith" <js****@AOL.COM > wrote in message
news:uL******** ******@tk2msftn gp13.phx.gbl... I want to share a variable between multiple forms in a Windows app. Where do I declare the variable ?
I came up with one way that is to declare it as a member of the MainForm, and then access it using ((MainForm)Appl ication.OpenFor ms[0]).<MyVariableNa me>.
Is there a more elegant or better way? Btw, the variable I'd like to share is an open SqlConnection.
Thanks!
Sorry to say but this is exactly the wrong approach.
Object oriented code has absolutely no good excuse for a public field.
You can declare a singleton class and expose properties if you absolutely
have to do this.
--
Bob Powell [MVP]
Visual C#, System.Drawing
Ramuseco Limited .NET consulting http://www.ramuseco.com
Find great Windows Forms articles in Windows Forms Tips and Tricks http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
"Earl" <br******@newsg roups.nospam> wrote in message
news:ed******** ******@TK2MSFTN GP14.phx.gbl... Create a module, declare the variable Public. Better yet, create a class object and put what you need in there as a property.
"John Smith" <js****@AOL.COM > wrote in message news:uL******** ******@tk2msftn gp13.phx.gbl...I want to share a variable between multiple forms in a Windows app. Where do I declare the variable ?
I came up with one way that is to declare it as a member of the MainForm, and then access it using ((MainForm)Appl ication.OpenFor ms[0]).<MyVariableNa me>.
Is there a more elegant or better way? Btw, the variable I'd like to share is an open SqlConnection.
Thanks!
I'm curious, Bob, what would you use for application scope (globa) variables
or what have you?
Scott
"Bob Powell [MVP]" <bob@_spamkille r_.bobpowell.ne t> wrote in message
news:ez******** ******@TK2MSFTN GP12.phx.gbl... Sorry to say but this is exactly the wrong approach.
Object oriented code has absolutely no good excuse for a public field.
You can declare a singleton class and expose properties if you absolutely have to do this.
-- Bob Powell [MVP] Visual C#, System.Drawing
Ramuseco Limited .NET consulting http://www.ramuseco.com
Find great Windows Forms articles in Windows Forms Tips and Tricks http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET. Subscribe to the RSS feeds provided and never miss a new article. "Earl" <br******@newsg roups.nospam> wrote in message news:ed******** ******@TK2MSFTN GP14.phx.gbl... Create a module, declare the variable Public. Better yet, create a class object and put what you need in there as a property.
"John Smith" <js****@AOL.COM > wrote in message news:uL******** ******@tk2msftn gp13.phx.gbl...I want to share a variable between multiple forms in a Windows app. Where do I declare the variable ?
I came up with one way that is to declare it as a member of the MainForm, and then access it using ((MainForm)Appl ication.OpenFor ms[0]).<MyVariableNa me>.
Is there a more elegant or better way? Btw, the variable I'd like to share is an open SqlConnection.
Thanks!
Have you tried a static field or property? In VB.NET they are even
called "shared".
MainForm.SomeVa r
Hehe. Bob, I didn't tell him if sharing a variable public was right or
wrong, just an answer to his question and then a better way of doing it ...
what's wrong with "create a class object and put what you need in there as a
property."?
"Bob Powell [MVP]" <bob@_spamkille r_.bobpowell.ne t> wrote in message
news:ez******** ******@TK2MSFTN GP12.phx.gbl... Sorry to say but this is exactly the wrong approach.
Object oriented code has absolutely no good excuse for a public field.
You can declare a singleton class and expose properties if you absolutely have to do this.
-- Bob Powell [MVP] Visual C#, System.Drawing
Ramuseco Limited .NET consulting http://www.ramuseco.com
Find great Windows Forms articles in Windows Forms Tips and Tricks http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET. Subscribe to the RSS feeds provided and never miss a new article. "Earl" <br******@newsg roups.nospam> wrote in message news:ed******** ******@TK2MSFTN GP14.phx.gbl... Create a module, declare the variable Public. Better yet, create a class object and put what you need in there as a property.
"John Smith" <js****@AOL.COM > wrote in message news:uL******** ******@tk2msftn gp13.phx.gbl...I want to share a variable between multiple forms in a Windows app. Where do I declare the variable ?
I came up with one way that is to declare it as a member of the MainForm, and then access it using ((MainForm)Appl ication.OpenFor ms[0]).<MyVariableNa me>.
Is there a more elegant or better way? Btw, the variable I'd like to share is an open SqlConnection.
Thanks!
I think I can safely answer that for you -- he wouldn't. Everything would be
a property in a class.
"Scott Coonce" <sd******@gmail .HEY_YOU.com> wrote in message
news:OE******** *****@TK2MSFTNG P10.phx.gbl... I'm curious, Bob, what would you use for application scope (globa) variables or what have you?
Scott "Bob Powell [MVP]" <bob@_spamkille r_.bobpowell.ne t> wrote in message news:ez******** ******@TK2MSFTN GP12.phx.gbl... Sorry to say but this is exactly the wrong approach.
Object oriented code has absolutely no good excuse for a public field.
You can declare a singleton class and expose properties if you absolutely have to do this.
-- Bob Powell [MVP] Visual C#, System.Drawing
Ramuseco Limited .NET consulting http://www.ramuseco.com
Find great Windows Forms articles in Windows Forms Tips and Tricks http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET. Subscribe to the RSS feeds provided and never miss a new article. "Earl" <br******@newsg roups.nospam> wrote in message news:ed******** ******@TK2MSFTN GP14.phx.gbl... Create a module, declare the variable Public. Better yet, create a class object and put what you need in there as a property.
"John Smith" <js****@AOL.COM > wrote in message news:uL******** ******@tk2msftn gp13.phx.gbl... I want to share a variable between multiple forms in a Windows app. Where do I declare the variable ?
I came up with one way that is to declare it as a member of the MainForm, and then access it using ((MainForm)Appl ication.OpenFor ms[0]).<MyVariableNa me>.
Is there a more elegant or better way? Btw, the variable I'd like to share is an open SqlConnection.
Thanks!
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: David WOO |
last post by:
Hi,
I am a newbie on C++, I need to define some global variables which should be
accessible to most classes. In the mean time, I don't won't the global
variables be modified freely at most of these classes. I know there is a
pattern called singleton can more or less do such a trick. I am wondering is
this the best way to do it (regarding the convenience and safety), as this
is such a fundamental thing, I believe most of you have a say...
|
by: s |
last post by:
Can I do this:
#define MYSTRING "ABC"
..
..
..
char mychar = MYSTRING;
..
..
..
|
by: Dead RAM |
last post by:
Hey people, i'll try to keep this short ;)
Here is what I want to type (or at least close too)...
#define VER_BUILD 1
#define STR_VER_BUILD "VER_BUILD"
But what happends is the preprocessor see the quots in STR_VER_BUILD and
replaces that text with "VER_BUILD"...
I need it to see the VER_BUILD and replace it with 1, and only after doing
|
by: David T. Ashley |
last post by:
Hi,
In my project, I typically declare and define variables in the .H
file, i.e.
DECMOD_MAIN UINT8 can_message_201_status_global
#ifdef MODULE_MAIN
= HAS_NEVER_BEEN_RECEIVED
#endif
;
|
by: Gunnar Beushausen |
last post by:
Hi!
I need a class to store the users data (ID, name etc.) that is accessible
from anywhere.
At application startup the class gets filled with its data about the user.
But how can i access this data from all other classes?
Normally to get access, i would have to say something like UserData *UD =
new UserData; But this way a new class gets instantieted. What can i do to
| |
by: MLH |
last post by:
I've read some posts indicating that having tons of GV's in
an Access app is a bad idea. Personally, I love GVs and I
use them (possibly abuse them) all the time for everything
imaginable - have been for years. If the machine has memory
to spare and windows can use it - I'm thinking "Why not?"
I was wondering what some of you have to say about that,
particularly any severe "gotchas" you've had the unfortunate
experience to contend with.
|
by: Minfu Lu |
last post by:
Hi,
I need define a global COM object in a VC++8 project. But when I compile it,
I get a error message:
error C3145:'ComLibC1': global or static variable may not have managed type
MYCOMLib::Class1^ (MYCOMLib is a COM Wrapper built in VC++6).
My code looks like this:
namespace MyApp
{
|
by: athanasios.silis |
last post by:
Hello everyone,
i am attempting to make a structure
#include "globalVars.h"
struct myStruct{
int offset;
unsigned char uChars;
} saveVars, getVars;
|
by: Sylvia A. |
last post by:
How can I define global classes in web application ?
Classes can be set to session variables ?
Thanks
|
by: Bryan Parkoff |
last post by:
An object can be defined using a class. The class contains variables
and functions. It has a pointer to bind variables and functions. If I want
to create more than one object. The class can have two objects with a
separate pointer. However, functions are always shared with one or more
objects, but each object has its own separate variables. It allows to
reduce unreadable messy source code and bugs.
However, I do not want to use a...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |