如何在c#中创建和使用全局变量 [英] How to create and use a global variable in c#

查看:81
本文介绍了如何在c#中创建和使用全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!我有一个具有多用户的WPF(C#)应用程序,用户可以使用该应用程序执行不同的操作但是,每个用户都有不同的访问权限我想在第一个窗口中创建一个包含用户名称的变量,原因我需要将它存储在数据库中的另一个表中(每次用户执行操作时我都会在表中保存他的名字和操作时间),所以现在我不知道如何创建一个可以使用的变量在应用程序的不同窗口...



我尝试过:



i试图创建一个包含静态变量的类,但是没有用....所以可以帮助我

解决方案

没有这样的概念作为.NET中的全局变量。一点也不。这非常好。使用这种变量的做法很糟糕。



有静态类成员,它们与全局变量有很多共同点,因为它们存储在全局变量中,每个类(不是每个实例),即使它们是类中描述的访问修改的主题。它们也应该小心使用;通常你可以避免使用它们。一个明显的问题是,他们创建了单独的额外共享资源,在使用多线程时需要同步。



解决方案?真正强大的方法是使用实例成员,而不是静态。当初学者说不同的窗口时,他们往往忘记解释:不同的类或实例?这个问题的实质是在实例中。最简单的解决方案是:您可以创建一个引用类型的窗口类的实例属性,该属性应保留对同一对象的引用(例如,使用用户凭据,如您的情况)。您可以在主窗口中创建第一个对象,该窗口扮演 singleton 的角色。这取决于;它可以在任何地方。其他窗口可以在创建它们时获得对同一对象的引用。无论如何,你保留在你控制下创建任何额外窗口的点,这样你就可以随时设置参考。



相关主题 singleton (也应谨慎使用,最好避免使用),请参阅:

Singleton模式 - 维基百科,免费的百科全书 [ ^ ],

C#深度:实现单例模式 [ ^ ]。



另请参阅我的提示&诀窍与你的问题没有直接关系的文章,但处理与你有很多共同点的查询者问题:一次回答的许多问题 - Windows窗体或WPF Windows之间的协作 [ ^ ]。



-SA


这应该有效...创建一个带静态属性的静态类


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

解决方案

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


That should work... creating a static class with a static property


这篇关于如何在c#中创建和使用全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆