Romz
Romz

Reputation: 1457

The variable is either undeclared or was never assigned warning

This is the base class:

public class BaseClass : UserControl
{
     protected ListView list;
     protected TreeView tree;
    
     public BaseClass()
     {
         //...
     }
     //...
}

Child class:

public partial class MyClass : BaseClass
{
     public MyClass()
     {
         InitializeComponent();
         this.BackColor = VisualStyleInformation.TextControlBorder;
         this.Padding = new Padding(1);
     }
     //...
}
    
partial class MyClass
{
    //...
        
    private void InitializeComponent()
    {
         this.tree = new System.Windows.Forms.TreeView();
         this.list = new System.Windows.Forms.ListView();
         //...
         this.tree.Location = new System.Drawing.Point(0, 23);
         this.tree.Name = "blablabla";
    }
}

Compiling the classes gives me these warnings:

Warning 1 The variable 'tree' is either undeclared or was never assigned.
Warning 2 The variable 'list' is either undeclared or was never assigned.

What am I doing wrong? These variables are declared in base class and assigned in child class.

Upvotes: 8

Views: 16323

Answers (3)

joao
joao

Reputation: 1

don't assign the variables inside private void InitializeComponent(); instead "Double click on the form and in it's Load event" make the assignments. (this will keep the designer happy). Kudos to "https://mbmproject.com/blog/tutorials/windows-forms-c-variables-strings-and-boolean"

Upvotes: 0

mainframe007
mainframe007

Reputation: 1

I have run into this with the designer and the main cause each time was the fact my solution was set for x64 output. Currently, the designer CANNOT deal with a control in a solution set for x64, so you need to change it to Any CPU, make your edits, then change it back to x64 to do your running/debugging.

Upvotes: 0

davmos
davmos

Reputation: 9587

This question lacks an answer, so here goes...

Rebuild Solution then restart Visual Studio worked for me :-)

Thanks to SLaks for his comment above.

Also discussed at:

stackoverflow.com - The variable 'variable_name' is either undeclared or was never assigned.

social.msdn.microsoft.com - The variable 'control_name' is either undeclared or was never assigned.

Upvotes: 6

Related Questions