0___________
0___________

Reputation: 67486

This simple user control code crashes VS2015. Have no idea why

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();
    }
}

Created with the designer just 4 radios and one panel. Even if I want to display properties of the control VS crashes, if I start the program it crashes. What can be the problem?

Can I have a property with get only?

Upvotes: 1

Views: 74

Answers (1)

Alexander Higgins
Alexander Higgins

Reputation: 6923

Could be a few reasons, but most likely it is because this is causing an infinite loop which causes a 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;
    }
}

The last line SetRatio could be calling the SetRatio property setter which causes the code to execute again starting at:

 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;

And looping for ever. VS and .Net don't handle stack overflow and out of memory exceptions too well.

Try:

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;
    }
}

If that doesn't work try changing your constructor to see if that is causing the issue because controls with constructors that throw exceptions can cause VS to crash as well:

 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;
}

Upvotes: 2

Related Questions