21

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'
    }
}
Adam Tegen
  • 25,378
  • 33
  • 125
  • 153

12 Answers12

49

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

Ram
  • 15,908
  • 4
  • 48
  • 41
  • 11
    Omg thank you, this was driving me nuts! Apparently when I added the class, it wasn't added as a ".cs" file, just a no-extension file. I manually added ".cs", but I guess that wasn't enough of a clue for VS. Anyway, thanks again! – emery.noel Nov 30 '18 at 12:16
  • 2
    I faced the same. I added without extension also and after manually added. Thanks – cdev Jan 21 '19 at 05:59
  • Same issue with `App.xaml` and `App.xaml.cs`. I forgot to add the C# file explicitly so the generated code was incomplete, even thought the C# was displayed in the Solution Explorer. Had to manually edit the `csproj` to add the C# file correctly. – Timo Jul 07 '22 at 09:19
45

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

Sklivvz
  • 30,601
  • 24
  • 116
  • 172
7

different namespace?

Matt Brunell
  • 10,141
  • 3
  • 34
  • 46
6

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
  • 2,612
  • 21
  • 25
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.

Community
  • 1
  • 1
Andrey K.
  • 665
  • 8
  • 29
  • 1
    This could be a good answer if you told us what exactly was said on your local SO, linked to the relevant question and translated it (assuming it's not in English). – ankh-morpork Aug 05 '15 at 12:05
2

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
  • 200,371
  • 61
  • 386
  • 320
  • 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 Oct 08 '08 at 21:24
  • 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 Oct 08 '08 at 21:26
  • 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 Oct 08 '08 at 21:28
  • 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 Oct 08 '08 at 21:40
2

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
  • 6,811
  • 4
  • 37
  • 53
  • 1
    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 Oct 08 '08 at 21:41
1

All the files should be in the same folder.

Anton Andreev
  • 2,052
  • 1
  • 22
  • 23
  • This was the only problem with my problem! I had a folder tree like: - Extensions -Client - Model I moved the extensions folder so i now have - Client -Model/-Extensions They are now at the same level and it is now working – Andre Fritzsche May 09 '19 at 16:41
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.

sada
  • 9
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>
Vic Seedoubleyew
  • 9,888
  • 6
  • 55
  • 76
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.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
-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.

Eric Ouellet
  • 10,996
  • 11
  • 84
  • 119