8

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.

Andreas
  • 5,393
  • 9
  • 44
  • 53
Romz
  • 1,437
  • 7
  • 36
  • 63
  • 1
    That's a designer issue. Re-build your solution and restart Visual Studio. – SLaks Sep 09 '12 at 20:39
  • 1
    Side-note: imho it's more readable to write `base.tree` instead of `this.tree` here. – Tim Schmelter Sep 09 '12 at 20:44
  • I don't see any problem with those fields. But you must decide if `MyClass` is `public` or not. The two `partial class` declaration must agree. – Jeppe Stig Nielsen Sep 09 '12 at 20:47
  • never-assigned is a proper warning for the BaseClass. It looks like it should be abstract. – H H Sep 09 '12 at 20:49
  • @TimSchmelter Imho, one should only use the `base` keyword when this type overrides (or hides) a member from the base class. In this case, the member is just inherited, so I think `this.tree` (or just `tree` with no specification of what instance) is best. – Jeppe Stig Nielsen Sep 09 '12 at 20:51
  • @JeppeStigNielsen: Altough no access modifier means most restrictive accessibility which is public when the other part of the class is public. – Tim Schmelter Sep 09 '12 at 20:56
  • @HenkHolterman But it's just a field. It can't be abstract (unless it's changed to a property). – Jeppe Stig Nielsen Sep 09 '12 at 20:56
  • @jeppe - the class (BaseClass) can be abstract. – H H Sep 09 '12 at 20:57
  • @Henk Holterman, warnings not for the BaseClass for MyClass when I try to add controls: `this.Controls.Add(this.tree)` . It can't be abstract, but it has virtual functions – Romz Sep 09 '12 at 20:59
  • @TimSchmelter You are right; I didn't know that. I thought the second "part" was declared (implicitly) `internal`, and therefore inconsistent with the first part, but I was wrong. – Jeppe Stig Nielsen Sep 09 '12 at 20:59
  • @JeppeStigNielsen: You'll get a compile time exception if they would have different access modifiers. But i must admit that i also didn't know that C# defaults to the other parts accessibility on partial classes. I had to test it. – Tim Schmelter Sep 09 '12 at 21:02
  • @William I tested your code and It works fine, as said SLaks try to rebuild the project and restart VS. – Omar Sep 09 '12 at 21:09
  • @TimSchmelter: IMHO `tree` would be better than either. – Ed S. Sep 09 '12 at 21:12

2 Answers2

6

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.

Community
  • 1
  • 1
davmos
  • 9,324
  • 4
  • 40
  • 43
  • My `UserControl` constructor was expecting a value. If I passed in a reference to a `static readonly` member variable, the designer would throw an exception (*The variable is either undeclared or was never assigned*). Unfortunately the above fix does not work for me in *CSharp* (*Visual Studio 2013*). Ended up moving the input parameter to an `Initialize` method that I added to the `UserControl`. Interestingly enough, if I simply instantiated the required type directly into the constructor's input parameter, everything was ok: `this.myControl = new MyUserControl(new CustomType());` – Pressacco Apr 14 '14 at 20:46
  • @Pressacco interesting... sounds like another question. – davmos Apr 17 '14 at 07:17
  • For me, this error went away when I changed Solution Platforms from Mixed Platforms to Any CPU. – Alex Mar 21 '17 at 16:03
  • This issue happened to me because I was building for x64 but the Visual Studio designer is hosted in-process in Visual Studio which is still a 32-bit process (even VS2017). After rebuilding for x86 and reloading the designer it worked. – Dai Jun 13 '17 at 19:47
0

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.