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

前端 未结 16 943
甜味超标
甜味超标 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 03:55

    I am working with WPF inside of Windows Forms.

    I hosted my WPF User Control in a Windows Forms Element Host. Due to that when InitializeComponent() got called I executed Code before reaching the InitializeComponent() of my WPF control. Tricky.

    So I moved it out of my constructor, Clean Build, Rebuild, Restart VS and everything works as expected. Finally.

    0 讨论(0)
  • 2020-12-15 03:59

    In my case, I added a third party control in my Toolbar(via a .dll file), and draw one of it in my form. And for some reason, my Toolbar clean this third party control out of the general group(I added it in the general group), so VS cannot find this control. Here is what I done to slove this problem:

    1. Add this control into the Toolbar.
    2. Clean the solution
    3. Rebuild the solution

    Redraw the control if neccessary.

    0 讨论(0)
  • 2020-12-15 04:00

    So, I have had the same problem in the past, for fix I did the following:

    • Solution → Clean Solution;
    • Build → Rebuild Solution;
    • Close Visual Studio, re-open.

    Thanks a lot to Marshall Belew!

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题