What is the best approach to have a global variable?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, friends,

I am programming a Windows Form app using VC#. I need to have some info
shared through the whole app, e.g., login user id/name. I know I can create a
classs to hold these info in its properties, but each time I will have to
instantiate this class.

So, what is the best approach to store those global variables?

Thanks a lot.
 
Hi Andrew,

You can have a class with static properties so that you don't have intialize
an object.

HTH,
Rakesh Rajan
 
Create one class and expose the variables as static
ie

class GlobalVariables
{
public static string UserID;
public static string Password;
}

the static keyword allows you to retrieve or modify the values without
having to instantiate the GlobalVariables class. So from you login form you
can call

GlobalVariables.UserID = "your value";
GlobalVariables.Password = "your value";


Hope it helps
 
As I posted previously, try the following:

Add a new class to your project -

class GlobalVariables
{
public static string UserID;
public static string Password;
}

the static keyword allows you to retrieve or modify the values without
having to instantiate the GlobalVariables class. So from you login form you
can call

GlobalVariables.UserID = "your value";
GlobalVariables.Password = "your value";


Try it an let me know


C# Learner said:
Opa said:
[...] but each time I will have to instantiate this class.

The Singleton pattern might be best here:
http://www.yoda.arachsys.com/csharp/singleton.html

No you will not. Try it.

Don't understand what you mean...
 
Opa said:
As I posted previously, try the following:
[snip]

That's one way of doing it; implementing Singleton is another.

Note that I didn't say that implementing Singleton *is* the best way --
just that it *might* be. It depends on the circumstances. As it is now,
we don't have enough detail from the OP to determine this.
 
You can do this without a class which every one seems to "demand." I am
not advocating using global variables, but it is rather easy. It leads
to poor coding practices. However, there are times to be pragmatic.

Add a GLOBAL.ASA file to your project.

In the section Session_Start

protected void Session_Start(Object sender, EventArgs e)
{
Session["ConnectionDB"] = "data source=MYSQLServer;initial
catalog=MyDataBase;integrated security=false;User
iD=whatever;Pwd=whatever;persist security info=True;packet size=4096";
}

This creates a session variable that you can access later on any form

private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
connectionDB.ConnectionString =
(string)Session["connectionDB"];
...ect...

I use this technique often when I have a SQL server at a customer site
that has different name and connection string requirement then my home
development environment. This way I put 2 connection strings in the
Global ASA file and then comment one out when at home and comment out
the other when I am at the office.

William
 
Back
Top