Search code examples
c#.netcastingtype-conversiontypeconverter

Unable to cast object of type... converting a label into a double variable


hi in this code im trying to convert a double from a label into the variable but im coming up with the exception

"Unable to cast object of type 'System.Windows.Forms.Label' to type 'System.IConvertible'."

on the "convert.toDouble(lblPricekey);" area and I'm not sure why.

  private void btnAddtoTotal_Click(object sender, EventArgs e)
    {
        double numPadTotal;
        numPadTotal = Convert.ToDouble(lblPricekey.Text);

        double finalTotal = total + numPadTotal;
        txtTotal.Text = finalTotal.ToString();
    }

Solution

  • You are converting Label to double. You should convert Label.Text.

    numPadTotal = Convert.ToDouble(lblPricekey.Text);