Why can't global resource namespaces be used in an import namespace statement? RRS feed

  • Question

  • User1183903743 posted

    Custom namespaces for global resources within the Resources namespace is a completely undocumented but nice new feature:


    This is an invariant file that gets code-generated into a public class ResourceName inside the namespace Resources.XXX.YYY.ZZZ

    string s= Resources.XXX.YYY.ZZZ.ResourceName.ResKey1;

    Before RTM I had a thousand entries which all appeared at once in the intellisense popup and were hard to navigate. Now I can break them up into different resx files and incorporate the naming hierarchy in the file names rather than the resource key names. 

    For some quirky reason you cannot put a using statement at the top of the page:

    using Resources.XXX.YYY.ZZZ;

    string s= ResourceName.ResKey1;

    The above would be defeating the purpose of the namespaces but this wouldn't:

    using Resources.XXX.YYY;
    string s= ZZZ.ResourceName.ResKey1;

    I'd appreciate if anyone knows the reason.

    Wednesday, December 14, 2005 8:28 AM

All replies

  • User287763314 posted


    well, i think that is just the way C# works. According to the spec:

    "Create a using directive to use the types in a namespace without having to specify the namespace. A using directive does not give you access to any namespaces that may be nested in the namespace you specify."


    so, it looks like using gives you access to the types defined in a namespace but not to other nested namespaces .

    Wednesday, December 14, 2005 9:41 AM
  • User1183903743 posted

    Hi Luis,

    We meet again. It does work. The problem was the generated file is outside the project and the IDE cannot see it until you re-build.
    The namespace is NOT nested and that is exactly the issue.

    The namespce is "Resources.XXX.YYY.ZZZ" It just happens to contain dots. Resources is a poor choice as it can be easily confused with System.Resources but it is what it is.

    using Resources.XXX.YYY.ZZZ;
    string s= ResName.ResKey1;

    The other culprit is the IDE intellisense which piece-meals the dot-separated parts one by one as though they are nested namepaces.

    If I only have one file: "~/App_GlobalResources/XXX.YYY.ZZZ.ResName.resx."  I should have have only ONE generated namespace.
    So when I start typing using... I should get the next intellisense tip "XXX.YYY.ZZZ". and not just "XXX". Actually, to be exact I should not get a tip at all until I am finished typing the whole thing because it is all one piece. The IDE is too helpful, misleadingly so.

    Nesting the namespaces would have only meant a couple of extra lines of CodeDom, so I don't know why they didn't do it. It is still possible to do a complete hierarchy if you create files like:

    "~/App_GlobalResources/ResName1.resx." namepsace is : "Resources"
    "~/App_GlobalResources/XXX.ResName2.resx." namepsace is : "Resources.XXX"
    "~/App_GlobalResources/XXX.YYY.ResName3.resx."  namepsace is : "Resources.XXX.YYY"
    "~/App_GlobalResources/XXX.YYY.ZZZ.ResName4.resx." namepsace is : "Resources.XXX.YYY.ZZZ"

    Again they wouldn't be nested but it doesn't matter usage-wise, unless you were doing reflection and trying to step up the hierachy of containers. You still get the organizational benefits which is what matters. 

    Someone should really blog about this.


    Wednesday, December 14, 2005 12:15 PM
  • User287763314 posted

    hello again :)

    ah...well, i though it generated nested namespaces and din't really bothered to check it out. thanks for the feedback Rob!


    Thursday, December 15, 2005 7:15 AM