Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello ! i have an application in WPF (C# ) that have multi users, the user can do different operations using the application but, every user has different access permission i want to create a variable in the first window that contains the name of the user , cause i need to store it in another table in the database ( every time the user do an operation i save his name and the time of this operation in the table ) , so now i don't know how to create a variable that can be used in different windows of the application ...

What I have tried:

i tried to create a class that contains a static variable , but that didn't work ....so could plz help me
Posted
Updated 4-Mar-16 14:15pm

There is no such concept as "global variable" in .NET. Not at all. And this is very good. The practice of using of such variable is bad.

There are static class members, which have a lot in common with "global variables", because they are stored "globally", per class (not per instance), even though they are the subject of access modification described in a class. They are also should be used with care; and usually you can avoid using them. One obvious problem is that they create separate additional shared resources which needs synchronization when multithreading is used.

The solution? The really robust approach is using the instance members, not static. When beginners say "different windows", they tend to forget to explain: different classes or instances? The essence of this problem is in the instances. The simplest solution is: you can create an instance property of your window classes, of a reference type, which should keep the reference to the same object (say, with user credentials, as in your case). You can create first object in, say, main window, which plays the role of a singleton. It depends; it can be anywhere. Other windows can get the reference to the same object as you create them. Anyway, you keep the points where you create any additional window under your control, so you can always set a reference.

For the related topic singleton (which also should be used with care and is best avoided), please see:
Singleton pattern — Wikipedia, the free encyclopedia[^],
C# in Depth: Implementing the Singleton Pattern[^].

See also my Tip & Tricks article which is not directly related to your issue, but deals with the inquirers' issue which has a lot in common with yours: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows[^].

—SA
 
Share this answer
 
Comments
Philippe Mori 5-Mar-16 9:02am    
Very good answer. I don't understand how one could vote 1.
Sergey Alexandrovich Kryukov 5-Mar-16 17:44pm    
Thank you, Philippe.
I do understand. Someone just gives 1 to all my answers, or maybe proportional number...
—SA
Maciej Los 5-Mar-16 9:52am    
5ed!
Sergey Alexandrovich Kryukov 5-Mar-16 17:43pm    
Thank you, Maciej.
—SA
That should work... creating a static class with a static property
 
Share this answer
 
Comments
Member 12352173 4-Mar-16 18:28pm    
maybe i didin't declare right ! i don't know this is new to me ! ....i need an example of this if it's possible ...thx
Philippe Mori 5-Mar-16 9:04am    
The answer explain it all. If you don't what we are talking about, better to read documentation.

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