可以引用“这个”吗?在构造函数中?

发布于 2024-10-19 07:12:39 字数 364 浏览 2 评论 0原文

在 C# 中,我使用的常见模式是使用表单对象填充较低计算类的详细信息。

MyForm 的构造函数是:

MyForm()
{
   _MyFormCalcs = new MyFormCalcs(this);
}

但我今天遇到了一个错误,这让我认为由于我的构造函数尚未完成,它创建了一个新的 MyForm 实例以传递给 MyData。因此它调用了构造函数两次。我发现 MyFormCalcs 中的静态列表被填充了两次,但第二次失败,因为密钥已经存在于列表中。

我可以在构造函数中使用 this 来引用这个实例吗?下层类中将包含什么 - 构造函数是否已运行。

将我的形式传递给下层阶级的更好方法是什么?

In C#, a common pattern that I use is to populate the details of a lower calculation class with a form object.

The constructor for MyForm is:

MyForm()
{
   _MyFormCalcs = new MyFormCalcs(this);
}

But I encountered an error today which makes me think that as my constructor had not finished completing, it creates a new instance of MyForm to pass into MyData. Thus it calls the constructor twice. I found that a static list in MyFormCalcs was being filled twice and was failing the second time as the keys were already present in the list.

Can I use this in the constructor to refer to this instance? What will it contain in the lower class - has the constructor been run or not.

What is a better way of passing my form into the lower class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

南七夏 2024-10-26 07:12:39

不,这不会创建 MyForm 的新实例。

一般来说,允许 this 从构造函数中“逃逸”是 危险,因为这意味着它可以在构造函数完成之前使用,但它不会创建一个新实例。如果您可以给出您所遇到的问题的简短但完整的示例,我们可以帮助进一步诊断。特别是,不清楚“静态列表被填充两次”是什么意思。通常,在实例构造函数中填充静态变量并不是一个好主意。

No, that won't create a new instance of MyForm.

In general, allowing this to "escape" from a constructor is dangerous as it means it can be used before the constructor has completed, but it's not going to be creating a new instance. If you could give a short but complete example of the problem you've seen, we could help diagnose it further. In particular, it's not clear what you mean about the "static list being filled twice". Usually it's not a good idea to populate a static variable in an instance constructor.

愁以何悠 2024-10-26 07:12:39

事实上,避免在构造函数内部进行此类调用确实很好,因为您的对象尚未构建(构造函数尚未完成其工作),但您已经使用这个“未完成”的对象作为参数。这是一个糟糕的方法。好的方法是创建一些特殊的方法:

class MyClass
{

 var obj:SomeClass;

  public MyClass()
  {
  }

  public Init()
  {
    obj = SomeClass(this);
  }

}

In fact, it is really nice to avoid such call inside constructor, because your object is not already built (constructor hasnt finish its work) but you are already use this "unfinished" object as a parameter. Its a bad way. Good way is creating some special method:

class MyClass
{

 var obj:SomeClass;

  public MyClass()
  {
  }

  public Init()
  {
    obj = SomeClass(this);
  }

}
回忆躺在深渊里 2024-10-26 07:12:39

创建一个私有属性,仅在第一次使用时实例化 MyFormCalcs,如下所示:

public class MyForm {

  private MyFormCalcs MyFormCalcs {
    get {
      _MyFormCalcs = _MyFormCalcs ?? new MyFormCalcs(this);   
    }
  }
}

这样,您就不必考虑何时“初始化”事物。

Make a private property that instantiate MyFormCalcs only when it is first used, like this:

public class MyForm {

  private MyFormCalcs MyFormCalcs {
    get {
      _MyFormCalcs = _MyFormCalcs ?? new MyFormCalcs(this);   
    }
  }
}

This way, you don't have to think about when to "initialize" things.

笨死的猪 2024-10-26 07:12:39

C# 构造函数顺序有一个非常完整的响应:

C# 构造函数执行顺序

There is this very complete response of C# constructor order:

C# constructor execution order

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