Namespaces being stupid

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

    #1

    Namespaces being stupid

    I've made some controls that have namespaces and I have trouble with
    the "using namespace" line, it doesn't do anything. Here's what's
    going on:

    1). I have a class like this:

    namespace N1.N2
    {
    class A{};
    }

    2). I reference the project with N1.N2 from another project's file
    where I want to use my class I have:

    using N1;
    using N1.N2;

    namespace N3
    {
    // Inside the code somewhere ...
    N2.A test = new N2.A(); // This line does not work
    N1.N2.A test = new N1.N2.A(); // This DOES work

    Why can't I make use of the "using namespace" line to shorten my code?
    I don't want to have to write out all the N1.N2.xxxx stuff, it's
    annoying and makes my code look messy. There are not naming conflicts
    with other namespaces.

    Help please!

    Thanks!
  • Joe Mayo

    #2
    Re: Namespaces being stupid

    "Justin" <jhtan@stanford .edu> wrote in message
    news:67095fce.0 310051620.1c85e d2f@posting.goo gle.com...[color=blue]
    > I've made some controls that have namespaces and I have trouble with
    > the "using namespace" line, it doesn't do anything. Here's what's
    > going on:
    >
    > 1). I have a class like this:
    >
    > namespace N1.N2
    > {
    > class A{};
    > }
    >
    > 2). I reference the project with N1.N2 from another project's file
    > where I want to use my class I have:
    >
    > using N1;
    > using N1.N2;
    >
    > namespace N3
    > {
    > // Inside the code somewhere ...
    > N2.A test = new N2.A(); // This line does not work
    > N1.N2.A test = new N1.N2.A(); // This DOES work
    >
    > Why can't I make use of the "using namespace" line to shorten my code?
    > I don't want to have to write out all the N1.N2.xxxx stuff, it's
    > annoying and makes my code look messy. There are not naming conflicts
    > with other namespaces.[/color]

    Hi Justin,

    A using statement only allows you to reference types, not nested namespaces.
    The dot operator in the N1.N2 namespace declaration makes N2 a nested
    namespace. If you want a shortcut, you could try an alias:

    using aliasN2 = N1.N2;

    aliasN2.A test = new aliasN2.A();

    Joe
    --
    Learn C# with step-by-step tutorials and practical examples. Start your first program, explore lists and parsing, and understand how .NET runs your code.



    Comment

    • Joe Mayo

      #3
      Re: Namespaces being stupid

      "Justin" <jhtan@stanford .edu> wrote in message
      news:67095fce.0 310051620.1c85e d2f@posting.goo gle.com...[color=blue]
      > I've made some controls that have namespaces and I have trouble with
      > the "using namespace" line, it doesn't do anything. Here's what's
      > going on:
      >
      > 1). I have a class like this:
      >
      > namespace N1.N2
      > {
      > class A{};
      > }
      >
      > 2). I reference the project with N1.N2 from another project's file
      > where I want to use my class I have:
      >
      > using N1;
      > using N1.N2;
      >
      > namespace N3
      > {
      > // Inside the code somewhere ...
      > N2.A test = new N2.A(); // This line does not work
      > N1.N2.A test = new N1.N2.A(); // This DOES work
      >
      > Why can't I make use of the "using namespace" line to shorten my code?
      > I don't want to have to write out all the N1.N2.xxxx stuff, it's
      > annoying and makes my code look messy. There are not naming conflicts
      > with other namespaces.[/color]

      Hi Justin,

      A using statement only allows you to reference types, not nested namespaces.
      The dot operator in the N1.N2 namespace declaration makes N2 a nested
      namespace. If you want a shortcut, you could try an alias:

      using aliasN2 = N1.N2;

      aliasN2.A test = new aliasN2.A();

      Joe
      --
      Learn C# with step-by-step tutorials and practical examples. Start your first program, explore lists and parsing, and understand how .NET runs your code.



      Comment

      • Chris R. Timmons

        #4
        Re: Namespaces being stupid

        jhtan@stanford. edu (Justin) wrote in
        news:67095fce.0 310051620.1c85e d2f@posting.goo gle.com:
        [color=blue]
        > I've made some controls that have namespaces and I have trouble
        > with the "using namespace" line, it doesn't do anything. Here's
        > what's going on:
        >
        > 1). I have a class like this:
        >
        > namespace N1.N2
        > {
        > class A{};
        > }
        >
        > 2). I reference the project with N1.N2 from another project's
        > file where I want to use my class I have:
        >
        > using N1;
        > using N1.N2;
        >
        > namespace N3
        > {
        > // Inside the code somewhere ...
        > N2.A test = new N2.A(); // This line does not work
        > N1.N2.A test = new N1.N2.A(); // This DOES work
        >
        > Why can't I make use of the "using namespace" line to shorten my
        > code?
        > I don't want to have to write out all the N1.N2.xxxx stuff,
        > it's
        > annoying and makes my code look messy. There are not naming
        > conflicts with other namespaces.[/color]

        Justin,

        I don't think you need the "using N1" statement. All you should need
        is:

        using N1.N2;

        namespace N3
        {
        ... inside a class somewhere ...
        A test = new A();
        }

        "using" directives in C# are very simple when compared to languages
        like Java and Python. There are no wildcards and the namespace name
        has no dependency upon the folder the .cs file is stored in. If
        you're familiar with Delphi, C# "using" directives are very similar
        to Delphi's "uses" clause.

        The "dot" (.) in an individual namespace name has no special
        significance, other than to separate words in the namespace name.

        See the docs for more info and examples:

        http://msdn.microsoft.com/library/de...l=/library/en-
        us/csref/html/vclrfNamespaces .asp

        or

        http://tinyurl.com/ptsq


        Hope this helps.

        Chris.
        -------------
        C.R. Timmons Consulting, Inc.

        Comment

        • Chris R. Timmons

          #5
          Re: Namespaces being stupid

          jhtan@stanford. edu (Justin) wrote in
          news:67095fce.0 310051620.1c85e d2f@posting.goo gle.com:
          [color=blue]
          > I've made some controls that have namespaces and I have trouble
          > with the "using namespace" line, it doesn't do anything. Here's
          > what's going on:
          >
          > 1). I have a class like this:
          >
          > namespace N1.N2
          > {
          > class A{};
          > }
          >
          > 2). I reference the project with N1.N2 from another project's
          > file where I want to use my class I have:
          >
          > using N1;
          > using N1.N2;
          >
          > namespace N3
          > {
          > // Inside the code somewhere ...
          > N2.A test = new N2.A(); // This line does not work
          > N1.N2.A test = new N1.N2.A(); // This DOES work
          >
          > Why can't I make use of the "using namespace" line to shorten my
          > code?
          > I don't want to have to write out all the N1.N2.xxxx stuff,
          > it's
          > annoying and makes my code look messy. There are not naming
          > conflicts with other namespaces.[/color]

          Justin,

          I don't think you need the "using N1" statement. All you should need
          is:

          using N1.N2;

          namespace N3
          {
          ... inside a class somewhere ...
          A test = new A();
          }

          "using" directives in C# are very simple when compared to languages
          like Java and Python. There are no wildcards and the namespace name
          has no dependency upon the folder the .cs file is stored in. If
          you're familiar with Delphi, C# "using" directives are very similar
          to Delphi's "uses" clause.

          The "dot" (.) in an individual namespace name has no special
          significance, other than to separate words in the namespace name.

          See the docs for more info and examples:

          http://msdn.microsoft.com/library/de...l=/library/en-
          us/csref/html/vclrfNamespaces .asp

          or

          http://tinyurl.com/ptsq


          Hope this helps.

          Chris.
          -------------
          C.R. Timmons Consulting, Inc.

          Comment

          • Jekke, Just Jekke

            #6
            Re: Namespaces being stupid

            On Sun, 5 Oct 2003 18:59:37 -0600, "Joe Mayo"
            <jmayo@ddiieess ppaammeerrssddi iee.com> wrote:[color=blue]
            >
            >A using statement only allows you to reference types, not nested namespaces.
            >The dot operator in the N1.N2 namespace declaration makes N2 a nested
            >namespace. If you want a shortcut, you could try an alias:
            >
            >using aliasN2 = N1.N2;
            >
            >aliasN2.A test = new aliasN2.A();[/color]

            Actually, his problem is that the dot in .net namespaces is not really
            a nesting operator. It is not safe to assume that the N1.N2 namespace
            is inside of the N1 namespace.

            Comment

            • Joe Mayo

              #7
              Re: Namespaces being stupid

              "Jekke, Just Jekke" <jekke@REMOVEAL Linsidejoke.tvC APS> wrote in message
              news:6s34ov8r4c k0h67r3bnvtojj1 up7nhb565@4ax.c om...[color=blue]
              > On Sun, 5 Oct 2003 18:59:37 -0600, "Joe Mayo"
              > <jmayo@ddiieess ppaammeerrssddi iee.com> wrote:[color=green]
              > >
              > >A using statement only allows you to reference types, not nested[/color][/color]
              namespaces.[color=blue][color=green]
              > >The dot operator in the N1.N2 namespace declaration makes N2 a nested
              > >namespace. If you want a shortcut, you could try an alias:
              > >
              > >using aliasN2 = N1.N2;
              > >
              > >aliasN2.A test = new aliasN2.A();[/color]
              >
              > Actually, his problem is that the dot in .net namespaces is not really
              > a nesting operator. It is not safe to assume that the N1.N2 namespace
              > is inside of the N1 namespace.[/color]

              That is not what I said. N2 is nested in N1. Looking at the original
              poster's code:

              namespace N1.N2
              {
              class A{};
              }

              is semantically equivalent to:

              namespace N1
              {
              namespace N2
              {
              class A{};
              }
              }

              according to the C# Specification:

              Hi. I'm Jon Jagger, director of software at Kosli. I built cyber-dojo, the place teams practice programming.


              Joe
              --
              Learn C# with step-by-step tutorials and practical examples. Start your first program, explore lists and parsing, and understand how .NET runs your code.



              Comment

              Working...