阅读几行小 C# 代码以及它们的含义

发布于 2024-11-03 06:55:16 字数 626 浏览 5 评论 0原文

对于那些非常了解这份工作的人来说,我的问题很简单。 这段代码是如何逐行读取的,尤其是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

别挽留 2024-11-10 06:55:16
  • 解释 1:您正在创建一个名为 parent 的变量来保存某些内容。它将保存的“东西”是一个formMain。通过将其设置为 null,您表示您还没有特定的 formMain,但您知道您最终应该拥有一个。
  • 解释 2:您正在创建一个方法来创建 formChild 的实例。该对象 (formChild) 依赖于 formMain。尽管依赖项与解释 1 中的依赖项具有相同的名称,但它们并不相同(尚未相同)。
  • 解释 3:外部传入的 formMain 称为 parent(来自解释 2)被分配给内部存储的 parent(来自解释 1)。现在刚刚创建的对象中的其余方法可以引用外部依赖项。

所以你使用这段代码的方式是这样的

//from a formMain type object. 
formChild childForm = new formChild(this);

//from the formChild object
this.parent.Title = "Title of parent being changed from child"
  • explanation 1: You are creating a variable called parent to hold something. The "something" that it will hold is a formMain. By setting it to null you are saying you don't have the specific formMain yet, but you know you should have one eventually.
  • explanation 2: You are creating a method which creates an instance of a formChild. This object (formChild) has a dependency on a formMain. Even though the dependency has the same name as the one from explanation 1 they are not the same (not yet).
  • explanation 3: The externally passed in formMain called parent (from explanation 2) is getting assigned to the internally stored parent 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

//from a formMain type object. 
formChild childForm = new formChild(this);

//from the formChild object
this.parent.Title = "Title of parent being changed from child"
爱人如己 2024-11-10 06:55:16

当我还是个新手的时候,我曾经这样评论过,但过了一段时间,你的评论水平就变得疯狂了,而是解释了函数/部分的目的是什么。

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.

你又不是我 2024-11-10 06:55:16

解释 #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.

挽梦忆笙歌 2024-11-10 06:55:16

1:初始化变量或对象永远不是一个坏主意。除非您知道希望变量具有 null 的初始值通常是更好的选择(大多数情况下的替代方案是使用一些您一无所知的随机值)。
2:这一行声明了一个函数/方法,它采用一个 formMain 类型的参数。在本例中,它称为parent
3:这一行将私有成员 parent 设置为等于传递给函数的参数 parent。它们的关键字 this 表示类中包含的元素。

更容易理解的重写可能如下所示:

private formMain parent = null; //explanation 1
public formChild(formMain element) //explanation 2
{
    InitializeComponent();
    this.parent = element; //explanation 3
}

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 parameter parent passed to the function. They keyword this represents elements contained within the class.

A rewrite more easier to understand might look like this:

private formMain parent = null; //explanation 1
public formChild(formMain element) //explanation 2
{
    InitializeComponent();
    this.parent = element; //explanation 3
}
┼── 2024-11-10 06:55:16

子窗体的构造函数正在设置对其父窗体的引用。

关于“这个”的使用。如果构造函数包含赋值就会出现问题。

parent = parent; //this is circular 

为了引用同名的私有文件(在构造函数之外),需要在前面添加“this”:

this.parent = parent; //assign the value of the param to the private field

其他注释。这种风格似乎是从 Java 借来的,对于 C# 来说并不典型。

class FormChild
{

    private FormParent _parent; //reference types are automatically null

    public FormChild( FormParent parent)//pass in the reference to the parent
    {
       InitializeComponent();
       _parent = parent; //assign the reference param to the private field.
    }

}

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.

parent = parent; //this is circular 

In order to reference the private filed by the same name (outside of the constructor), 'this' is pre-pended:

this.parent = parent; //assign the value of the param to the private field

Other comments. The style seems to be borrowed from Java and would not be typical for C#.

class FormChild
{

    private FormParent _parent; //reference types are automatically null

    public FormChild( FormParent parent)//pass in the reference to the parent
    {
       InitializeComponent();
       _parent = parent; //assign the reference param to the private field.
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文