清除一个变量并在整个项目中使用 [英] Declearing one Variable and using in whole project
问题描述
亲爱的朋友们
我正在使用Windows应用程序c#
i希望在主窗体中声明一个变量并使用该变量我所有的起源
我可以这样做吗?
如果是,我怎样才能实现
一种方法可能是向表单添加属性并将其声明为内部静态。如果只允许主窗体更改值,则将set部分设为私有(或者提供一个get only并声明一个单独的变量)。
例如internal static int TheProperty {get;私人集; }或
private static int myVariable;
internal static int TheProperty {get {return myVariable; }
另一种方法当然是将值传递给需要它的表单,无论是在构造函数中还是通过设置在您显示表单之前的属性。
如果所有表单都需要它,您可以使用基本类表单,其他表单来自。
当然,您选择的方法取决于您的设计。另一个要问自己的问题是,主要是否构成了保存变量的正确位置,或者它是否属于另一个类 - 例如例如,配置类只有一个实例...
问候,
Ian。
Dear Friends
I am Using windows application c#
i Want to declare one variable in main form and use that variable i all froms
can i do this?
if Yes how can i achieve that
One way might be to add a property to the form and declare it internal static. If the main form only is to be allowed to change the value then make the set part private (or provide a get only and declare a separate variable).
E.g.internal static int TheProperty { get; private set; }or
private static int myVariable; internal static int TheProperty { get { return myVariable; } }
Another way would be of course to pass the value to the forms that require it, either in the constructor or by setting a property before you show the form.
If all forms require it, you could have a base-class form which your other forms are derived from.
Of course, the method you choose depends on your design. Another question to ask yourself is whether the main form the correct place to hold the variable, or whether it belongs to another class - e.g. to a configuration class, for example, which would have only a single-instance...
Regards,
Ian.
这篇关于清除一个变量并在整个项目中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!