473,402 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

How to define 'Global' variables in a .NET app?

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!
Nov 17 '05 #1
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/

Nov 17 '05 #2
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/

Nov 17 '05 #3
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/

Nov 17 '05 #4
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!

Nov 17 '05 #5
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!


Nov 17 '05 #6
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!



Nov 17 '05 #7
Have you tried a static field or property? In VB.NET they are even
called "shared".

MainForm.SomeVar

Nov 17 '05 #8
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!



Nov 17 '05 #9
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!



Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
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...
97
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
20
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...
6
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 ;
6
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...
33
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...
4
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...
10
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;
11
by: Sylvia A. | last post by:
How can I define global classes in web application ? Classes can be set to session variables ? Thanks
1
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
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...
0
tracyyun
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...
0
agi2029
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,...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.