none
namespace collision problem RRS feed

  • Question

  • Hi,

    I've a problem of namespace collision, it happens when there is something like this:

    namespace A.B
    {
        public class Test1
        {
            public A.B.A.C.Test2 test2;
        }
    }
    
    namespace A.B.A.C
    {
        public class Test2
        {
           
        }
    
    }

    The reference A.B.A.C.Test2 test2 is invalid as the first A is evaluated as the second A by the compiler, why is this happening and how can I resolve it ?

    --------

    Thanks,

    Hussein


    Monday, February 4, 2013 9:05 PM

Answers

  • Since both namespaces are declared in the same assembly, A.C is merged into the existing A.B

    You can create an instance of Test2 using:

    namespace A.B
    {
        public class Test1
        {
            public A.C.Test2 test;
        }
    }
    
    namespace A.B.A.C
    {
        class Test2
        {
    
        }
    
    }

    To avoid this merge, declare the two namespaces in seperate assemblies.  What you have now is the same as:

    namespace X
    {
        namespace Y
        {
            class Test3
            { }
            namespace X
            {
                namespace z
                {
                    class Test4
                    { }
                }
            }
        }
    }


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Hussein Nomier Monday, February 4, 2013 11:00 PM
    Monday, February 4, 2013 9:17 PM
    Moderator

All replies

  • Since both namespaces are declared in the same assembly, A.C is merged into the existing A.B

    You can create an instance of Test2 using:

    namespace A.B
    {
        public class Test1
        {
            public A.C.Test2 test;
        }
    }
    
    namespace A.B.A.C
    {
        class Test2
        {
    
        }
    
    }

    To avoid this merge, declare the two namespaces in seperate assemblies.  What you have now is the same as:

    namespace X
    {
        namespace Y
        {
            class Test3
            { }
            namespace X
            {
                namespace z
                {
                    class Test4
                    { }
                }
            }
        }
    }


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Hussein Nomier Monday, February 4, 2013 11:00 PM
    Monday, February 4, 2013 9:17 PM
    Moderator
  • Ok I understand this a little bit now but if it's that case:

    namespace A.B.D
    {
        public class Test1
        {
            public A.B.A.C.Test2 test2;
        }
    }
    
    namespace A.B.A.C
    {
        public class Test2
        {
           
        }
    
    }

    Does that mean the compiler will create a namespace A.B and add inner namespaces D and A.C to it ?

    • Marked as answer by Hussein Nomier Monday, February 4, 2013 10:59 PM
    • Unmarked as answer by Hussein Nomier Monday, February 4, 2013 11:00 PM
    Monday, February 4, 2013 10:13 PM
  • Yes, you would have:

    A.B.A.C.Test2 test;
    A.B.D.Test1 othertest;
    

    So again, you would only need A.C.Test2 within the Test1 class since they are both in namespace A.B

    namespace A.B.D
    {
        public class Test1
        {
            public A.C.Test2 test2;
        }
    }
    
    namespace A.B.A.C
    {
        public class Test2
        {
    
        }
    
    }


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, February 4, 2013 10:22 PM
    Moderator
  • Great, thanks a lot for helping.
    Monday, February 4, 2013 10:33 PM
  • Sure.  If your issue is resolved, please close the thread by marking one or more appropriate posts as answer.

    Thanks!


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, February 4, 2013 10:39 PM
    Moderator