2

I have a user control named Editor that is part of the StackCustomWindow namespace. The StackCustomWindow namespace contains the main form for the program. When I add the Editor user control to the main window using the designer, Visual Studio 2010 places code like this in the designer:

this.editor1 = new StackCustomWindow.Editor();

when it should just be this:

this.editor1 = new Editor();

Compiling throws an exception:

Error 1 The type name 'Editor' does not exist in the type

'StackCustomWindow.StackCustomWindow' C:\Users\ricardo\Documents\Visual Studio

2010\Projects\StackCustomWindow\StackCustomWindow\StackCustomWindow.Designer.cs 35 51 StackCustomWindow

I found a similar question, with a solution that said a duplicate name must exist somewhere in the solution, but a) I don't know why that would exist in this case, since I have no other controls with that name, and b) I don't know how to check if a duplicate name actually does exist. I don't have any other user controls or items named Editor, and Visual Studio does this for all of my user controls. As you can see below, all of the code is very basic, with no controls besides the user control and the main window.

Code for Editor:

using System.Windows.Forms;

namespace StackCustomWindow
{
    public partial class Editor : UserControl
    {
        public Editor()
        {
            InitializeComponent();
        }
    }
}

and it's designer.cs file:

namespace StackCustomWindow
{
    partial class Editor
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // Editor
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "Editor";
            this.Size = new System.Drawing.Size(452, 276);
            this.ResumeLayout(false);

        }

        #endregion

    }
}

StackCustomWindow.cs:

using System.Windows.Forms;

namespace StackCustomWindow
{
    public partial class StackCustomWindow : Form
    {
        public StackCustomWindow()
        {
            InitializeComponent();
        }

    }
}

StackCustomWindow.designer.cs:

namespace StackCustomWindow
{
    partial class StackCustomWindow
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // StackCustomWindow
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(718, 535);
            this.Name = "StackCustomWindow";
            this.Text = "StackCustomWindow";
            this.ResumeLayout(false);

        }

        #endregion

    }
}
2

2 Answers 2

10

The problem is that the StackCustomWindow Type is named the same as the StackCustomWindow namespace. So StackCustomWindow.Editor ends up being interpreted as "member 'Editor' of type StackCustomWindow", instead of "member 'Editor' of namespace StackCustomWindow".

Using a different name for the namespace and the type will prevent this problem.

Your code is valid, the code generated by the designer isn't. The code generator doesn't expect you to do what you did and doesn't handle it well.

3
  • 1
    It's interesting that Visual Studio doesn't warn you about this, but maybe it's because it's such an amateur mistake or such a warning could over-complicate matters (and yes, this fixes the problem). Aug 6, 2012 at 15:04
  • @svinja you nailed it! the same occurs in VB .NET if you have for instance a project with a module declared using the same project's name. Like AppGlobals.AppGlobals.vb. VB users (not used to see Namespace declarations): change this namespace in Project Properties' first tab. Sep 21, 2015 at 17:08
  • still encountering the same problem even in vs2017, instead of changing the name of the class, which had been used at multiple places I changed the namespace since mine was a much smaller project
    – yolob 21
    Feb 27, 2020 at 14:27
-1

You may be clashing with the System.Collections.Stack System.Collections.Generic.Stack class. Are you using either of these namespaces (System.Collections, System.Collections.Generic)?

1
  • No, I'm not including either of those. I don't know why that would cause a conflict with Editor, though. If I rename Stack to StackCustomWindow I still have problems (I updated the question to reflect the name change). Aug 6, 2012 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.