C# - Namespaces - accessing of sub-namespaces

Consider the following code snippet, in a file:
namespace NamespaceA.NamespaceB.NamespaceC
{
public class
ClassC
{
public static string
StringC
{
get { return "string c"
; }
}
}
}

Now, I want to be able to access the property from within another file. The following works -

using NamespaceA.NamespaceB;

namespace
Tester
{
public class
TesterClass
{
public string
StringC()
{
string
myValue;
myValue = NamespaceA.NamespaceB.NamespaceC.
ClassC
.StringC;
return
myValue;
}
}
}

However, I don't want to have to explicitly use NamespaceA.NamespaceB when trying to access my NamespaceC class. Given that I've included a using statement at the top of the file, using NamespaceA.NamespaceB;, why can't I just use the following line of code

myValue = NamespaceC.ClassC.StringC;

I vaguely remember having something like this working in the past, but I'd like to know what setting/configuration changes are necessary to allow it to work.

Just to clarify, this is in a web project, and these files are in the App_Code directory, but if they are not in that directory (eg. the first file is in App_Code, but the second class isn't), does it make a difference .




Answer this question

C# - Namespaces - accessing of sub-namespaces

  • Tami

    I think I've stumbled across the answer -- please correct me if you  find another way to achieve this end.

    The only way that StringC can be accessed using NamespaceC.ClassC.StringC is if the namespace in which the code resides is NamespaceA.NamespaceB. Note the updated code:

     

    namespace NamespaceA.NamespaceB
    {
       public class TesterClass
       {
          public string StringC()
          {
             string myValue;
             myValue = NamespaceC.ClassC.StringC;
             return myValue;
          }
       }
    }


  • dboone

    That's really good. Thank you.

    What I really wanted to do was access NamespaceC, and using the method that you've so kindly pointed out - I'm able to:


    using NamespaceC=NamespaceA.NamespaceB.NamespaceC;

    namespace Tester
    {
    public class TesterClass
    {
    public string StringC()
    {
    string myValue;
    myValue = NamespaceC.ClassC.StringC;
    return myValue;
    }
    }
    }

    Ie. - it works with namespaces (in the using statement), as well as classes!

    Thanks again --- very helpful!



  • DanWoerner

    Yes - In my application, I've used namespaces quite liberally to break up functionality. So I might have:

    CompanyName.ApplicationName.Bus (and then a class, Constants)

    and

    CompanyName.ApplicationName.Web (and then a class, Constants)

    So to avoid ambiguity, I can create an alias as such as:

    using BusConstants=CompanyName.ApplicationName.Bus.Constants;

    using WebConstants=CompanyName.ApplicationName.Bus.Constants;

    Then, in code, I can either choose the business constants, or the web constants using the aliases created BusConstants.Value1, or WebConstants.Value2.

    It also saves having the complete reference in the namespace, as you mention, ie. it's no longer necessary to use:

    CompanyName.ApplicationName.Bus.Constants.Value1

    Once again, thank you.



  • Bermychild

    Try to specify you using:


    using ClassC = NamespaceA.NamespaceB.NamespaceC.ClassC;

    using NamespaceA.NamespaceB;

    namespace Tester
    {
    public class TesterClass
    {
    public string StringC()
    {
    string myValue;
    myValue = ClassC.StringC;
    return myValue;
    }
    }
    }




  • bholemaharaj

    I just tried the following method in the first file, and it didn't seem to work either (Error 2 The name 'NamespaceC' does not exist in the current context C:\----\Common.cs 18 14 http://localhost/webSite/):

    namespace NamespaceA.NamespaceB
    {
    namespace
    NamespaceC
    {
    public class
    ClassC
    {
    public static string
    StringC
    {
    get { return "string c"
    ; }
    }
    }
    }
    }




  • Christian Kleinerman - MS

    It can become very handy when two classes with the same name, lets say ClassA, are in different namespaces wish you both use. Then every time you want to use ClassA your compiler gone scream that he don't know wish one to use.

    You can specify the fullnamespace and class name to fix this, but specify the class usage in you using section at the top of your class is cleaner i think.


  • C# - Namespaces - accessing of sub-namespaces