2

How to access to root namespace in debug while we are inside the namespace with duplicated name?

For example:

Root namespace: Project

Namespace which caught a breakpoint: Project.Models.Project (notice duplicated Project segment).

Code:

using Project.Models.Project;

namespace Project
{
    class Program
    {
        static void Main(string[] args)
        {
            new Foo().Bar();
        }
    }

    namespace Models
    {
        namespace Project
        {
            public class Foo
            {
                public void Bar()
                {
                     //breakpoint! in this moment I want to access to Bar class (there is no `using Project.Models.Issue;` above)
                     //var boo = new Boo(); Cannot resolve symbol 'Boo'
                }
            }
        }

        namespace Issue
        {
            public class Boo { }
        }
    }
}

Watch window:

watch window

There is access only to nested Project namespace (Project.Models.Project), but I'm not able to access to the root namespace with the same name and go to Project.Models.Issues.Boo.

Any ideas?

Pawel Maga
  • 5,428
  • 3
  • 38
  • 62

2 Answers2

2

You can do this:

var boo = new Models.Issue.Boo();

The compiler is slightly confused because of the multiple Project namespaces, but within that scope, you only need to qualify it starting with the Models namespace.

The other thing you could do to help the compiler discern between the different Project namespaces is utilize an alias to Project in your using directive:

using Project.Models.Project;
using p = Project;

Then this will work:

var boo = new p.Models.Issue.Boo();

Yet another perfectly good option is provided by Sefe's answer.

rory.ap
  • 34,009
  • 10
  • 83
  • 174
  • Not duplicated, but related http://stackoverflow.com/questions/125319/should-using-statements-be-inside-or-outside-the-namespace – Cleptus Feb 28 '17 at 12:18
  • btw, same problem of http://stackoverflow.com/questions/6297279/nested-namespaces – Cleptus Feb 28 '17 at 12:21
  • @rory.ap yeah I know how to fix that, but I was curious how to access to this namespace when I'm in debug and don't want to cancel it and modify code. Both solutions are great ;) – Pawel Maga Feb 28 '17 at 12:25
  • @Pawel Maga, I also test it in my side, without using the custom code, it is really hard to view or access the root namespace in debugging directly. So I think Sefe and rory.ap provided the correct answer for this issue. If possible, you could mark it as the answer. In addition, if you have good suggestion, you could submit the uservoice to the product team here: http://visualstudio.uservoice.com/forums/121579-visual-studio. – Jack Zhai Mar 02 '17 at 08:39
2

You can use the global namespace alias (global::) to indicate that you want to start from the root namespace:

var boo = new global::Project.Models.Issues.Boo()

As @rory.ap has correctly pointed out, in this case you can do without the global namespace alias, but sometimes it is your only choice. Consider this example:

namespace Foo {
    class Bar {}
    namespace Bat {
        namespace Foo {
            class Bar {}
        }
        static class Test {
            public static void AccessNS() {
                //The global:: alias is necessary here to avoid a creating a Foo.Bat.Foo.Bar()
                var fooBar = new global::Foo.Bar();
            }
        }
     }
}
Sefe
  • 13,731
  • 5
  • 42
  • 55