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

  • Thread starter Thread starter John Smith
  • Start date Start date
J

John Smith

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!
 
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. ;)
 
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.
 
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 said:
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 said:
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 said:
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 said:
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 said:
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 said:
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 said:
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 said:
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 said:
I'm curious, Bob, what would you use for application scope (globa)
variables or what have you?

Scott



Bob Powell said:
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 said:
Create a module, declare the variable Public. Better yet, create a class
object and put what you need in there as a property.

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!
 
Back
Top