这个简单的用户控制代码使VS2015崩溃。不知道为什么 - c#

public partial class displayvoltage : UserControl
{
    public displayvoltage()
    {
        InitializeComponent();
        if (!this.ratio_1.Checked && !this.ratio_12.Checked && !this.ratio_34.Checked && !this.ratio_14.Checked)
            this.ratio_1.Checked = true;
    }

    public double Ratio
    {
        get
        {
            if (this.ratio_1.Checked) return 1.0;
            if (this.ratio_1.Checked) return 4.0 / 3.0;
            if (this.ratio_1.Checked) return 2.0;
            return 4.0;
        }
    }

    public int SetRatio
    {
        set
        {
            if (value == 1) this.ratio_1.Checked = true;
            if (value == 2) this.ratio_34.Checked = true;
            if (value == 3) this.ratio_12.Checked = true;
            if (value == 4) this.ratio_14.Checked = true;
            SetRatio = value;
        }
    }

    [DefaultValue(0.0)]
    public double Voltage
    {
        get { return Voltage * this.Ratio; }
        set { Voltage = value; }
    }

    private bool DisplayVoltage = false;
    private bool Pause = false;

    private void ratio_CheckedChanged(object sender, EventArgs e)
    {
        RadioButton r = (RadioButton)sender;

        if (r.Checked) Invalidate();
    }
}

由设计师创建的仅4个收音机和一个面板。
即使我想显示VS崩溃控件的属性,即使我启动程序也会崩溃。可能是什么问题?

我可以拥有仅获得财产吗?

参考方案

可能有几个原因,但是最有可能的原因是,这会导致无限循环,从而导致StackOverflow:

public int SetRatio
{
    set
    {
        if (value == 1) this.ratio_1.Checked = true;
        if (value == 2) this.ratio_34.Checked = true;
        if (value == 3) this.ratio_12.Checked = true;
        if (value == 4) this.ratio_14.Checked = true;
        SetRatio = value;
    }
}

最后一行SetRatio可能正在调用SetRatio属性设置器,这将导致代码从以下位置再次执行:

 if (value == 1) this.ratio_1.Checked = true;
 if (value == 2) this.ratio_34.Checked = true;
 if (value == 3) this.ratio_12.Checked = true;
 if (value == 4) this.ratio_14.Checked = true;
 SetRatio = value;

并永远循环。 VS和.Net不能很好地处理堆栈溢出和内存不足异常。

尝试:

int setRatio;
public int SetRatio
{
    set
    {
        if (value == 1) this.ratio_1.Checked = true;
        if (value == 2) this.ratio_34.Checked = true;
        if (value == 3) this.ratio_12.Checked = true;
        if (value == 4) this.ratio_14.Checked = true;
        setRatio = value;
    }
}

如果这样不起作用,请尝试更改构造函数以查看是否是引起此问题的原因,因为具有构造函数的控件会引发异常,并且也会导致VS崩溃:

 public displayvoltage()
{
    InitializeComponent();
    //if (!this.ratio_1.Checked && !this.ratio_12.Checked && !this.ratio_34.Checked && !this.ratio_14.Checked)
    //    this.ratio_1.Checked = true;
}

LeetCode题解1131.maximum-of-absolute-value-expression

题目地址(1131. 绝对值表达式的最大值) https://leetcode-cn.com/problems/maximum-of-absolute-value-expression/description/ 题目描述 给你两个长度相等的整数数组,返回下面表达式的最大值: |arr1[i] - arr1[j]| + |arr2[i] - arr2[j]| …

如何创建Kafka压缩主题 - java

我有一个Kafka应用程序,该应用程序有一个producer谁会生成主题消息。 consumer然后从主题中获取消息,对给定的消息进行一些逻辑处理,然后将它们生成到另一个主题。我正在使用ProducerRecord和ConsumerRecords。我希望我的应用创建2个compacted topics,然后使用它们。如果compacted topics已经存…

为什么`if(guess!='a'|| guess!='A'||…)`不起作用? - java

Improve this question 这是我的代码,我知道if语句真的很长,代码可能会更高效,但是我只是想知道答案,因为它使我发疯。while (whileloop == 1) { if (guess != 'a' || guess != 'A' || guess != 'b' || gues…

从包含列表的嵌套字典创建数据框 - python

我找不到类似的答案来解决我的问题,所以我们开始:我有以下形式的字典:d = {key_1: { metric_1: [value_11, value_12], metric_2: [value_13, value_14], metric_3: value_15 }, key_2: { metric_1: [value_21], metric_2: [valu…

递归QuickSort遭受StackOverflowException - c#

我正在研究GenericList类中的递归QuickSort方法实现。我将有第二种方法,该方法接受compareDelegate来比较不同类型,但是出于开发目的,我对GenericList <int>进行了排序。我将根据列表大小在不同位置接收stackoverflow区域。我一直在盯着这个代码并在其中查找了几个小时,可能只需要一双(经验丰富的)眼…