partial class does not contain definition - c#

I got a weird problem and finally fixed it. Just like to share this expeirence so other people will search this topic and get the answer.
I defined 2 partial classes, and compiled with the error of "does not contain a definition for ...". It was weird because it worked when the 2 classes were individual classes.
Here is the code:
Before:
Foo1.cs
public class foo1{public void xyz(){}}
Foo2.cs
public class foo2{}
After:
Foo1.cs
public partial class foo1{ public void xyz(){}}
Foo2.cs
public partial class foo1{}
Compilation said "MyNamespace.foo1 does not contain a definition for xyz". But xyz was good when the class was not partial! This could be caused by different namespaces. But I doubted that. Because if namespaces were different, it would not compile.
I figured out at last. The problem was in Foo1.cs, the namespace was not defined but it was defined in Foo2.cs. Before I defined the partial class, the compiler provided a default namespace to Foo1.cs, that was the same as the one defined explicitly in Foo2.cs. But with the partial class, the compiler would not provide a default namespace to the partial class. This is why the compiler complained when a third party called foo1.xyz().
I learned a lesson that always define the namespace explicitly for each single cs file, except that you do not define the namespace.

Always define namespace explicitly.

Related

Why is the using alias directive not resolving the conflict between imported class and class in parent namespace?

I am working with a code base which includes the following class
namespace Api.Data.Models;
// legacy db class entry
public class Log
{
}
and in another class I am constructing a model binder which includes some logging
using Log = Serilog.Log;
namespace Api.Data.Models.Binding;
public class ModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
Log.Information("Trying to call Log.Information");
// This cannot resolve symbol Information as it is still pointing at Api.Data.Models.Log
}
}
I'm not sure if I am trying to do the impossible or if I'm missing something, but why is the aliasing not resolving my name conflict issue in this scenario?
There are other ways to solve my specific problem in the code base but I couldn't find a post or question about this particular case online and hence the question.
The main problem is the namespace nomenclature here.
Your Log class is declared in namespace Api.Data.Models namespace. and ModelBinder is declared in the namespace Api.Data.Models.Binding;.
Api.Data.Models prefix is the same for both the namespaces and that is why it considers your Log class when calling Log.Information("Trying to call Log.Information");.
So you could have a different namespace and your issue will be resolved. like for your Log class use namespace Api.Data.Models.Logs.
Edit:
As mentioned in the comment, If you can not change the Log file then you could change the namespace of ModelBinder class or use a different alias for Serilog.Log. Thank you #Fildorfor pointing it out.
Based on OP's comment the local namespace class always takes precedence over the imported class of the same name regardless of aliasing.

Inherited base class that inherits ComponentBase in partial class gives error

I am working with a something.razor file and a code behind Something.razor.cs file. My partial class inherits from SomethingBase which itself inherits from ComponentBase.
This however gives errors
CS0115 .buildrendertree(rendertreebuilder)': no suitable method found to override in partial class
CS0263 partial declarations of must not specify different base classes.
However if Something inherits directly from ComponentBase there is no issue.
First question please be gentle. I'll update with code and more detail in the morning if needed.
With a code behind file you have 2 spots where you can specify the base class.
You will always have to specify it in the .razor file because you don't want the default there:
// Something.razor
#inherits SomethingBase
and then you have a choice in the .razor.cs file:
// Something.razor.cs (a)
partial class Something // take base class from other part
{
}
or
// Something.razor.cs (b)
partial class Something : SomethingBase // must use the same as other part
{
}
I would use (a) .

C# - private static method in partial class not shared between files?

I'm creating static partial class like this.
If I put them in the same file there is no problem, but when I move one class to another file in the same project, this method private_method_from_a() does not exist anymore.
Is this by design? Do I miss something here?
//File A
public static partial class PartialClass
{
private static void private_method_from_a()
{
//Do something
}
}
//File B
public static partial class PartialClass
{
public static void public_method_b()
{
private_method_from_a();
}
}
Most likely the two files are in different namespaces, as #MickyD suggested in a comment. The reason for this problem is you're then making 2 different classes entirely. Chances are, one of them is in the namespace of your project, while the other is in the global namespace - that is, you didn't specify a namespace for it at all. Here's how the compiler sees them (suppose your project is called "MyProject", and has that name as the top-level namespace):
partial class global::MyProject.PartialClass
and
partial class global::PartialClass
Well, that's two distinct types, with different fully qualified names, both partial.
Make sure they're are both in the same namespace for the C# compiler to treat them as the same type.

How to implement partial class in MVC4?

I have a class Employee.cs that was autogenerated by EntityFramework when i connected to a database, now i want to implement a partial class but I can't use "Employee.cs" name for that class because it already exists. Can I give other name to it and it will work? Can you explain how the compiler will know that there is another partial class of Employee.cs?
You can give the file whatever name you want like Employee.custom.cs but you have to call the class in it Employee in the same namespace as the Employee class in the other file, also with a partial modifier, and you should be good to go.
The compiler just gathers all the partial classes with the same name and compiles it into one class per name.
Compiler always works with fullname of class. If there is a project named MyProject, full name for the class would be something like:
MyProject.Employee
If You want to create class with the same name, You have to add a level of separation in naming, or if it have to be partial class with this generated class, You have to mark it also as a partial.
You can either create (in Model/Employee.cs):
public class MyProject.AnySubPath.Employee {}
Or:
In file Model/Employee.cs
public partial class MyProject.Employee {}
In file ViewModel/Employee.cs
public partial class MyProject.Employee {}
Important for You is to realize what is the partial mean & what will the compiler to with Your class: In fact the compiler will just put all the partial classes together and then compile them. Only benefit from being partial is that You can have the code split among multiple files. Excelent example for this is Windows.Forms, where You have "code-behind" file (Form1.cs) and then You have "designer" file (Form1.Designer.cs).
Also You can name the files whatever name You want, it is just common standard to have 1 class = 1 file and the class name should fit the file name.

Missing partial modifier on declaration of type [class name]; another partial declaration of this type exists

Note: I found some similar questions (Link 1 and Link 2), but they didn't helped me.
I'm trying to create a partial class, but I getting the following error on the "main class":
Missing partial modifier on declaration of type SomeClass; another partial declaration of this type exists
Those are the files that cotains the classes and the problem is that the error is pointing to the "main class" not the partial class. The reason for that I'm creating this partial class is that the file SomeClass.cs is Checked Out with someone else as we (the people developing with me) are using Team Foundation Server for source control.
File: SomeClass.cs
namespace MyNamespace
{
public class SomeClass
{
// Some methods...
}
}
File: SomeClass2.cs
namespace MyNamespace
{
public partial class SomeClass
{
// Some methods...
}
}
If you check MSDN - Partial Classes and Methods you can find the notice: All the parts must use the partial keyword.
Just flag both declarations as partial.
There is no "main" class and "partial" classes when you use partial in C#. The partial modifier just means that the class can be defined in multiple locations, but it is still a single class. The compiler wants all declarations of the class to be denoted as partial if any are partial, which has the effect of always making every location obvious that there is more to the class declaration than what you see at the time.
If any declaration of a class is labeled partial, then all declarations of it need to be partial.

Categories

Resources