14

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'
    }
}
4

12 回答 12

40

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

于 2008-10-08T21:17:43.347 回答
35

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 -> Build action property

于 2017-07-25T02:03:21.167 回答
5

different namespace?

于 2008-10-08T21:18:15.513 回答
5

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.

于 2008-10-08T21:48:35.953 回答
4

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.

于 2015-08-05T12:01:55.887 回答
1

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.

于 2008-10-08T21:21:02.123 回答
1

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.

于 2008-10-08T21:21:52.950 回答
1

All the files should be in the same folder.

于 2018-12-24T19:35:24.203 回答
0

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.

于 2010-10-21T15:13:15.347 回答
0

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>
于 2018-07-16T12:19:19.387 回答
0

Same namespace in textual context just a different case on the words; which actually puts them into different compiler namespaces.


Example

Note the case of O in overflow.

File 1:

namespace StackOverflow {
public partial class MyVM

File 2:

namespace Stackoverflow {
public partial class MyVM

Turns out somewhere in the past ten years my default project name became different that my original project name, but only in case. Uggg.

于 2021-12-07T22:07:46.187 回答
-3

Just for reference (VS 2020)... Error CS0103 => All same but different folder.

But classes should have same namespace AND ALSO BE in same folder !!!

Although they could be defined in the same namespace, both files should be in the same folder. I know that the folder structure should reflect the namespace but for clarity between the generated code and my added code, I wanted to separate by folder, but it does not works.

于 2020-10-01T14:25:16.840 回答