Solved

Global Variables/Classes in C#

Posted on 2004-10-18
16
2,263 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

789 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