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

前端 未结 16 944
甜味超标
甜味超标 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:48

    Don't put anything other than InitializeComponent(); in the constructor. You can put the code from there in events like Load().

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

    In my case, I had an older Windows Forms project where InitializeComponents() started like this:

    private void InitializeComponent()
    {
        var componentResourceManager = new ComponentResourceManager(typeof(MyForm));
        ...
    

    This resulted in an error message later on when accessing the componentResourceManager inside InitializeComponent():

    The variable 'componentResourceManager' is either undeclared or was never assigned.

    When comparing with a newly created form, I saw that it was similar to my non-working form, except for one thing:

    The variable was not named componentResourceManager but simply resources.

    Solution

    After doing a rename on my variable to also have the name resources, everything works successfully:

    private void InitializeComponent()
    {
        var resources = new ComponentResourceManager(typeof(MyForm));
        ...
    

    The Windows Forms Designer in Visual Studio 2017 did open the form correctly.

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

    First I had code that referenced something a type that the designer couldn't load (for whatever reason). Then I had code in the constructor that couldn't be executed from my local laptop. I decided the best option was to move the logic to the Load event and check if the component was in DesignMode and exit if it was.

    Even this wasn't enough for me as the designer still tried to JIT the type that was later down in the method, so I had to move it out to a separate method to stop that from happening. Here's basically what I ended up with:

        private void userControl_Load(object sender, EventArgs e)
        {
            if (DesignMode) return;
    
            Initialize();
        }
    
        private void Initialize()
        {
            // do your work
        }
    

    Special thanks to this SO answer which pointed me to a comment in a blog post about not accessing the DesignMode property until you're in the Load event...

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

    Maybe the error occurs due to your constructor code. Place InitializeComponent(); at the beginning of the constructor like this:

    public FormularioGeneral()     
    {         
        InitializeComponent();
        ConfigurarUI();         
        AccionesConstructor();
        PostInicializacionComponentes();         
        EstablecerIcono();         
        InicializarLocalizacionFormulario();     
    } 
    

    Explanation:

    The variables are initialized in that method.

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

    Renaming the variable componentResourceManager to resources solved error.

    Unfortunately i had to change a ton of other items to get the designer working for Telerik reports designer

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

    This error occurs for me while creating a third party control in InitializeComponent() which is called from form constructor. When I created it after InitializeComponent() it works fine for me.

    public MyForm() //Form constructor
    {         
        InitializeComponent();
    
        //Create/initialize third party control here with new operator    
    }
    
    0 讨论(0)
提交回复
热议问题