阅读几行小 C# 代码以及它们的含义
对于那些非常了解这份工作的人来说,我的问题很简单。 这段代码是如何逐行读取的,尤其是 3 个带注释的代码?
private formMain parent = null; //explanation 1
public formChild(formMain parent) //explanation 2
{
InitializeComponent();
this.parent = parent; //explanation 3
}
我的想法: - 解释 1:创建具有 null 值的 formMain 的引用父级。那个“null”我真的不知道它是什么意思,我想要解释。也许是一个空值,因为它必须在使用前初始化? - 解释2:在子窗体的构造函数中传递父窗体的对象? - 解释3:this.parent指的是私有成员(位于第一行),=parent指的是构造函数参数列表中的成员?
在父表单中,在初始化子表单时,在 (formMain Parent) 的构造函数中传递 (this) 时,有一些小代码,我认为它正在传递当前表单。
我在互联网上找到了这段代码,它运行良好,用于从子表单访问父表单的属性。 任何帮助将不胜感激,提前致谢。
PS 请使用此类模板发表评论: - 解释1: - 解释2: - 解释 3:
my question is very simple for those who know the job very well.
How is this code read line by line, especially the 3 commented ones?
private formMain parent = null; //explanation 1
public formChild(formMain parent) //explanation 2
{
InitializeComponent();
this.parent = parent; //explanation 3
}
My thougths:
- explanation 1: creating reference parent of formMain that has null value. That 'null' I really don't know what does it mean, I want explanation for that. Maybe a null value because it must be initializated before use?
- explanation 2: passing object of parent form in the constructor of child form?
- explanation 3: this.parent refers to the private member (which is in the first line) and = parent refers to the member in the arguments list of the constructor?
And in the parent form there is small code when initializating the child form passing (this) in the constructor for (formMain parent) which I think it's passing the current form.
This code I found on internet, it's working perfectly and it is used for accessing properties of parent form from child form.
Any help will be appreciated, thanks in advance.
P.S. Please comment using this kind of template:
- explanation 1:
- explanation 2:
- explanation 3:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
parent
的变量来保存某些内容。它将保存的“东西”是一个formMain
。通过将其设置为null
,您表示您还没有特定的formMain
,但您知道您最终应该拥有一个。formChild
的实例。该对象 (formChild
) 依赖于formMain
。尽管依赖项与解释 1 中的依赖项具有相同的名称,但它们并不相同(尚未相同)。formMain
称为parent
(来自解释 2)被分配给内部存储的parent
(来自解释 1)。现在刚刚创建的对象中的其余方法可以引用外部依赖项。所以你使用这段代码的方式是这样的
parent
to hold something. The "something" that it will hold is aformMain
. By setting it tonull
you are saying you don't have the specificformMain
yet, but you know you should have one eventually.formChild
. This object (formChild
) has a dependency on aformMain
. Even though the dependency has the same name as the one from explanation 1 they are not the same (not yet).formMain
calledparent
(from explanation 2) is getting assigned to the internally storedparent
from explanation 1. Now the rest of the methods in the object which has just been created can reference the external dependency.So the way you would use this code is something like this
当我还是个新手的时候,我曾经这样评论过,但过了一段时间,你的评论水平就变得疯狂了,而是解释了函数/部分的目的是什么。
IE 您不需要解释您设置 Partent = null,因为代码已经说明了这一点,您解释为什么将其设置为 null。
I used to comment like this when i was very new, but after a while you insane this with level of commenting that, and instead explain what the purpos of a function/part is.
IE You dont need to explain that you setting Partent = null, because the code already says that, you explain why your setting it to null.
解释 #2 是正确的(并且解释 #3 也非常接近)。
子窗体通过将其分配给私有成员变量来保留对其父窗体的引用。您想要保留引用的主要原因是为了与父级进行通信,或者在父级表单中访问公共属性或函数。
Explanation #2 is correct (and #3 is pretty close too).
The child form is keeping the reference to it's parent by assigning it to a private member variable. The main reason you would want to keep the reference is for communicating back to the parent, or accessing public properties or functions back in the parent form.
1:初始化变量或对象永远不是一个坏主意。除非您知道希望变量具有
null
的初始值通常是更好的选择(大多数情况下的替代方案是使用一些您一无所知的随机值)。2:这一行声明了一个函数/方法,它采用一个 formMain 类型的参数。在本例中,它称为
parent
。3:这一行将私有成员
parent
设置为等于传递给函数的参数parent
。它们的关键字this
表示类中包含的元素。更容易理解的重写可能如下所示:
1: It is never a bad idea to initialize variables or objects. Unless you know that initial values you want variables to have
null
is usually a better choice (the alternative in most cases is to have some random value you know nothing of).2: This line declares a function/method that takes one parameter of type formMain. In this case it is called
parent
.3: This line is setting the private member
parent
equal to the parameterparent
passed to the function. They keywordthis
represents elements contained within the class.A rewrite more easier to understand might look like this:
子窗体的构造函数正在设置对其父窗体的引用。
关于“这个”的使用。如果构造函数包含赋值就会出现问题。
为了引用同名的私有文件(在构造函数之外),需要在前面添加“this”:
其他注释。这种风格似乎是从 Java 借来的,对于 C# 来说并不典型。
The constructor for a child form is setting the reference to its parent.
Regarding the use of "this". If the constructor contained the assignment there would be a problem.
In order to reference the private filed by the same name (outside of the constructor), 'this' is pre-pended:
Other comments. The style seems to be borrowed from Java and would not be typical for C#.