[Help] Confusion about a NameSpace Name resolution issue...

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

    [Help] Confusion about a NameSpace Name resolution issue...

    I ran into something that I consider a little strange this weekend and I was
    wondering if any of the Gurus out there could explain it to me. Here is a
    sample piece of code that will not compile because of line 11.

    1: using System;
    2:
    3: namespace eyecatcher.Syst em
    4: {
    5: class CommandLinePars er
    6: {
    7: private string m_sCmdLine;
    8:
    9: public CommandLinePars er()
    10: {
    11: Parse(System.En vironment.Comma ndLine); <-- Error occurs
    here.
    12: }
    13:
    14: public CommandLinePars er(string sCmd)
    15: {
    16: Parse(sCmd);
    17: }
    18:
    19: private void Parse(string sCmd)
    20: {
    21: m_sCmdLine = sCmd;
    22: }
    23: }
    24: }

    However if I change line 11 to look like this:

    Parse(Environme nt.CommandLine) ;

    Everything works.

    I spent a large portion of this weekend looking through the language specs
    and I could find no reason as to why my code failed. As a matter of fact,
    it seemed more likely (according to the documentation) that my fix should
    actually be causing an error and my original code should be correct.

    Anyways, if anyone has any helpful insights I would greatly appreciate it.

    Thanks
    Shawn Anderson



  • Peter Koen

    #2
    Re: [Help] Confusion about a NameSpace Name resolution issue...

    "Shawn Anderson" <sanderson@ripp letech.com> wrote in
    news:OGaqmpCmDH A.1408@TK2MSFTN GP11.phx.gbl:
    [color=blue]
    > I ran into something that I consider a little strange this weekend and
    > I was wondering if any of the Gurus out there could explain it to me.
    > Here is a sample piece of code that will not compile because of line
    > 11.
    >
    > 1: using System;
    > 2:
    > 3: namespace eyecatcher.Syst em[/color]

    [...]
    [color=blue]
    > I spent a large portion of this weekend looking through the language
    > specs and I could find no reason as to why my code failed. As a
    > matter of fact, it seemed more likely (according to the documentation)
    > that my fix should actually be causing an error and my original code
    > should be correct.
    >
    > Anyways, if anyone has any helpful insights I would greatly appreciate
    > it.
    >
    > Thanks
    > Shawn Anderson
    >
    >[/color]

    Hi,

    That's because of your namespace. the compiler thinks that
    System.Environm ent.CommandLine is part of your eyecatcher.Syst em and
    starts complaining about the wrong namespace name.

    that's due to an optimization, the compiler isn't always searching for
    the whole name. in this case due to your namespace name he get's
    confused.

    if you omit System then the compiler will first look in eyecatcher.Syst em
    if he can find the type Environment. if he doesn't he starts looking in
    the namespaces included by using.

    when it's absolutely necessary to use namespacenames that potentially
    produce compiler errors you can clear things up with an alias:

    using Env = System.Environm ent;

    --
    best regards

    Peter Koen
    -----------------------------------
    MCAD, CAI/R, CAI/S, CASE/RS, CAT/RS

    Comment

    • Shawn Anderson

      #3
      Re: [Help] Confusion about a NameSpace Name resolution issue...

      Thanks, you just confirmed what I was thinking -- that 'using' might be the
      way to go in this case.

      Thanks again,
      Shawn

      "Peter Koen" <koen-newsreply&snusn u.at> wrote in message
      news:e4yRmWDmDH A.988@TK2MSFTNG P10.phx.gbl...[color=blue]
      > "Shawn Anderson" <sanderson@ripp letech.com> wrote in
      > news:OGaqmpCmDH A.1408@TK2MSFTN GP11.phx.gbl:
      >[color=green]
      > > I ran into something that I consider a little strange this weekend and
      > > I was wondering if any of the Gurus out there could explain it to me.
      > > Here is a sample piece of code that will not compile because of line
      > > 11.
      > >
      > > 1: using System;
      > > 2:
      > > 3: namespace eyecatcher.Syst em[/color]
      >
      > [...]
      >[color=green]
      > > I spent a large portion of this weekend looking through the language
      > > specs and I could find no reason as to why my code failed. As a
      > > matter of fact, it seemed more likely (according to the documentation)
      > > that my fix should actually be causing an error and my original code
      > > should be correct.
      > >
      > > Anyways, if anyone has any helpful insights I would greatly appreciate
      > > it.
      > >
      > > Thanks
      > > Shawn Anderson
      > >
      > >[/color]
      >
      > Hi,
      >
      > That's because of your namespace. the compiler thinks that
      > System.Environm ent.CommandLine is part of your eyecatcher.Syst em and
      > starts complaining about the wrong namespace name.
      >
      > that's due to an optimization, the compiler isn't always searching for
      > the whole name. in this case due to your namespace name he get's
      > confused.
      >
      > if you omit System then the compiler will first look in eyecatcher.Syst em
      > if he can find the type Environment. if he doesn't he starts looking in
      > the namespaces included by using.
      >
      > when it's absolutely necessary to use namespacenames that potentially
      > produce compiler errors you can clear things up with an alias:
      >
      > using Env = System.Environm ent;
      >
      > --
      > best regards
      >
      > Peter Koen
      > -----------------------------------
      > MCAD, CAI/R, CAI/S, CASE/RS, CAT/RS
      > http://www.kema.at[/color]


      Comment

      Working...