views:

70

answers:

4

Hello,

I've been working 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?

A: 

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.

Leniel Macaferi
Does the Winforms designer use this default namespace when generating its code? What is the solution? To remove the default namespace?
Robert Harvey
As Mike Caron answered it looks like you have a class named NC that is conflicting and messing things up. Do you have such a class in you project/solution?
Leniel Macaferi
+3  A: 

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...

Mike Caron
This was exactly the problem. The project has grown far beyond its initial scope. It was originally just a single class that ran through some basic tests. I had forgotten that class had the same name as the namespace. Thank you!
Chris Boden
Mike: Good call. @Chris: Check this out for more details on why this is a horrid situation to be in: http://blogs.msdn.com/b/ericlippert/archive/tags/namespaces/
Eric Lippert
@Eric: Your blog post is what made me think of it :D
Mike Caron
@Eric: That's a good read, thanks! Normally I don't in actual projects. I originally just wrote a single class that was designed to test an idea I had. It then outgrew itself as I continued to have more ideas and I had completely forgotten about the original class.I appreciate the assistance.
Chris Boden
A: 

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.

JupiterP5
The Winforms designer is generating this code, so the problem lies elsewhere.
Robert Harvey
Ah, missed that little bit. In that case, I'm as baffled as you are.
JupiterP5
A: 

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

AllenG