C# winform:从其他表单访问公共属性以及静态和公共属性之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14663710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# winform: Accessing public properties from other forms & difference between static and public properties
提问by jomsk1e
I am trying to understand whats the difference between a static and public properties. But when I tried to access my public property 'Test' in other form it says 'null'.
我试图了解静态和公共属性之间的区别是什么。但是当我尝试以其他形式访问我的公共财产“Test”时,它显示“null”。
Heres Form1:
继承人Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string _test;
public string Test
{
get { return _test; }
set { _test = value; }
}
private void Form1_Load(object sender, EventArgs e)
{
_test = "This is a test";
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
}
Here's Form2:
这是Form2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
label1.Text = frm1.Test;
}
}
To check the value of 'Test' in Form1, I put a breakpoint to this line:
为了检查 Form1 中 'Test' 的值,我在这一行放置了一个断点:
label1.Text = frm1.Test;
But the value is 'null'.
但该值为“空”。
Please help me how can I access public properties to other forms.
请帮助我如何访问其他表单的公共属性。
And BTW I tried to make this public property be a 'public static'. I can access this using this:
顺便说一句,我试图使这个公共财产成为“公共静态”。我可以使用这个访问它:
Form1.Test
But I noticed that I can change 'Test' value from Form2 which I don't want to happen. That's why I am trying to use public property but with no luck. Can somebody clarify me these things. Thanks for all your help guys!
但我注意到我可以从 Form2 更改“测试”值,这是我不想发生的。这就是为什么我试图使用公共财产但没有运气。有人可以向我澄清这些事情。感谢您的帮助!
EDIT: (For follow up question)
Sir John Koerner's answer is the best answer for my question. But I have a follow up question, I tried to make these 'test' properties to be a 'static', and I noticed that even if I make this property a static or public property, it still can be edit in Form2. To make myself clear here's a sample:
John Koerner 爵士的回答是我问题的最佳答案。但是我有一个后续问题,我尝试将这些“测试”属性设为“静态”,并且我注意到即使我将此属性设为静态或公共属性,它仍然可以在 Form2 中进行编辑。为了让自己清楚,这里有一个示例:
public partial class Form2 : Form
{
private Form1 f1;
public Form2(Form1 ParentForm)
{
InitializeComponent();
f1 = ParentForm;
}
private void Form2_Load(object sender, EventArgs e)
{
label1.Text = f1.Test;
}
private void button1_Click(object sender, EventArgs e)
{
f1.Test = "This test has been changed!";
this.Close();
}
}
After Form2 closed, I tried to break again in Form1_Load to check value of 'Test', and it was changed! How can I make a public property in Form1 to readOnly in Form2 and cannot be editted? Please clarify to me. Thanks a lot guys!
Form2 关闭后,我尝试在 Form1_Load 中再次中断以检查 'Test' 的值,并且它被更改了!如何使 Form1 中的公共属性在 Form2 中只读且无法编辑?请向我澄清。非常感谢伙计们!
采纳答案by John Koerner
Your property is an instance variable, so the value can be different across different instances of Form1
.
您的属性是一个实例变量,因此 的不同实例的值可能不同Form1
。
If you are trying to access instance variables from a parent form, the easiest way to do that is to pass Form1 in to the constructor of Form2.
如果您尝试从父窗体访问实例变量,最简单的方法是将 Form1 传递给 Form2 的构造函数。
public partial class Form2 : Form
{
private Form1 f1;
public Form2(Form1 ParentForm)
{
InitializeComponent();
f1 = ParentForm;
}
private void Form2_Load(object sender, EventArgs e)
{
label1.Text = f1.Test;
}
}
Then when you create a new Form2 from Form1, you can do this:
然后当你从 Form1 创建一个新的 Form2 时,你可以这样做:
Form2 frm2 = new Form2(this);
If you want your property to be read only, you can simply not specify a setter:
如果你希望你的属性是只读的,你可以简单地不指定一个 setter:
public string Test
{
get { return _test; }
}
回答by Hamlet Hakobyan
The frm1
not your main form object. It is newly created object where property Test
initializes when it loads (in Form1_Load
event handler).
该frm1
不是你的主要形式对象。它是新创建的对象,其中属性Test
在加载时初始化(在Form1_Load
事件处理程序中)。
回答by CodeCaster
The first instance of Form1
shows an instance of Form2
, and then Form2
creates anotherinstance of Form1
. This could work, but you set _test
in the Form.Load
event, which:
的第一个实例Form1
显示 的实例Form2
,然后Form2
创建 的另一个实例Form1
。这可以工作,但你_test
在Form.Load
事件中设置,其中:
Occurs before a form is displayed for the first time.
在第一次显示表单之前发生。
You do not show the instance of Form1
you're trying to read Test
from, so its Load
event will not occur and Test
remains null
.
您没有显示Form1
您尝试读取的实例Test
,因此它的Load
事件不会发生并Test
保持不变null
。
You could add a constructor overload or property to pass the Form1 reference as @JohnKoerner mentions, but I would prefer to only pass the required variable, perhaps even encapsulated in an event, to reduce coupling. Form2
usually doesn't need to know all about Form1
.
你可以添加一个构造函数重载或属性来传递 @JohnKoerner 提到的 Form1 引用,但我更愿意只传递所需的变量,甚至可能封装在一个事件中,以减少耦合。Form2
通常不需要知道所有关于Form1
.
回答by zandi
public
民众
The type or member can be accessed by any other code in the same assembly or another assembly that references it.
同一程序集或引用它的另一个程序集中的任何其他代码都可以访问该类型或成员。
static
静止的
The static modifier on a class means that the class cannot be instantiated, and that all of its members are static. A static member has one version regardless of how many instances of its enclosing type are created.
类上的 static 修饰符意味着该类不能被实例化,并且它的所有成员都是静态的。一个静态成员有一个版本,不管它的封闭类型有多少实例被创建。
A static class is basically the same as a non-static class, but there is one difference: a static class cannot be externally instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself.
静态类与非静态类基本相同,但有一个区别:静态类不能从外部实例化。换句话说,您不能使用 new 关键字来创建类类型的变量。由于没有实例变量,您可以使用类名本身访问静态类的成员。
However, there is a such thing as a static constructor. Any class can have one of these, including static classes. They cannot be called directly & cannot have parameters (other than any type parameters on the class itself). A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced. Looks like this:
但是,有一个静态构造函数之类的东西。任何类都可以拥有其中之一,包括静态类。它们不能被直接调用并且不能有参数(除了类本身的任何类型参数)。在创建第一个实例或引用任何静态成员之前,会自动调用静态构造函数来初始化类。看起来像这样:
static class Foo() { static Foo() { Bar = "fubar"; }
静态类 Foo() { 静态 Foo() { Bar = "fubar"; }
public static string Bar { get; set; }
}
}
Static classes are often used as services, you can use them like so:
静态类通常用作服务,您可以像这样使用它们:
MyStaticClass.ServiceMethod(...);
MyStaticClass.ServiceMethod(...);
回答by M.GH
Use of this method 'static'
使用此方法“静态”
At first Control label property Modifiers=Public
首先控制标签属性 Modifiers=Public
in Program code below
在下面的程序代码中
public static Form1 frm1 = new Form1();
public static Form2 frm2 = new Form2();
in Form1 code below
在下面的 Form1 代码中
Program.frm2.show();
in Form2 code below
在下面的 Form2 代码中
label1.Text=Program.frm1.text;