944,132 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 236244
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 13th, 2006
0

How to define global variable in C#

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Swapnil Palande is offline Offline
13 posts
since May 2006
Jul 13th, 2006
0

Re: How to define global variable in C#

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.
Reputation Points: 10
Solved Threads: 1
Light Poster
asmith3006 is offline Offline
26 posts
since Jun 2006
Jul 14th, 2006
0

Re: How to define global variable in C#

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Swapnil Palande is offline Offline
13 posts
since May 2006
Jul 14th, 2006
0

Re: How to define global variable in C#

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.
Reputation Points: 10
Solved Threads: 1
Light Poster
asmith3006 is offline Offline
26 posts
since Jun 2006
Jul 20th, 2006
0

Re: How to define global variable in C#

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.
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003
Jul 21st, 2006
0

Re: How to define global variable in C#

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Swapnil Palande is offline Offline
13 posts
since May 2006
Jul 21st, 2006
2

Re: How to define global variable in C#

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.
C# Syntax (Toggle Plain Text)
  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)

C# Syntax (Toggle Plain Text)
  1. GlobalClass.GlobalVar = "Some value";

retrieve it anywhere like this:

C# Syntax (Toggle Plain Text)
  1. myFormLabel.Text = GlobalClass.GlobalVar;
Last edited by hollystyles; Jul 21st, 2006 at 6:08 am.
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Jul 21st, 2006
0

Re: How to define global variable in C#

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Swapnil Palande is offline Offline
13 posts
since May 2006
Jul 21st, 2006
0

Re: How to define global variable in C#

Quote ...
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.
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Jan 12th, 2009
0

Re: How to define global variable in C#

Thanks a lot for your info! Worked For me!
Reputation Points: 10
Solved Threads: 1
Newbie Poster
harry_fyt is offline Offline
1 posts
since Jan 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C# Forum Timeline: Windows Service won't auto start
Next Thread in C# Forum Timeline: Disable close button





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC