将两个类中的每一个类实例化

发布于 2024-11-29 00:12:17 字数 700 浏览 0 评论 0原文

我有一个 C# 类问题,希望得到一些帮助。我有一个名为 CountdownUserControl 的类。该类中有许多函数。然后我有另一个名为 Min 的课程。在 CountdownUserControl 类中,我需要这种形式的某些东西,因此我在 CountdownUserControl 中创建它的实例:

public partial class CountdownUserControl : UserControl
{
    //-----------------------------------------------
    // Private data members
    //-----------------------------------------------
    private Min _Min = new Min();

但是,在 Min 类中,我还想使用 CountdownUserControl 类中包含的函数 - 但是我无法创建它在 Min 类中,例如

public partial class Min : Form
{
    private CountdownUserControl CU = new CountdownUserControl();

在 Min 类中我可以执行 CU.Method_I_want();

因为这会导致堆栈溢出。有谁知道这个问题的解决方案?感谢您抽出时间。

I have a C# class question which I hope to get some help with. I have one class called CountdownUserControl. There are a number of functions within this class. I then have another class called Min. There are certain things I need in this form within CountdownUserControl class so I create an instance of it within CountdownUserControl:

public partial class CountdownUserControl : UserControl
{
    //-----------------------------------------------
    // Private data members
    //-----------------------------------------------
    private Min _Min = new Min();

However within the Min class I also would like to use a function which is contained within the CountdownUserControl class - however I cannot create an instance of it within the Min class such as

public partial class Min : Form
{
    private CountdownUserControl CU = new CountdownUserControl();

so that within Min class I could do CU.Method_I_want();

as this will give a stackoverflow. Does anyone know a solution around this? Thanks for your time.

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

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

发布评论

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

评论(9

庆幸我还是我 2024-12-06 00:12:17

类和方法

类不应该仅仅被视为方法的集合,当使用new创建(“实例化”)它们时,它们就成为应该被如此对待的实际对象。

您需要重新考虑您的策略,而只是引用一个MinCoundownUserControl对象。每一项代表什么?您建议 UserControlForm 的一部分)需要为其创建自己的 Form - 事实并非如此。 Form创建UserControl 的对象。 UserControl 可以使用ParentForm 属性访问Form

StackOverflow

当您执行以下操作:new Min()时,您正在创建一个新的Min对象。然后,在 Min 中,当您执行以下操作:new CountdownUserControl() 时,您将创建一个新的 CounddownUserControl 对象。这又创建了一个新的 Min 对象,依此类推。

如何解决您的问题

简而言之,如果您确定用户控件位于其类型的 Form 上,请在用户控件中使用 Min min = (Min)this.ParentForm;最小值

最后/此外,您不应该自己创建 CountdownUserControl 实例,除非您要设置属性并将其添加到代码中表单的 Controls 集合中。相反,构建您的项目,将 CountdownUserControl 拖放到表单上 - 将在您的 Min 类上自动创建一个用于访问该控件的属性,称为 倒计时用户控件1。

Classes and methods

Classes shouldn't just been seen as a collection of methods, when they are created ('instantiated') with new, they become actual objects that should be treated as such.

You need to re-think your strategy and instead simply reference a Min or CoundownUserControl object. What does each one represent? You are suggesting that the UserControl (a part of a Form) needs to have its own Form created for it - that's not the case. The Form is the object that creates the UserControl. The UserControl can access the Form by using the ParentForm property.

The StackOverflow

When you do this: new Min() you are creating a new Min object. Then, in Min, when you do this: new CountdownUserControl() you are creating a new CoundownUserControl object. Which in turn creates a new Min object, and so on and so forth.

How to fix your problem

Simply put, use Min min = (Min)this.ParentForm; within the user control if you are sure the user control is on a Form whose type is Min.

And finally/additionally, you should not create the instance of CountdownUserControl yourself unless you are going to set the properties and add it to the Controls collection of the form within your code. Instead, build your project, drag-and-drop the CountdownUserControl onto your form - a property to access the control will be created on your Min class automatically, called countdownUserControl1.

过期以后 2024-12-06 00:12:17

所有这些答案都很好,但您还应该考虑这样一个事实:循环依赖通常确实很难与. 一起工作。您的类应该是一些孤立的单元,具有单一目的。它们应该彼此松散耦合。如果您重新设计类以消除循环依赖并遵循这些准则,那么您通常会编写出更好且更易于维护的代码,并且首先会减少此类问题。

All these answers are great, but you should also consider the fact that cyclic dependencies are generally really difficult to work with. Your classes should be somewhat isolated units with a single purpose. They should be loosely coupled from each other. If you redesign your classes to eliminate cyclic dependencies and follow these guidelines, you will write better and more maintainable code in general and will have less issues like this in the first place.

红颜悴 2024-12-06 00:12:17

对此肯定有很多解决方案。一个简单的方法是将当前的 Min 传递给 CountdownUserControl 在其构造函数中(反之亦然):

private Min min;
public CountdownUserControl(Min min)
{
     this.min = min;
}

但是,当您面临此类问题时,您的类结构可能存在问题。也许您可以将类中的某些功能拆分为新的类来避免此问题。

There surely are plenty of solutions to this. An easy one would be to pass the current Min to CountdownUserControl in its constructor (or vice versa):

private Min min;
public CountdownUserControl(Min min)
{
     this.min = min;
}

When you are facing these kind of problems, there probably is something wrong with your class structure, though. Maybe you could split some of the functionality out of your classes into a new one to avoid this problem.

∝单色的世界 2024-12-06 00:12:17

您可以在构造函数中设置它的值,而不是每次都实例化一个新的 Min 类。

public CountdownUserControl(Min min)
{
     _Min = min;
...

Instead of instantiating a new Min class every time, you could set the value of it in a constructor.

public CountdownUserControl(Min min)
{
     _Min = min;
...
ˇ宁静的妩媚 2024-12-06 00:12:17

CountdownUserControl 是否会出现在 Min 表单上?为什么要从控件实例化表单,而不是相反?倒计时控件不应该在窗体上吗?然后您可以实例化表单,初始化控件,然后访问该函数。

Is CountdownUserControl going to be on the Form Min? Why would you instantiate the form from the control, and not the reverse? Shouldn't the Countdown control be on the form? Then you could instantiate the form, initialize the control, and then access the function.

薄荷港 2024-12-06 00:12:17

这里的问题是,每当创建 CountdownUserControl 时,它都会尝试创建一个新的 Min,但是每当创建新的 Min 时,它都会尝试创建一个新的 Min。创建一个新的CountdownUserControl。这个过程可能会无限期地继续下去,因此堆栈会被烧毁。

解决方案是将 CountdownUserControl 实例注入到 Min 中(反之亦然,具体取决于首先创建的内容)。

所以 CountdownUserControl 将如下所示:

public partial class CountdownUserControl : UserControl
{
    //-----------------------------------------------
    // Private data members
    //-----------------------------------------------
    private Min _Min;

    public CountdownUserControl(Min min)
    {
        _Min = min;
    }

...而 Min 将如下所示:

public partial class Min : Form
{
    private CountdownUserControl CU;

    public Min()
    {
        CU = new CountdownUserControl(this);
    }

The problem here is that whenever a CountdownUserControl is created, it tries to create a new Min, but whenever a new Min is created, it tries to create a new CountdownUserControl. This process could continue indefinitely and therefore the stack is blown.

The solution is to inject the instance of CountdownUserControl into Min (Or vice versa, depending on what gets created first).

So CountdownUserControl will look like this:

public partial class CountdownUserControl : UserControl
{
    //-----------------------------------------------
    // Private data members
    //-----------------------------------------------
    private Min _Min;

    public CountdownUserControl(Min min)
    {
        _Min = min;
    }

...and Min will look like this:

public partial class Min : Form
{
    private CountdownUserControl CU;

    public Min()
    {
        CU = new CountdownUserControl(this);
    }
爱殇璃 2024-12-06 00:12:17

不要在用户控件中创建窗体。将其添加为依赖项:

public partial class CountdownUserControl : UserControl
{
    private Min _Min;

    public CountdownUserControl(Min parentForm);

然后在您的表单中使用它:

public partial class Min : Form
{
    private CountdownUserControl CU;

    public Min()
    {
       CU = new CountdownUserControl (this);
    }

更新

更合适的解决方案是从两个类中分离出公共功能并将它们放入单独的类中。阅读单一责任原则。我在我的博客中收到了一篇关于它的帖子。

Do not create a form in the user control. Add it as a dependency:

public partial class CountdownUserControl : UserControl
{
    private Min _Min;

    public CountdownUserControl(Min parentForm);

And then use it in your form:

public partial class Min : Form
{
    private CountdownUserControl CU;

    public Min()
    {
       CU = new CountdownUserControl (this);
    }

Update

A more suitable solution would be to break out the common functionality from the two classes and put them in separate classes. Read up on single responsibility principle. I got a post about it in my blog.

怎会甘心 2024-12-06 00:12:17

您可以将 CountdownUserControl 的实例传递给 Min 类(通过附加构造函数参数或通过属性)。

public partial class Min: Form {
    public Min() {
        InitializeComponent();
    }

    public CountdownUserControl Countdown {get;set;}
}

由于这是一种形式,您可能希望使用该属性,以免破坏设计器。

我不会谈论这是否是正确的形式(希望您使用的是PresentationModel 或MVC 之类的东西),因为我知道有时需要这样的代码。

现在正如已经指出的那样,如果您计划在表单中显示用户控件,那么应该采用不同的解决方案。但这完全取决于您如何计划如何使用该表单。例如,如果您在对话框中显示表单,则保留类级别引用是一个坏主意,因为一旦关闭,表单将被丢弃。

更多细节将使您更接近完全正确的答案。

You can pass in an instance of your CountdownUserControl to your Min class (via an additional constructor parameter or via a property).

public partial class Min: Form {
    public Min() {
        InitializeComponent();
    }

    public CountdownUserControl Countdown {get;set;}
}

Since this is a form you probably want to go with the property so as not to break the designer.

I won't speak to whether or not this is proper form (hopefullt you are using something like PresentationModel or MVC), as I do know tehre are times code like this is required.

Now as has been pointed out, if you plan on showing the user control in the form then a different solution should be adopted. But it all depends on how you are planning on what you are doing with the form. If you are for example showing teh form in a dialog keeping a class level reference is a bad idea since once closed the form will be disposed.

A little more detail would get you closer to a fully correct answer.

春花秋月 2024-12-06 00:12:17

但是,在 Min 类中,我还想使用 CountdownUserControl 类中包含的函数 - 但是我无法在 Min 类中创建它的实例,例如

提到的函数是否真的有必要是 CountdownUserControl 的成员?

你能透露一下你的类的更多公共接口吗?
如果该函数没有对 CountdownUserControl 实例的引用,请考虑将其更改为静态。否则尝试设计名为 CountdownState 的新类,其实例可以在 CountdownUserControl 和 Min 之间共享。

However within the Min class I also would like to use a function which is contained within the CountdownUserControl class - however I cannot create an instance of it within the Min class such as

Is it really necessary that mentioned function is member of CountdownUserControl?

Could you reveal more of public interface of your classes?
If the function has no reference to instance of CountdownUserControl consider changing it to static. Else try to design new class named e.g. CountdownState whose instance may be shared among both CountdownUserControl and Min.

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