views:

1845

answers:

6

I am using partial classes to split some functionality between 2 files, but I am getting an error. What am I doing wrong?

A1.cs:

private partial class A
{
    private string SomeProperty { get { return "SomeGeneratedString"; } }       
}

A2.cs:

private partial class A
{
    void SomeFunction()
    {
        //trying to access this.SomeProperty produces the following compiler error, at least with C# 2.0
        //error CS0117: 'A' does not contain a definition for 'SomeProperty'
    }
}
+8  A: 

Are the two partial classes in the same namespace? That could be an explanation.

Sklivvz
+4  A: 

different namespace?

Matt Brunell
+1  A: 

I think it's because you're declaring your class as "private". Try changing the modifier to "internal" so that the two "halves" of the class can "see" each other within the same assembly.

Matt Hamilton
According to the MSDN documentation, private is fine for partial classes, as long as both are private. (The reason I know this is that I thought the same thing, since I've only ever seen public partial classes.. So I looked it up).http://msdn.microsoft.com/en-us/library/wa80x488.aspx
Troy Howard
Weird. I knocked up a little console application in Visual Studio before posting this and pasted in the OP's exact code, and it won't compile. Change the modifier to "internal" and it compiles fine.
Matt Hamilton
Woudln't be the first time MSDN was wrong... ;) But that's probably too simple of an explanation. Certainly worth digging into to find the definitive answer.
Troy Howard
OK, turns out private/protected classes only work if they are nested classes. The "Elements defined in a namespace..." error is due to that. This doesn't explain OP's probably of not being to access the property. It seems obvious that if he got that far, then these are nested classes.
Troy Howard
+1  A: 

The error I get is:

Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal

I'm guessing it's a namespace issue as previously stated.

John Kraft
Yeah, that's the error I see too.
Matt Hamilton
OK, turns out private/protected classes only work if they are nested classes. The "Elements defined in a namespace..." error is due to that. This doesn't explain OP's probably of not being to access the property. It seems obvious that if he got that far, then these are nested classes.
Troy Howard
+3  A: 

At first, I was unable to reproduce your error.

When these partial classes are defined alone, inside a namespace, the private keyword causes the build to fail with "Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal"...

If I keep them private and nest them within another class, everything works fine.

I can reproduce your error only when, in one file, I have part of the class nested inside another class, and in another file, I do NOT nest the class, and then remove the private keyword... like this:

Class1.cs:

namespace stackoverflow.answers
{
    public class Foo
    {
        private partial class Bar
        {
            private string SomeProperty { get { return "SomeGeneratedString"; } }
        }
    }
}

Class2.cs:

namespace stackoverflow.answers
{
    partial class Bar
    {
        void SomeFunction()
        {
            string bar = this.SomeProperty;
        }
    }    
}

I also get the error you described if the namespaces differ.

Please post the entire code for the solution, because the provided code is invalid C# syntax, and can't be looked into without more context.

Troy Howard
A: 

I analyze your code. you declared partial class as nested class this is cause to show error. why bcz partial class will not declare as nested class the parial keyword is split into ultiple files so, every file nae is same when you declared in nested class it will not recognize.

sada