question on namespaces

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Aniket Sule

    question on namespaces

    Hi,
    I am trying to use a class from one namespace in another via the using
    directive. The 2 namespaces have some part of the name in common (A.B.x,
    A.B.y). However in the second namespace, I am unable to access the class
    from the first without qualifying it, which is what i want to avoid. Any
    ideas what I am doing wrong?

    I am attaching the code and compiler error below to show exactly what I am
    trying to do,

    Any help is greatly appreciated,
    Thanks
    Aniket

    file1.cs -
    using System;
    namespace apstry.A.apstry 1
    {
    public class apstry1
    {
    public apstry1()
    {
    }
    public static void Dummy()
    {
    System.Windows. Forms.MessageBo x.Show("dummy call");
    }
    }
    public class noName
    {
    public noName()
    {
    }
    }
    }

    file2.cs -
    using System;
    using apstry.A.apstry 1;
    namespace apstry.A.apstry 2
    {
    public class Class11
    {
    public Class11()
    {
    System.Windows. Forms.MessageBo x.Show("duh");
    apstry1.apstry1 .Dummy(); // -- this works
    apstry1.Dummy() ;

    /*--gives the error C:\temp\apstry2 \apstry2\Class1 .cs(22): The type or
    namespace name 'Dummy' does not exist in the class or namespace
    'apstry.A.apstr y1' (are you missing an assembly reference?)*/

    noName y;
    noName z;

    }
    public void duh()
    {
    }
    }
    }


  • Mark Johnson

    #2
    Re: question on namespaces

    The name of the class and the end of the namespace are the same so the
    compiler doesn't know which one you want without prefixing the name of the
    class with the last part of the namespace.

    "Aniket Sule" <asule@meridium .com> wrote in message
    news:u6GPBjAbDH A.1748@TK2MSFTN GP12.phx.gbl...[color=blue]
    > Hi,
    > I am trying to use a class from one namespace in another via the using
    > directive. The 2 namespaces have some part of the name in common (A.B.x,
    > A.B.y). However in the second namespace, I am unable to access the class
    > from the first without qualifying it, which is what i want to avoid. Any
    > ideas what I am doing wrong?
    >
    > I am attaching the code and compiler error below to show exactly what I am
    > trying to do,
    >
    > Any help is greatly appreciated,
    > Thanks
    > Aniket
    >
    > file1.cs -
    > using System;
    > namespace apstry.A.apstry 1
    > {
    > public class apstry1
    > {
    > public apstry1()
    > {
    > }
    > public static void Dummy()
    > {
    > System.Windows. Forms.MessageBo x.Show("dummy call");
    > }
    > }
    > public class noName
    > {
    > public noName()
    > {
    > }
    > }
    > }
    >
    > file2.cs -
    > using System;
    > using apstry.A.apstry 1;
    > namespace apstry.A.apstry 2
    > {
    > public class Class11
    > {
    > public Class11()
    > {
    > System.Windows. Forms.MessageBo x.Show("duh");
    > apstry1.apstry1 .Dummy(); // -- this works
    > apstry1.Dummy() ;
    >
    > /*--gives the error C:\temp\apstry2 \apstry2\Class1 .cs(22): The type or
    > namespace name 'Dummy' does not exist in the class or namespace
    > 'apstry.A.apstr y1' (are you missing an assembly reference?)*/
    >
    > noName y;
    > noName z;
    >
    > }
    > public void duh()
    > {
    > }
    > }
    > }
    >
    >[/color]


    Comment

    • Fergus Cooney

      #3
      Re: question on namespaces

      public void duh()

      Lol, I like that one.

      Fergus


      Comment

      • Goran Genter

        #4
        Re: question on namespaces

        "Aniket Sule" <asule@meridium .com> wrote in message
        news:u6GPBjAbDH A.1748@TK2MSFTN GP12.phx.gbl[color=blue]
        >[/color]

        I think that the problem is in :

        namespace apstry.A.apstry 1
        {
        public class apstry1
        {
        ..
        ..
        ..

        You have a namespace and class with same name : apstry1.


        Comment

        • Aniket Sule

          #5
          Re: question on namespaces

          Thanks a lot, that explains it. I dont have control over the class naming
          right now, so looks like I will have to go with the qualified name

          "Joe" <joe@nospam.com > wrote in message
          news:e9HYxuAbDH A.2572@TK2MSFTN GP12.phx.gbl...[color=blue]
          > Hi Aniket,
          >
          > apstry1.Dummy() doesn't work because the compiler is resolving the name by
          > following the highest level namespace down through nested namespaces. It
          > first finds the namespace apstry.A.apstry 1 and then looks for Dummy. The
          > only class next in the hierarchy is aspstry1, but Dummy is a member of the
          > aspstry1 class and not at the same level, so the compiler doesn't find it
          > where you said it should be.
          >
          > The best way to avoid this problem is by not naming a class the same as[/color]
          any[color=blue]
          > of the namespaces in its containing hierarchy. The C# style guidelines[/color]
          (in[color=blue]
          > the SDK docs) advise against this practice also.
          >
          > Joe
          > --
          > http://www.csharp-station.com
          >
          >
          > "Aniket Sule" <asule@meridium .com> wrote in message
          > news:u6GPBjAbDH A.1748@TK2MSFTN GP12.phx.gbl...[color=green]
          > > Hi,
          > > I am trying to use a class from one namespace in another via the using
          > > directive. The 2 namespaces have some part of the name in common (A.B.x,
          > > A.B.y). However in the second namespace, I am unable to access the class
          > > from the first without qualifying it, which is what i want to avoid. Any
          > > ideas what I am doing wrong?
          > >
          > > I am attaching the code and compiler error below to show exactly what I[/color][/color]
          am[color=blue][color=green]
          > > trying to do,
          > >
          > > Any help is greatly appreciated,
          > > Thanks
          > > Aniket
          > >
          > > file1.cs -
          > > using System;
          > > namespace apstry.A.apstry 1
          > > {
          > > public class apstry1
          > > {
          > > public apstry1()
          > > {
          > > }
          > > public static void Dummy()
          > > {
          > > System.Windows. Forms.MessageBo x.Show("dummy call");
          > > }
          > > }
          > > public class noName
          > > {
          > > public noName()
          > > {
          > > }
          > > }
          > > }
          > >
          > > file2.cs -
          > > using System;
          > > using apstry.A.apstry 1;
          > > namespace apstry.A.apstry 2
          > > {
          > > public class Class11
          > > {
          > > public Class11()
          > > {
          > > System.Windows. Forms.MessageBo x.Show("duh");
          > > apstry1.apstry1 .Dummy(); // -- this works
          > > apstry1.Dummy() ;
          > >
          > > /*--gives the error C:\temp\apstry2 \apstry2\Class1 .cs(22): The type[/color][/color]
          or[color=blue][color=green]
          > > namespace name 'Dummy' does not exist in the class or namespace
          > > 'apstry.A.apstr y1' (are you missing an assembly reference?)*/
          > >
          > > noName y;
          > > noName z;
          > >
          > > }
          > > public void duh()
          > > {
          > > }
          > > }
          > > }
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Tyler Dixon

            #6
            Re: question on namespaces

            It's because your first class has the same name as the enclosing namespace.
            The compiler thinks you are trying to call

            apstry.A.apstry 1.Dummy()

            which of course doesn't work because namespaces cannot contain methods
            directly. Just change the name of the first class to something else, and
            the compiler won't get confused.

            "Aniket Sule" <asule@meridium .com> wrote in message
            news:u6GPBjAbDH A.1748@TK2MSFTN GP12.phx.gbl...[color=blue]
            > Hi,
            > I am trying to use a class from one namespace in another via the using
            > directive. The 2 namespaces have some part of the name in common (A.B.x,
            > A.B.y). However in the second namespace, I am unable to access the class
            > from the first without qualifying it, which is what i want to avoid. Any
            > ideas what I am doing wrong?
            >
            > I am attaching the code and compiler error below to show exactly what I am
            > trying to do,
            >
            > Any help is greatly appreciated,
            > Thanks
            > Aniket
            >
            > file1.cs -
            > using System;
            > namespace apstry.A.apstry 1
            > {
            > public class apstry1
            > {
            > public apstry1()
            > {
            > }
            > public static void Dummy()
            > {
            > System.Windows. Forms.MessageBo x.Show("dummy call");
            > }
            > }
            > public class noName
            > {
            > public noName()
            > {
            > }
            > }
            > }
            >
            > file2.cs -
            > using System;
            > using apstry.A.apstry 1;
            > namespace apstry.A.apstry 2
            > {
            > public class Class11
            > {
            > public Class11()
            > {
            > System.Windows. Forms.MessageBo x.Show("duh");
            > apstry1.apstry1 .Dummy(); // -- this works
            > apstry1.Dummy() ;
            >
            > /*--gives the error C:\temp\apstry2 \apstry2\Class1 .cs(22): The type or
            > namespace name 'Dummy' does not exist in the class or namespace
            > 'apstry.A.apstr y1' (are you missing an assembly reference?)*/
            >
            > noName y;
            > noName z;
            >
            > }
            > public void duh()
            > {
            > }
            > }
            > }
            >
            >[/color]


            Comment

            • 100

              #7
              Re: question on namespaces

              Hi Aniket,
              If you don't have control over the name of the class. You might be able to
              change the name you are using only in file2. You can do that using aliases

              file2:

              using System;
              using apstry.A.apstry 1;
              using NewName = apstry.A.apstry 1.apstry1; //a new name for the class apstry1

              and then call Dummy like:
              NewName.Dummy() ;

              HTH
              B\rgds
              100
              "Aniket Sule" <asule@meridium .com> wrote in message
              news:%23YXsd4Ab DHA.2668@TK2MSF TNGP09.phx.gbl. ..[color=blue]
              > Thanks a lot, that explains it. I dont have control over the class naming
              > right now, so looks like I will have to go with the qualified name
              >
              > "Joe" <joe@nospam.com > wrote in message
              > news:e9HYxuAbDH A.2572@TK2MSFTN GP12.phx.gbl...[color=green]
              > > Hi Aniket,
              > >
              > > apstry1.Dummy() doesn't work because the compiler is resolving the name[/color][/color]
              by[color=blue][color=green]
              > > following the highest level namespace down through nested namespaces.[/color][/color]
              It[color=blue][color=green]
              > > first finds the namespace apstry.A.apstry 1 and then looks for Dummy.[/color][/color]
              The[color=blue][color=green]
              > > only class next in the hierarchy is aspstry1, but Dummy is a member of[/color][/color]
              the[color=blue][color=green]
              > > aspstry1 class and not at the same level, so the compiler doesn't find[/color][/color]
              it[color=blue][color=green]
              > > where you said it should be.
              > >
              > > The best way to avoid this problem is by not naming a class the same as[/color]
              > any[color=green]
              > > of the namespaces in its containing hierarchy. The C# style guidelines[/color]
              > (in[color=green]
              > > the SDK docs) advise against this practice also.
              > >
              > > Joe
              > > --
              > > http://www.csharp-station.com
              > >
              > >
              > > "Aniket Sule" <asule@meridium .com> wrote in message
              > > news:u6GPBjAbDH A.1748@TK2MSFTN GP12.phx.gbl...[color=darkred]
              > > > Hi,
              > > > I am trying to use a class from one namespace in another via the using
              > > > directive. The 2 namespaces have some part of the name in common[/color][/color][/color]
              (A.B.x,[color=blue][color=green][color=darkred]
              > > > A.B.y). However in the second namespace, I am unable to access the[/color][/color][/color]
              class[color=blue][color=green][color=darkred]
              > > > from the first without qualifying it, which is what i want to avoid.[/color][/color][/color]
              Any[color=blue][color=green][color=darkred]
              > > > ideas what I am doing wrong?
              > > >
              > > > I am attaching the code and compiler error below to show exactly what[/color][/color][/color]
              I[color=blue]
              > am[color=green][color=darkred]
              > > > trying to do,
              > > >
              > > > Any help is greatly appreciated,
              > > > Thanks
              > > > Aniket
              > > >
              > > > file1.cs -
              > > > using System;
              > > > namespace apstry.A.apstry 1
              > > > {
              > > > public class apstry1
              > > > {
              > > > public apstry1()
              > > > {
              > > > }
              > > > public static void Dummy()
              > > > {
              > > > System.Windows. Forms.MessageBo x.Show("dummy call");
              > > > }
              > > > }
              > > > public class noName
              > > > {
              > > > public noName()
              > > > {
              > > > }
              > > > }
              > > > }
              > > >
              > > > file2.cs -
              > > > using System;
              > > > using apstry.A.apstry 1;
              > > > namespace apstry.A.apstry 2
              > > > {
              > > > public class Class11
              > > > {
              > > > public Class11()
              > > > {
              > > > System.Windows. Forms.MessageBo x.Show("duh");
              > > > apstry1.apstry1 .Dummy(); // -- this works
              > > > apstry1.Dummy() ;
              > > >
              > > > /*--gives the error C:\temp\apstry2 \apstry2\Class1 .cs(22): The[/color][/color][/color]
              type[color=blue]
              > or[color=green][color=darkred]
              > > > namespace name 'Dummy' does not exist in the class or[/color][/color][/color]
              namespace[color=blue][color=green][color=darkred]
              > > > 'apstry.A.apstr y1' (are you missing an assembly reference?)*/
              > > >
              > > > noName y;
              > > > noName z;
              > > >
              > > > }
              > > > public void duh()
              > > > {
              > > > }
              > > > }
              > > > }
              > > >
              > > >[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Val Savvateev

                #8
                Re: question on namespaces

                I'll agree with Joe that this the problem occurs because of the way name
                resolutions is done in C#. However it's not that simple - for instance try
                to move [using apstry.A.apstry 1;] directive _inside_ apstry2 namespace and
                see what happens.
                If you find it entertaining you could go through sections 3.7, 3.8 and 9 of
                the language cpecs, but I'd suggest not to bother and just avoid name
                collisions.

                Regards,
                Val.


                "Aniket Sule" <asule@meridium .com> wrote in message
                news:u6GPBjAbDH A.1748@TK2MSFTN GP12.phx.gbl...[color=blue]
                > Hi,
                > I am trying to use a class from one namespace in another via the using
                > directive. The 2 namespaces have some part of the name in common (A.B.x,
                > A.B.y). However in the second namespace, I am unable to access the class
                > from the first without qualifying it, which is what i want to avoid. Any
                > ideas what I am doing wrong?
                >
                > I am attaching the code and compiler error below to show exactly what I am
                > trying to do,
                >
                > Any help is greatly appreciated,
                > Thanks
                > Aniket
                >
                > file1.cs -
                > using System;
                > namespace apstry.A.apstry 1
                > {
                > public class apstry1
                > {
                > public apstry1()
                > {
                > }
                > public static void Dummy()
                > {
                > System.Windows. Forms.MessageBo x.Show("dummy call");
                > }
                > }
                > public class noName
                > {
                > public noName()
                > {
                > }
                > }
                > }
                >
                > file2.cs -
                > using System;
                > using apstry.A.apstry 1;
                > namespace apstry.A.apstry 2
                > {
                > public class Class11
                > {
                > public Class11()
                > {
                > System.Windows. Forms.MessageBo x.Show("duh");
                > apstry1.apstry1 .Dummy(); // -- this works
                > apstry1.Dummy() ;
                >
                > /*--gives the error C:\temp\apstry2 \apstry2\Class1 .cs(22): The type or
                > namespace name 'Dummy' does not exist in the class or namespace
                > 'apstry.A.apstr y1' (are you missing an assembly reference?)*/
                >
                > noName y;
                > noName z;
                >
                > }
                > public void duh()
                > {
                > }
                > }
                > }
                >
                >[/color]


                Comment

                Working...