Reputation: 25905
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'
}
}
Upvotes: 21
Views: 21108
Reputation: 445
For me, I was splitting up a rather large WinForms form class. I added the new class file, then moved over a set of functions. Everything compiled fine, but I was seeing the CS0103 error in the Error List tab. Turns out, I was missing the Form line from the csproj file.
<Compile Include="MyPartialForm.cs">
<SubType>Form</SubType>
</Compile>
Upvotes: 0
Reputation: 31721
Same namespace in textual context just a different case on the words; which actually puts them into different compiler namespaces.
Note the case of O
in overflow.
namespace StackOverflow {
public partial class MyVM
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.
Upvotes: 0
Reputation: 11763
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.
Upvotes: -3
Reputation: 10576
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>
Upvotes: 0
Reputation: 16874
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
Upvotes: 56
Reputation: 735
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.
Upvotes: 4
Reputation: 9
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.
Upvotes: 0
Reputation: 2622
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.
Upvotes: 6
Reputation: 6840
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.
Upvotes: 2
Reputation: 204279
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.
Upvotes: 2
Reputation: 31173
Are the two partial classes in the same namespace? That could be an explanation.
Upvotes: 46