| | |
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:
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: 26
Reputation:
Solved Threads: 1
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:
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.
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:
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.
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.
C# Syntax (Toggle Plain Text)
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)
C# Syntax (Toggle Plain Text)
GlobalClass.GlobalVar = "Some value";
retrieve it anywhere like this:
C# Syntax (Toggle Plain Text)
myFormLabel.Text = GlobalClass.GlobalVar;
Last edited by hollystyles; Jul 21st, 2006 at 6:08 am.
•
•
Join Date: May 2006
Posts: 13
Reputation:
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 6:28 am.
•
•
•
•
Hence in your code, "m_globalVar" should also be define as static. Otherwise it will give error.
Encryption thpt no idea not something I've really ever done i'm afraid.
![]() |
Similar Threads
Other Threads in the C# Forum
- Previous Thread: How to CREATE a mySQL database in C# code
- Next Thread: Hi, I dont get what Iv done wrong
| Thread Tools | Search this Thread |
.net access algorithm angle array asp.net barchart bitmap box broadcast c# capturing check checkbox client combobox control conversion csharp custom database datagrid datagridview dataset datetime dbconnection degrees delegate design development disappear draganddrop drawing encryption enum eventhandlers excel file firefox form format forms function gdi+ image index input install java label leak libraries list listbox loop mandelbrot math monodevelop mouseclick msword mysql operator path pause photoshop picturebox pixelinversion post programming radians regex remoting resourcefile richtextbox round server sleep socket sql statistics stream string table tcpclientchannel text textbox thread time timer update usercontrol validation virtualization visualbasic visualstudio webbrowser windows winforms wpf xml






