How to define global variable in C#

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2006
Posts: 13
Reputation: Swapnil Palande is an unknown quantity at this point 
Solved Threads: 0
Swapnil Palande Swapnil Palande is offline Offline
Newbie Poster

How to define global variable in C#

 
0
  #1
Jul 13th, 2006
Hi all,

Please help me solving following query:

How to add global variable in C# window base application. This variable must be accessible to all forms.

What I exactly want is:

I have a variable "UserName" and "UserType". When user click "Submit" button on Login form, user name get stored in variable "UserName" and user type get stored in variable "UserType". After click "Submit" button Login form get closed and "Project" form get open.

On "Project" form there are label "User Name" and "User Type". When "Project" form get open, the value of "UserName" and "UserType" variable should be get displayed in front of these labels.

I hope you understand my question.

Thanks and regards,

Swapnil.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 26
Reputation: asmith3006 is an unknown quantity at this point 
Solved Threads: 1
asmith3006 asmith3006 is offline Offline
Light Poster

Re: How to define global variable in C#

 
0
  #2
Jul 13th, 2006
I don't know how to solve your problem, but I'm guessing there's no such thing as a global variable since that would destroy encapsulation.

As a guess, couldn't you use static members of a class (called sessionSetttings or similar) and then have a property access function (is that what they're called?) to modify it.

Is this an accepted way of doing things in C#? If not, I would be very interested in the actual method too.

Thanks.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 13
Reputation: Swapnil Palande is an unknown quantity at this point 
Solved Threads: 0
Swapnil Palande Swapnil Palande is offline Offline
Newbie Poster

Re: How to define global variable in C#

 
0
  #3
Jul 14th, 2006
In this problem I am speaking about Window based application and not Web based application. Here global variable means the variable which is accessible to all forms or classes.

I tried static member, I define static variable "UserName" in seperate class (GlobalClass). When user inserts user name and password in LoginForm and clicks "Submit" button the user name gets stored in static variable ("UserName") and LoginForm closes, here is the problem, when LoginForm get closed the value of static variable "UserName" is also wiped out. I want "UserName" on "ProjectForm" which get open after LoginForm closes.

I hope you got it.

Thanks and regards,

Swapnil.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 26
Reputation: asmith3006 is an unknown quantity at this point 
Solved Threads: 1
asmith3006 asmith3006 is offline Offline
Light Poster

Re: How to define global variable in C#

 
0
  #4
Jul 14th, 2006
I get what you mean.

Make sure the constuctor is a static constructor too, this way it will only happen the first time you construct the class.
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 2,414
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Solved Threads: 123
Team Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: How to define global variable in C#

 
0
  #5
Jul 20th, 2006
You're looking at an issue of object persistence, more than likely.

When you close the form, the object that the form represents, including all members included in it, are disposed. More than likely, you'll want to serialize the information in a temp file, or something like that.

http://msdn.microsoft.com/library/de...bjserializ.asp

That'd be my guess. I'd really like to know if there's something better.
Alex Cavnar, aka alc6379
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 13
Reputation: Swapnil Palande is an unknown quantity at this point 
Solved Threads: 0
Swapnil Palande Swapnil Palande is offline Offline
Newbie Poster

Re: How to define global variable in C#

 
0
  #6
Jul 21st, 2006
Thanks Alex,

I tried "Serialzation" and it is working fine. But problem is that whenever you want to retrive the value of variable you have to "deserialize" the object.

In my case, there is variable "UserName". When user insert UserName adn password and clicks "Submit" button, user name gets stored in "UserName" variable and Login form closes. Now what I want is, "UserName" should get displayed on each and every form which I will open. And there are around 10 to 12 forms.

I hope you got it.

Thanks and regards,

Swapnil.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: How to define global variable in C#

 
0
  #7
Jul 21st, 2006
Easy. this should work, you just need a static class:

Add a new class.cs file to the project and stick something like this in it.
  1. static class GlobalClass
  2. {
  3. private string m_globalVar = "";
  4.  
  5. public static string GlobalVar
  6. {
  7. get { return m_globalVar; }
  8. set { m_globalVar = value; }
  9. }
  10.  
  11.  
  12. }

Use it anywhere like this (it's static you don't instantiate it anywhere)

  1. GlobalClass.GlobalVar = "Some value";

retrieve it anywhere like this:

  1. myFormLabel.Text = GlobalClass.GlobalVar;
Last edited by hollystyles; Jul 21st, 2006 at 6:08 am.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 13
Reputation: Swapnil Palande is an unknown quantity at this point 
Solved Threads: 0
Swapnil Palande Swapnil Palande is offline Offline
Newbie Poster

Re: How to define global variable in C#

 
0
  #8
Jul 21st, 2006
Thanks hollystyles,

This is what I am looking for. Thanks very much.

Static class contains only static Members.

Hence in your code, "m_globalVar" should also be define as static. Otherwise it will give error.

static class GlobalClass
{
private static string m_globalVar = "";

public static string GlobalVar
{
get { return m_globalVar; }
set { m_globalVar = value; }
}


}

If possible can you tell me how to encrypt the password.

I want both encrypt and decrypt methos.

Thanks and regards,

Swapnil.
Last edited by Swapnil Palande; Jul 21st, 2006 at 6:28 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: How to define global variable in C#

 
0
  #9
Jul 21st, 2006
Hence in your code, "m_globalVar" should also be define as static. Otherwise it will give error.
Absolutely right , typo on my part tut tut.

Encryption thpt no idea not something I've really ever done i'm afraid.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 1
Reputation: harry_fyt is an unknown quantity at this point 
Solved Threads: 1
harry_fyt harry_fyt is offline Offline
Newbie Poster

Re: How to define global variable in C#

 
0
  #10
Jan 12th, 2009
Thanks a lot for your info! Worked For me!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC