Solved

Global Variables/Classes in C#

Posted on 2004-10-18
16
2,252 Views
Last Modified: 2008-02-26
Greetings,
I am wondering how to load up global classes/variables in C# at the launch of an application that I can access from any form in the program.

Thanks!
0
Comment
Question by:duerra
  • 7
  • 6
  • 2
  • +1
16 Comments
 
LVL 3

Expert Comment

by:servicegroup
ID: 12338295
You can use the web.config file to store "global variables"

First insert the data in web.config

After the "</system.web>" line, insert these values:

<appSettings>
      <add key="adminEmail" value="admin@admin.com"/>
</appSettings>

Where the variable name is called adminEmail.

In the csharp side

using System.Configuration; //place this line before the namespace declearation

string admin = ConfigurationSettings.AppSettings["adminEmail"]; //after this, the variable admin will have the value "admin@admin.com"

Let me know if that solves your problem

-Jim
0
 
LVL 18

Expert Comment

by:tusharashah
ID: 12338432
Setting Global Variable in Web.Config is indeed very good practise. Alternativly, you can even Set up Session variables at different stage.

You can set Session["Name"] variable inside your application anywhere. & they will be available to your application from anywhere. If you want something to initialize at StartUp then you can write your code in Global.asax file.

Another good thing about Session variable is they are objects. So, you can store anything inside Session variable.. starting from string, integer, Array to even DataSet or DataView.

ex.
  Session["Name"] = "Tushar";

  string strName = Session["Name"].ToString();


-tushar
0
 
LVL 9

Expert Comment

by:msdixon
ID: 12338516
both comments are only for web apps.

using the app.config / web.config is only an option if the value is constant. if it will change, you should use a separate class with a public property. this way you can change the value throughout the application. if you want to save the changes (for instance have a list of recently opened documents), you can save it to a database, or file on the local system.
0
 
LVL 3

Expert Comment

by:servicegroup
ID: 12338562
separate class with a public property... store to a database.... file on the local system....

Please read the question first

msdixon, that is the worst answer yet I heard on EE.
0
 
LVL 4

Author Comment

by:duerra
ID: 12339180
This is not a web application.  

I do not want to create a new instance of the class in every form, or read the data from a database or file on every form instance.  I would like to have an instance of a class that can be used on any form that the application opens.  

For example, if I have a class called "AppSettings", I would like to have one instance of this class available to all the different forms in the project for them to read from.

Thanks for the comments so far.  Hopefully this clarified things up a bit better.
0
 
LVL 3

Accepted Solution

by:
servicegroup earned 250 total points
ID: 12339235
You could make a class called AppSettings, and declear all the variables in that class static.

public class AppSettings
{
public static string adminEmail = "test@testing.com";
public static string location = "Earth";

//etc
}

In other classes, you can just call AppSettings.location (and this will be Earth).

-Jim
0
 
LVL 4

Author Comment

by:duerra
ID: 12339301
I appreciate that.  However, that's not quite the ticket, either.  I will need to be able to modify certain things in this class, so all the properties in it will not be able to be static.  They also need to be set dynamically, so yeah....
0
 
LVL 3

Expert Comment

by:servicegroup
ID: 12339371
OK... the variable doesn't have to be static, for instance:

public class AppSettings
{
public static string adminEmail = "test@testing.com";
public string location = "Earth";  //NOT STATIC

//etc
}

somewhere else in your other class

AppSettings.location = "Mars";

after this line is executed, AppSettings.location will be "Mars" to every other class.

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 9

Expert Comment

by:msdixon
ID: 12339396
servicegroup,

your comments have a lot of weight considering the high number of points you have.


all,

and i was not saying to read from the file / database every time the value was requested. i was saying when the app starts, read it from the external source, and when the app is closing, save the data. this is a much better way of doing things IF THE VALUE MIGHT CHANGE, otherwise it's better to use the app.config file since you won't have to re-compile in order to change the data. i'm sorry i wasn't clear enough for you two intelectual giants. the question wasn't very clear either.

i'll be quiet now so you ladies can work it out. this should be fun.
0
 
LVL 4

Author Comment

by:duerra
ID: 12339534
servicegroup, that is what I was looking for.

Now.... for future reference, what do I need to know regarding any security issues or stability issues with this sort of thing?
0
 
LVL 3

Expert Comment

by:servicegroup
ID: 12339548
I don't think you can change the app.config settings within a program.
0
 
LVL 4

Author Comment

by:duerra
ID: 12339604
eh?

Also, it looks like the variables do have to be declared static to be able to reference them statically.  It makes sense, but yeah, the code you listed above isn't quite correct for the "location" string.
0
 
LVL 3

Expert Comment

by:servicegroup
ID: 12339680
If that's what you are looking for, the best way is to have methods that get or change them for stability

public class AppSettings
{
private static string adminEmail = "test@testing.com";
private static string location = "Earth";  //NOT STATIC

public static string getadminEmail() //only get for email
{
return adminEmail;
}

public static string getlocation() //get for location
{
return location;
}

public static void setlocation(string newlocation) //set for location
{
location = newlocation;
}
//etc
}

0
 
LVL 4

Author Comment

by:duerra
ID: 12339784
Yep, already there, thanks :)

I was just wondering what you were talking about with the "app.config" stuff....
0
 
LVL 3

Expert Comment

by:servicegroup
ID: 12339914
App.config is the win app version of web.config, How to use it as follow, but the main issue with app.config is that you CANNOT change the values within the application.

Here's how to add it:
Right click on your C# solution(Not the very top one) -> add New Item -> The last item is Application configuration -> that will give you the App.config file

From there, you can add configuration settings, such as

<appSettings>
     <add key="adminEmail" value="admin@admin.com"/>
</appSettings>

and

In the csharp side

using System.Configuration; //place this line before the namespace declearation

string admin = ConfigurationSettings.AppSettings["adminEmail"]; //after this, the variable admin will have the value "admin@admin.com"

0
 
LVL 4

Author Comment

by:duerra
ID: 12340187
Ahh... I see.  The "web.config" is greek to me, since I don't use C#/ASP/etc... for my web development, but that's good to know.

Thanks.  Answer accepted.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now