Tech Off Thread

14 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

C# namespace weirdness

Back to Forum: Tech Off
  • User profile image
    phreaks

    Weird thing just happened to me.

    I moved a folder between projects within a solution.

    Then I did a global Find & Replace to update the namespace(s) from root.project1.SomeFolder to root.project2.SomeFolder

    Now comes the weirdness:

    using root.project2;

    SomeFolder.Class  myObject = new SomeFolder.Class()

    When I try to new-up an instance of a class that lives within the SomeFolder namespace it throws namespace could not be found error.


    However if I alias the namespace, it works.

    using SomeFolder = root.project2.SomeFolder;

    wtf?



       
       

  • User profile image
    W3bbo

    You did perform a full rebuild the solution after performing the Find+Replace operation, right?

  • User profile image
    phreaks

    W3bbo wrote:
    You did perform a full rebuild the solution after performing the Find+Replace operation, right?


    Well, I tried. But it fails with the unresolved namespace issue.

    This is VS.NET 2005, BTW.

  • User profile image
    littleguru

    I think this is no VS bug... There must be something that you did wrong. Perhaps you did just forget to rename everything...

  • User profile image
    phreaks

    littleguru wrote:
    I think this is no VS bug... There must be something that you did wrong. Perhaps you did just forget to rename everything...


    I thought the same thing at first, but there is also other 'wirdness' going on with this folder.

    The folder contains classes that derive from DevExpress.XtraReports.UI .

    Along with the namespace alias issue, I now have 2 reports whose designers and/or resx files are now orphaned from it's parent partial classes.

  • User profile image
    Sven Groot

    C# can't shorthand a namespace like that unless it's the namespace you're currently in.

    So this won't work:
    using root.project2;

    class Foo {
       public void Bar() {
          SomeFolder.ClassName x;
       }
    }

    While this will work:
    namespace root.project2 {

      class Foo {
         public void Bar() {
            SomeFolder.ClassName x;
         }
      }

    }

  • User profile image
    jmacdonagh

    I know it's kind of late, but why didn't you just use VS.net 2005's refactoring? You could right click on the portion of the namespace you want changed, hit rename, and it does a smart rename across the project.

  • User profile image
    phreaks

    jmacdonagh wrote:
    I know it's kind of late, but why didn't you just use VS.net 2005's refactoring? You could right click on the portion of the namespace you want changed, hit rename, and it does a smart rename across the project.


    Doh!
    Now you tell me. That's pretty nifty, does it work well?

  • User profile image
    Sven Groot

    H4L0PR1CK wrote:
    Right, but shouldn't this work?

    using root.Project2.SomeFolder;

    namespace root.Project1 {
          
       class Foo {
           public void Bar() {
             //Instantiate Object of type root.Project2.SomeFolder.SomeClass
             SomeFolder.SomeClass myObject = new SomeFolder.SomeClass();
           }
       }
    }

    No, it shouldn't.

    If you are using a partial namespace name, it can only refer to a namespace that's a child of the namespace you're currently in. In the sample above, using "SomeFolder" is only legal if: 1) SomeFolder is a type name in the current namespace or any imported namespace, 2) SomeFolder is a top-level namespace name or 3) SomeFolder is a namespace that is directly below the current namespace. It does not work if SomeFolder is a namespace below an imported namespace.

    So you either need to put the full namespace in the "using" bit, and use just the classname in the code, fully qualify the name in the code or alias the full namespace and use the alias in the code.

    VB can do what you're attempting here, C# cannot.

    EDIT: Actually, in the code above you're doing "using root.Project2.SomeFolder" so you could just use the class name directly. I'll assume you meant "using root.Project2" like you did above.

  • User profile image
    phreaks

    Sven Groot wrote:
    C# can't shorthand a namespace like that unless it's the namespace you're currently in.

    So this won't work:
    using root.project2;

    class Foo {
       public void Bar() {
          SomeFolder.ClassName x;
       }
    }

    While this will work:
    namespace root.project2 {

      class Foo {
         public void Bar() {
            SomeFolder.ClassName x;
         }
      }

    }


    Right, but shouldn't this work?

    using root.Project2.SomeFolder;

    namespace root.Project1 {
          
       class Foo {
           public void Bar() {
             //Instantiate Object of type root.Project2.SomeFolder.SomeClass
             SomeFolder.SomeClass myObject = new SomeFolder.SomeClass();
           }
       }
    }


    It's always worked for me before, but now I have to either alias the namespace in my using directive or add a using clause for the parent namespace and reference my object that way.

     All I am asking is shouldn't these 2 statement be the same?
    Because in this particular case, only the aliased namespace reference works.

    using Company.FITALibrary.Reporting;
    using Reports = Company.FITALibrary.Reporting.Reports;


    Also, this doesn't work for me now in this project either:

     
    using Company.FITALibrary.Reporting;

    Then I should be able to say something like:
        Reporting.Reports.ReportA myReport ; but I can't do that either.



  • User profile image
    Sven Groot

    H4L0PR1CK wrote:
    Ok, thanks for your help.
    If VB can do it, why can't C#?

    I don't know, ask Anders Heijlsberg. Wink

  • User profile image
    jmacdonagh

    H4L0PR1CK wrote:
    
    jmacdonagh wrote: I know it's kind of late, but why didn't you just use VS.net 2005's refactoring? You could right click on the portion of the namespace you want changed, hit rename, and it does a smart rename across the project.


    Doh!
    Now you tell me. That's pretty nifty, does it work well?


    Works perfectly if your application builds perfectly. If your application is having compile errors it usually works just fine too.

  • User profile image
    littleguru

    Sven Groot wrote:
    
    H4L0PR1CK wrote: Ok, thanks for your help.
    If VB can do it, why can't C#?

    I don't know, ask Anders Heijlsberg.


    Hahaha Big Smile

  • User profile image
    phreaks

    Sven Groot wrote:
    
    H4L0PR1CK wrote: Right, but shouldn't this work?

    using root.Project2.SomeFolder;

    namespace root.Project1 {
          
       class Foo {
           public void Bar() {
             //Instantiate Object of type root.Project2.SomeFolder.SomeClass
             SomeFolder.SomeClass myObject = new SomeFolder.SomeClass();
           }
       }
    }

    No, it shouldn't.

    If you are using a partial namespace name, it can only refer to a namespace that's a child of the namespace you're currently in. In the sample above, using "SomeFolder" is only legal if: 1) SomeFolder is a type name in the current namespace or any imported namespace, 2) SomeFolder is a top-level namespace name or 3) SomeFolder is a namespace that is directly below the current namespace. It does not work if SomeFolder is a namespace below an imported namespace.

    So you either need to put the full namespace in the "using" bit, and use just the classname in the code, fully qualify the name in the code or alias the full namespace and use the alias in the code.

    VB can do what you're attempting here, C# cannot.

    EDIT: Actually, in the code above you're doing "using root.Project2.SomeFolder" so you could just use the class name directly. I'll assume you meant "using root.Project2" like you did above.


    Ok, thanks for your help.
    If VB can do it, why can't C#?

    Although I still don't understand what the differences are between:

    using Reports = Company.FITALibrary.Reporting.Reports;
    and
    using Company.FITALibrary.Reporting;

    Both should facilitate this line:
       
       Reports.ReportA myReport = new Reports.ReportA();

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.