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)Application.OpenForms[0]).<MyVariableName>.
Is there a more elegant or better way? Btw, the variable I'd like to share
is an open SqlConnection.
Thanks! 9 5511
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 ApplicationSession : Hashtable
{
private static ApplicationSession instance = new
ApplicationSession();
private ApplicationSession()
{
}
public static ApplicationSession 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**********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.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 ApplicationSession : Hashtable { private static ApplicationSession instance = new ApplicationSession();
private ApplicationSession() { }
public static ApplicationSession 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.ApplicationBlock.Data. 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**************@tk2msftngp13.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)Application.OpenForms[0]).<MyVariableName>.
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******@newsgroups.nospam> wrote in message
news:ed**************@TK2MSFTNGP14.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**************@tk2msftngp13.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)Application.OpenForms[0]).<MyVariableName>.
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@_spamkiller_.bobpowell.net> wrote in message
news:ez**************@TK2MSFTNGP12.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******@newsgroups.nospam> wrote in message news:ed**************@TK2MSFTNGP14.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**************@tk2msftngp13.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)Application.OpenForms[0]).<MyVariableName>.
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.SomeVar
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@_spamkiller_.bobpowell.net> wrote in message
news:ez**************@TK2MSFTNGP12.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******@newsgroups.nospam> wrote in message news:ed**************@TK2MSFTNGP14.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**************@tk2msftngp13.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)Application.OpenForms[0]).<MyVariableName>.
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*************@TK2MSFTNGP10.phx.gbl... I'm curious, Bob, what would you use for application scope (globa) variables or what have you?
Scott "Bob Powell [MVP]" <bob@_spamkiller_.bobpowell.net> wrote in message news:ez**************@TK2MSFTNGP12.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******@newsgroups.nospam> wrote in message news:ed**************@TK2MSFTNGP14.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**************@tk2msftngp13.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)Application.OpenForms[0]).<MyVariableName>.
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
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...
| |