•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 422,995 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,927 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Views: 38389 | Replies: 8
![]() |
•
•
Join Date: May 2006
Posts: 13
Reputation:
Rep Power: 3
Solved Threads: 0
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.
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.
•
•
Join Date: Jun 2006
Posts: 18
Reputation:
Rep Power: 3
Solved Threads: 0
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.
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.
•
•
Join Date: May 2006
Posts: 13
Reputation:
Rep Power: 3
Solved Threads: 0
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.
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.
•
•
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,333
Reputation:
Rep Power: 11
Solved Threads: 102
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.
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
•
•
Join Date: May 2006
Posts: 13
Reputation:
Rep Power: 3
Solved Threads: 0
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.
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.
•
•
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,165
Reputation:
Rep Power: 7
Solved Threads: 59
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.
Use it anywhere like this (it's static you don't instantiate it anywhere)
retrieve it anywhere like this:
Add a new class.cs file to the project and stick something like this in it.
static class GlobalClass
{
private string m_globalVar = "";
public static string GlobalVar
{
get { return m_globalVar; }
set { m_globalVar = value; }
}
}Use it anywhere like this (it's static you don't instantiate it anywhere)
GlobalClass.GlobalVar = "Some value";
retrieve it anywhere like this:
myFormLabel.Text = GlobalClass.GlobalVar;
Last edited by hollystyles : Jul 21st, 2006 at 5:08 am.
•
•
Join Date: May 2006
Posts: 13
Reputation:
Rep Power: 3
Solved Threads: 0
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.
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 5:28 am.
•
•
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,165
Reputation:
Rep Power: 7
Solved Threads: 59
•
•
•
•
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C# Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 4 (0 members and 4 guests)
Other Threads in the C# Forum
- Previous Thread: Is it possible to execute the ASP.NET code in command prompt..?
- Next Thread: Datagrid to draw a table



Linear Mode