Question

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'
    }
}
Was it helpful?

Solution

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

OTHER TIPS

Same answer as @Andrey K but in simple terms

Set the build action of all your partial classes to 'Compile' using the 'Properties' windows of each of those files

Properties window - loading= Build action property">

different namespace?

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.

Edit:

solution: build action -> Complile, nothing else

I'll try to be more specific:

I had a class shared among 3 partial classes in 3 different files. At one moment a function call (from one part, of a function declared in other part) started showing error "does not exist in current context". It took me long until some guy helped me to figure out that accidentally i set the build action of the file with one part to "EmbeddedResourse" instead of "Compile".

To switch build action you should right click on file in solution explorer, then choose properties. Then change the build action.

This question is old, and my answer is not exactly helpful in this very case. But it may be helpful in one other very rare case related to partial classes. Sorry if my English is not clear.

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.

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.

All the files should be in the same folder.

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.

A corner case where additional help can save you time is if you are using a partial class to complement a Run-time Text Template's generated class.

Chances are that the problem is indeed that your partial class' namespace is different from the namespace of the generated part of that class. To check that, simply look at the generated code.

To fix that, you need to edit the .csproj file for your project, and in the section about that template, add the <ClassNamespace> tag:

<Content Include="MyTemplate.tt">
  <Generator>TextTemplatingFilePreprocessor</Generator>
  <ClassNamespace>My.Namespace</ClassNamespace>
  <LastGenOutput>MyTemplate.cs</LastGenOutput>
</Content>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top