Solved

Global Variables/Classes in C#

Posted on 2004-10-18
16
2,258 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What .NET URL re-routing tool did I use? 2 54
Error in JQuery 5 51
Video Player 11 23
tableview is not updating 1 7
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

786 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