开发者

Why does one class contained in a namespace not see another one contained in the same namespace?

I've been work开发者_C百科ing on an application and I've run across an unusual error that I've never seen before. I have two classes, one being a UserControl and one being a Form, set up like so:

namespace NC
{
    public partial class Board : UserControl
    {
        // stuff
    }
}

namespace NC
{
    partial class Board
    {
        // this is the *.designer.cs file
    }
}

namespace NC
{
    public partial class MainForm : Form
    {
        // normal form stuff
    }
}

namespace NC
{
    partial class MainForm
    {
        // this is the *.designer.cs file. it has a Board added to it.
    }
}

It's all contained in the same project so it shouldn't be an issue. However, I get this when I compile:

The type name 'Board' does not exist in the type 'NC.NC'

I looked in the Form's designer file and found this:

this.Board1 = new NC.Board();

I remove the namespace and it works fine. Every time I access the forms editor, it adds it again. I've never had any issue with this before. Is there some kind of setting or something I can change?


Do you have a class named NC? If you do, you'd do well to rename it to something else. It sounds like the compiler is looking for Board inside a class named NC, not the namespace NC...


Your project settings page have a default namespace. Take a look there.

In fact, the default namespace name shouldn't be causing this behavior. For example, I just created an empty Windows Forms app and its default namespace is set with WindowsFormsApplication1, that is the name of the Solution inside VS.


If you are using a namespace to access a class you either need to use the full namespace (which is probably ProjectName.NC.Board) or it has to be deeper than any namespace you have access to.
Board is in the same namespace that you are already in, so you don't need to qualify it with the namespace.


If you're calling [namespace].[class] from within its own namespace, so C# assumes you're looking for a class called NC even though you don't have one. That is, it is automatically appending NC. to anything you're calling that isn't referencing an outside namespace (System.[whatever]). So your call to NC.Board gets translated to NC.NC.Board

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜