Ricardo Altamirano
Ricardo Altamirano

Reputation: 15208

Why does Visual Studio append the namespace to the name of a user control when adding it to the designer?

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

    }
}

Upvotes: 2

Views: 2405

Answers (2)

Polyfun
Polyfun

Reputation: 9639

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)?

Upvotes: -1

svinja
svinja

Reputation: 5576

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.

Upvotes: 10

Related Questions