4

So I have two different source files:

file1.cs:

namespace namespace1 {
    public partial class Class1 {
        public partial class NestedClass {

             public int myInt{ get; set; }

        }
    }    
}

file2.cs:

namespace namespace1.Class1 {
    public partial class NestedClass {

         void doSomething() {
             Console.WriteLine(this.myInt); // class does not contain definition for myInt
         }
    } 
}

The Problem:

I am trying to access a member variable declared in the first partial class in the second. No variables I declare can be viewed from the other file.

My Attempts at a Solution:

I found this post, but it did not seem to fix my issue. I declared several test variables in each of the partial class files and nothing was visible to the other file. I tried both public and private variables, with and without setters, since the problem in that situation involved a missing setter. I thought maybe my classes were named incorrectly, so I triple checked my namespaces as well as class names and that they were both declared partial. Finaly, I have tried restarting Visual Studio as well, to no avail.

Any help would be greatly appreciated!

Community
  • 1
  • 1
dak1220
  • 306
  • 4
  • 13
  • 7
    The **namespace is different**. – Willem Van Onsem May 16 '17 at 12:31
  • The first partial class exists as a nested class, thus I added to the namespace in file2.cs. – dak1220 May 16 '17 at 12:32
  • http://stackoverflow.com/questions/9143046/nested-partial-class check this solution – Annie May 16 '17 at 12:37
  • You should actually get compiler error on file2 as namespace1.Class1 is already declared in file1. – Bhuban Shrestha May 16 '17 at 12:40
  • @dak1220 Please don't edit your question after it is solved for the sole purpose of removing mistakes from the code you asked the question about. If you would like to provide a solution to your own question, post it as an answer. Editing the mistakes out of the original code defeats the purpose of this site and makes the question/answer useless for future readers. I've rolled back your edit to preserve the original code in order to help anyone else who may be having a similar problem. – Dan Bechard May 16 '17 at 13:04
  • @Dan I edited for clarification for the answer below (see comments). I should have added an edited section and left the original code as well. I will remember that for the future! – dak1220 May 16 '17 at 13:06
  • @dak1220 If you still think the edited-in code was useful, please do add it back to your question in an "updated code per Willem's request", or add it as a new answer if it is fully working and better than Willem's answer. The edited code is still available by clicking "edited xxx mins ago" on your question, should you require it. – Dan Bechard May 16 '17 at 13:19
  • 1
    @Dan I don't believe it to be necessary. All I did was edit it per William's answer below. I will leave the question and answer as-is. I think it will make the most since to future viewers the way it currently is. – dak1220 May 16 '17 at 13:25

1 Answers1

7

The problem is that in your first file, you have a namespace:

namespace namespace1 {

whereas in the second it is:

namespace namespace1.Class1 {

Since the namespaces are different, C# considers the classes to be different as well: they happen to have the same name, but since these are declared in separate namespaces, C# considers them to be different.

In the case you work with partial nested classes, you should write it like:

file1.cs:

namespace namespace1 {

    public partial class Class1 {

        public partial class NestedClass {

             public int myInt{ get; set; }

        }
    }
}

file2.cs:

using System;

namespace namespace1 {

    public partial class Class1 {

        public partial class NestedClass {

            void doSomething() {
                Console.WriteLine(this.myInt); // class does not contain definition for myInt
            }

        }

    }
}

So you do not specify the outer class as a namespace, but you declare the outer class twice.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • 1
    Now that the code works the comment that the definition for `myInt` doesn't exist is wrong ;-) – Bill Tür stands with Ukraine May 16 '17 at 12:38
  • I have edited the code in this way and Visual Studio still doesn't like it. I edited the namespace of the second file and made sure it was inside of the same class as in the first file, but I still seem to get the same error. Could this require some Visual Studio trickery? – dak1220 May 16 '17 at 12:43
  • @dak1220: usually not. Can you update your question with the code? – Willem Van Onsem May 16 '17 at 12:43
  • @WillemVanOnsem Edited! – dak1220 May 16 '17 at 12:45
  • @dak1220: I've compiled it and except for the `using System;` statement, I do not get any errors. – Willem Van Onsem May 16 '17 at 12:50
  • @WillemVanOnsem I just made a simple 2 source file project separate from the project that this problem is occurring in, and everything is working fine. I will mark this as the answer (since it is) and see if I can't figure out why it isn't working in the other project. Thanks for your help! – dak1220 May 16 '17 at 12:54