473,387 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

What is the best approach to have a global variable?

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.
Nov 16 '05 #1
8 20399
Hi Andrew,

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

HTH,
Rakesh Rajan

"Andrew" wrote:
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.

Nov 16 '05 #2
Opa
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


"Andrew" wrote:
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.

Nov 16 '05 #3
Andrew <An****@discussions.microsoft.com> wrote:
[...] 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
Nov 16 '05 #4
Opa
No you will not. Try it.

"C# Learner" wrote:
Andrew <An****@discussions.microsoft.com> wrote:
[...] 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

Nov 16 '05 #5
Opa <Op*@discussions.microsoft.com> wrote:
Andrew <An****@discussions.microsoft.com> wrote:
[...] 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...
Nov 16 '05 #6
Opa
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" wrote:
Opa <Op*@discussions.microsoft.com> wrote:
Andrew <An****@discussions.microsoft.com> wrote:

[...] 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...

Nov 16 '05 #7
Opa <Op*@discussions.microsoft.com> wrote:
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.
Nov 16 '05 #8
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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: David WOO | last post by:
Hi, I am a newbie on C++, I need to define some global variables which should be accessible to most classes. In the mean time, I don't won't the global variables be modified freely at most of...
1
by: seberino | last post by:
Python lets me access module level variables from *anywhere*. All I have to do is add module name in front. e.g. mymodule.myvariable Is this considered a 'global'? Or, does a 'global...
24
by: LP | last post by:
After a code review one coworker insisted that global are very dangerous. He didn't really give any solid reasons other than, "performance penalties", "hard to maintain", and "dangerous". I think...
9
by: Shapper | last post by:
Hello, I am declaring a variable in my aspx.vb code as follows: Public Class catalogue Public productid As String Private Sub Page_Load ... I have an image button where I call the...
2
by: Miguel Dias Moura | last post by:
Hello, I need to declare a global variable in my Aspx.Vb code to be available on my Aspx page. Where should i declare my variable inside my asp.vb code? Thanks, Miguel
6
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why doesn't the global variable "divId" always refer to the element with id="divId"?...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
1
by: jainhemant007 | last post by:
What is Different between global variable and public variable ?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.