The variable 'variable_name' is either undeclared or was never assigned

前端 未结 16 941
甜味超标
甜味超标 2020-12-15 03:14

I have a question related to the error on the title. Im working with c# and Visual Studio 2010.

I have a form declared as \"public class FormularioGeneral : Form\",

16条回答
  •  [愿得一人]
    2020-12-15 04:03

    About the variables, can you simply initialize them in the declaration? I think that would suffice, even if you change the value later. From what I'm seeing, the compiler is unable to check whether you have initialized them or not because it's not directly on the constructor code, it's being done on a virtual method which will evaluate only at runtime.

    So, instead of:

    protected Color m_VariableName;
    public Color VariableName
    {
        get { return m_VariableName; }
        set { }
    }
    

    Do:

    protected Color m_VariableName = Color.White; // Or null
    public Color VariableName
    {
        get { return m_VariableName; }
        set { }
    }
    

    And a comment: you should avoid virtual calls in the constructor, which can lead to obscure errors in your application. Check it here.

提交回复
热议问题