Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Am working in project which use c# and WPF in visual studio 2010....
I want to use global variable which is of Boolean type and which is defined in main window and i have other page in which am using that control...
Please help me...
Thanks in advance
Posted
Updated 6-Jan-12 18:04pm
v2

You shouldn't use capitals in your question. It is deemed as shouting on public forums and is extremely rude. You should not use Global variables in c# either as that is considered bad practice, you should create a public property that returns a boolean in your main form and pass that as a parameter in your constructor for form2, so that form2 can use and change the property appropriately.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Jan-12 4:18am    
You are right, my 5, but I'm afraid OP would not get this idea without some clarification; the problem is, to share this variable, you need to pass a reference to it, so, if the variable has to be a primitive one, it should be encapsulated in a reference type (class) and the instance of this class passed. (OK, I did not explain it in my answer, but I can refer to this comment).

There is some more to it -- please see my answer.
--SA
ranjith m amin 7-Jan-12 0:02am    
Sorry Sir,
I am very new to s/w development that's why i explained question in my way.. Please, I need some more clarification ... Can anybody give me the example..
I understood this much , first i have to create a class in mainwindow.xaml page and instance of that class is to be created and passed to 1.cs page... From there i have to use that passed value... Is it right...
Thanks for Reply
There is no a concept of global variable in .NET (sight… finally; thanks goodness!).

The closest analog is a static field of some class, both having access modifier internal or public. This is not recommended.

Yes, you can have a reference to a stack variable declared in entry point passed where it is needed, as Wayne suggested (please also see my clarification in my comment to his answer); another solution is the singleton design pattern, see:
http://en.wikipedia.org/wiki/Singleton_pattern[^],
http://en.wikipedia.org/wiki/Design_pattern_(computer_science)[^],
http://csharpindepth.com/Articles/General/Singleton.aspx[^] (good implementation samples here).

—SA
 
Share this answer
 
v2
add new class & name it global variable
delcare few variables on that class
now in your project call that class
assign value to variable , now u can access this variable anywhere
in your project
 
Share this answer
 
You can do it by creating a static class suppose it is Global.cs

now define few variables in it i.e
C#
public static string user="";

now if your application have a login form then on btnclick event pass the userid to this Global variable i.e
C#
Global.user=txtuser.text;

then use this Global variable in your application as globally.

hope u got it or i am correct
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 6-Jan-12 4:30am    

This is a very bad practice! Avoid doing it by all means, everyone!


--SA
Member 11072312 29-Sep-14 8:09am    
To me, this seems like good practice, because it's works and is simple. But I'm not that good at C#. Why is it bad practice? Give a simple explanation, please.
Sergey Alexandrovich Kryukov 29-Sep-14 10:10am    
Sometimes "works" is worse than not working at all. If it is not working at all, everyone has an incentive to put it to work. If "working", it makes an illusion of being fine, but in fact keeps a rotten piece in place.

It's not obvious that some object is global. If you eventually add a new thread, you have to worry about all global objects, because they become unusable without locking. If you decide to modify this global object in some new way, you may not take into account the effect of it on other pieces of code. In other words, your "working" code is harder to modify. It's less supportable. And, importantly, global objects are not really needed; and singletons are needed very rarely.

—SA
devbtl 6-Jan-12 4:35am    
may be it is a bad practise but some how i am using it in small window application to pass data from one form to another. Thanks
Sergey Alexandrovich Kryukov 6-Jan-12 4:45am    
Some things which work are worse than if they did not. This is one of such things. Why using bad practices if there are good ones?
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900