为什么普通的类实例化会导致未处理的溢出异常?

Why does a normal class instantiation cause an unhandled overflow exception?
2021-06-20
  •  译文(汉语)
  •  原文(英语)

除了正常的类初始化外,我看不到其他任何东西.

这是Bet.cs类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab_ADayAtTheRaces
{
public class Bet : Form1
{
    public int Bets_Joe;
    public int Bets_Bob;
    public int Bets_Al;
    public int Dog_Joe;
    public int Dog_Bob;
    public int Dog_Al;

    public int Amount;
    public int Dog;
    public Guy Bettor;

    public string GetDescription()
    {
        Amount = (int)numericUpDownBucksToBet.Value;
        Dog = (int)numericUpDownDogToBetOn.Value;
        //Bettor =
        return Bettor + " placed a bet in the amount of " + Amount + " bucks on dog number " + Dog;
    }

    public int PayOut(int Winner)
    {
        return Winner;
    }

    public void MakeBets()
    {
        if (joeRadioButton.Checked == true)
        {
            Bets_Joe = (int)numericUpDownBucksToBet.Value;
            Dog_Joe = (int)numericUpDownDogToBetOn.Value;
        }

        else if (bobRadioButton.Checked == true)
        {
            Bets_Bob = (int)numericUpDownBucksToBet.Value;
            Dog_Bob = (int)numericUpDownDogToBetOn.Value;
        }

        else if (alRadioButton.Checked == true)
        {
            Bets_Al = (int)numericUpDownBucksToBet.Value;
            Dog_Al = (int)numericUpDownDogToBetOn.Value;
        }
    }

}

}

这是引发异常的代码:

namespace Lab_ADayAtTheRaces
{
public partial class Form1 : Form
{
    Bet bets = new Bet(); //**THIS LINE THROWS THE STACKOVERFLOW EXCEPTION**

    Greyhound[] dogs = new Greyhound[3];

它想让我说些更多的话,但是我没有其他要添加的内容,因此我将在此处和此处添加一些行.它想让我说些更多的话,但是我没有其他要添加的内容,因此我将在此处和此处添加一些行.它想让我说些更多的话,但是我没有其他要添加的内容,因此我将在此处和此处添加一些行.它想让我说些更多的话,但是我没有其他要添加的内容,因此我将在此处和此处添加一些行.非常感谢您的任何帮助...在此先感谢Kristjan

速聊1:
请添加确切的异常类型和消息.
速聊2:
恰如其分:您应该使用公共属性,而不要公开您的字段(Bets_Joe,Bets_Bob等).
解决过程1

您的Bet继承自Form1,因此Bet()将调用Form1(),然后在其内部Form1()再次调用Bet()->一遍又一遍->StackOverflow

提示:我们永远不要像这样在类的构造函数或其类定义中调用该类的构造函数:

public class Form1 : Form {
    public Form1(){
    }
    public Form1(string s){
    }
    public Form1 f = new Form1();//this will throw StackOverflowException
    public Form1 f = new Form1("");//this will also throw StackOverflowException       
    //Form2 inherits from Form1
    public Form2 f = new Form2(); //this will throw StackOverflowException
}
//or
public class Form1 : Form {
    public Form1(){
        Form1 f = new Form1();//This will throw stackoverflowexception
        Form1 f = new Form1("");//This won't throw any exception
    }
    public Form1(string s){
    }
}

初始化类时,所有成员在调用构造函数之前首先被初始化,因此该行在Bet bets = new Bet();初始化your之前执行Form1.因此,您必须避免这种情况.要初始化new Bet(),您应该在某个事件中调用它,以使该事件永远不会通过调用构造函数来触发Load.

Bet bets;//Just declare it without initializing it
private void Form1_Load(object sender, EventArgs e){
   bets = new Bet();
}

I can't see anything other than an normal class initialized.

Here is the class, class Bet.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab_ADayAtTheRaces
{
public class Bet : Form1
{
    public int Bets_Joe;
    public int Bets_Bob;
    public int Bets_Al;
    public int Dog_Joe;
    public int Dog_Bob;
    public int Dog_Al;

    public int Amount;
    public int Dog;
    public Guy Bettor;

    public string GetDescription()
    {
        Amount = (int)numericUpDownBucksToBet.Value;
        Dog = (int)numericUpDownDogToBetOn.Value;
        //Bettor =
        return Bettor + " placed a bet in the amount of " + Amount + " bucks on dog number " + Dog;
    }

    public int PayOut(int Winner)
    {
        return Winner;
    }

    public void MakeBets()
    {
        if (joeRadioButton.Checked == true)
        {
            Bets_Joe = (int)numericUpDownBucksToBet.Value;
            Dog_Joe = (int)numericUpDownDogToBetOn.Value;
        }

        else if (bobRadioButton.Checked == true)
        {
            Bets_Bob = (int)numericUpDownBucksToBet.Value;
            Dog_Bob = (int)numericUpDownDogToBetOn.Value;
        }

        else if (alRadioButton.Checked == true)
        {
            Bets_Al = (int)numericUpDownBucksToBet.Value;
            Dog_Al = (int)numericUpDownDogToBetOn.Value;
        }
    }

}

}

Here is the code that throws the exception:

namespace Lab_ADayAtTheRaces
{
public partial class Form1 : Form
{
    Bet bets = new Bet(); //**THIS LINE THROWS THE STACKOVERFLOW EXCEPTION**

    Greyhound[] dogs = new Greyhound[3];

It wants me to say something more but I have nothing more to add so I'll just add some lines here and here. It wants me to say something more but I have nothing more to add so I'll just add some lines here and here. It wants me to say something more but I have nothing more to add so I'll just add some lines here and here. It wants me to say something more but I have nothing more to add so I'll just add some lines here and here. Any help is muchly appriciated... thanks in advance Kristjan

Talk1:
Please add the exact exception type and message.
Talk2:
Just as a side note: you should use public properties instead of exposing your fields (Bets_Joe, Bets_Bob, etc).
Solutions1

your Bet inherits from Form1, so Bet() will call to Form1(), and Form1() will again call to Bet() inside it -> again and again -> StackOverflow

Tip: We should never call the constructor of a class in its constructor or its class definition like this:

public class Form1 : Form {
    public Form1(){
    }
    public Form1(string s){
    }
    public Form1 f = new Form1();//this will throw StackOverflowException
    public Form1 f = new Form1("");//this will also throw StackOverflowException       
    //Form2 inherits from Form1
    public Form2 f = new Form2(); //this will throw StackOverflowException
}
//or
public class Form1 : Form {
    public Form1(){
        Form1 f = new Form1();//This will throw stackoverflowexception
        Form1 f = new Form1("");//This won't throw any exception
    }
    public Form1(string s){
    }
}

When a class is initialized, all the members are initialized first before calling the constructor, hence the line Bet bets = new Bet(); is executed before initializing your Form1. So you have to avoid it. To initialize new Bet(), you should call it in some event so that the event will never be fired by calling the constructor, such as Load.

Bet bets;//Just declare it without initializing it
private void Form1_Load(object sender, EventArgs e){
   bets = new Bet();
}
转载于:https://stackoverflow.com/questions/18571778/why-does-a-normal-class-instantiation-cause-an-unhandled-overflow-exception

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入