How can I derive a class nested inside a partial class? - c#

If I have an abstract class nested inside a partial container class nested inside another class, how can I derive classes from the base class? I would think the following would work, but it says BaseClass isn't accessible due to protection level. I need the derived classes to be private though.
And before anyone tells me what a bad structure this is, this is why I might need it.
class SingletonClass
{
public static partial class ContainerClass
{
abstract class BaseClass
{
}
}
}
static partial class ContainerClass
{
class DerivedClass : BaseClass
{
}
}

You code does not compile because there are two different ContainerClass types. One is a "normal" class and the other is nested in SingletonClass. If there are no namespace involved then the full name of the first is SingletonClass.ContainerClass while the full name of the second is ContainerClass (i.e., two different types).
To do what you want you need to make SingletonClass partial also:
partial class SingletonClass
{
public static partial class ContainerClass
{
abstract class BaseClass
{
}
}
}
partial class SingletonClass
{
public static partial class ContainerClass
{
class DerivedClass : BaseClass
{
}
}
}

Related

Is it a bad idea to initialize all derived member variables in base class?

I have a bunch of derived classes that inherit from a base class. Each derived class still has unique members. One of these derived classes needs to be able to access all the other derived class's members. My aim with this problem is to write as little code as possible.
Would it be sloppy or bad practice to initialize all the derived classes' members in the base class so that one of the derived classes can access those members, instead of initializing them in said derived class?
Proposed approach:
public class BaseClass {
public BaseClass() {
...
der1Initializer = Der1Initializer(new Factory1());
der2Initializer = Der1Initializer(new Factory2());
List initializers = new List(){ der1Initializer , der2Initializer };
der3Initializer = Der3Initializer(initializers);
}
Der1Initializer der1Initializer;
Der2Initializer der2Initializer;
Der3Initializer der3Initializer;
}
public class DerivedClass1 : BaseClass {
public SomeFunction {
der1Initializer.init();
}
}
public class DerivedClass2 : BaseClass {
public SomeFunction {
der2Initializer.init();
}
}
public class DerivedClass3 : BaseClass {
...
}
So that:
public class Der3Initializer {
public GroupInitializationFunction {
initializers[0].init(); //der1Initializer
initializers[1].init(); //der2Initializer
}
}
Instead of:
public class BaseClass {
public BaseClass() {
...
}
public class DerivedClass1 : BaseClass {
public DerivedClass1 {
der1Initializer = Der1Initializer(new Factory1());
}
public SomeFunction {
der1Initializer.init();
}
Der1Initializer der1Initializer;
}
public class DerivedClass2 : BaseClass {
public DerivedClass2 {
der2Initializer = Der2Initializer(new Factory2());
}
public SomeFunction {
der2Initializer.init();
}
Der2Initializer der2Initializer;
}
public class DerivedClass3 : BaseClass {
public DerivedClass3 {
List initializers = new List()
{
Der1Initializer(new Factory1()),
Der2Initializer(new Factory2());
};
der3Initializer = Der3Initializer(initializers);
}
...
Der3Initializer der3Initializer;
}
...
public class Der3Initializer {
public GroupInitializationFunction {
initializers[0].init(); //der1Initializer
initializers[1].init(); //der2Initializer
}
}
This is a vast oversimplification of the "problem". The purpose of this code is to reduce duplicate code and the need to reinitialize members that can be shared and to optimize performance.
I am aware that it isn't necessarily a good idea to give other classes access to members they aren't using.
I just thought this is an interesting problem regarding code separation vs. duplicate code.
I would not recommend initializing childs on the parent class as
Violates Open Closed Principle as the base class has the responsibility of initializing its children and there will always be the need to add new child classes and consequently modifying base class .
Violates Single Responsibility Principle as this is not the role for the base class (to initialize its childs )
Violates Inversion of Control as base class is tightly coupled to child class see this link

How to Inherit from Generic Parent

I have a parent Class
public class GenericRepository<TEntity> where TEntity : class
{
//Implementation
}
And I want to inherit from this class, but I can't seem to get it right,here are my attempts
public class CustomerRepository<Customer> : GenericRepository<Customer>
{
//implementation
}
Or this,
public class CustomerRepository<T> : GenericRepository<T> where T : new Customer()
{
}
Or this one
public class CustomerRepository<T> : GenericRepository<CustomerRepository<T>> where T : CustomerRepository<T>
{
}
No matter what I do, I get this error. Please show me how I can inherit from this class, classes share the same Namespace
Error 'GenericRepository' does not contain a constructor that takes 0 arguments CustomerRepository.cs
It sounds like you want a non-generic class inheriting from a generic one, like this:
public class CustomerRepository : GenericRepository<Customer>
{
}
If you want this to be a generic class that narrows the type of the generic parameter (only allows Customer or a derived type):
public class CustomerRepository<T> : GenericRepository<T>
where T : Customer
{
}
Regarding your compile-time error:
Error 'GenericRepository<Customer>' does not contain a constructor that takes 0 arguments
This means exactly what it says. You have not defined a constructor in your derived class, which means that a constructor is implicitly generated, as though you had typed this:
public CustomerRepository() : base() { }
However, the base class (GenericRepository<Customer>) does not have a constructor that takes no arguments. You need to explicitly declare a constructor in the derived class CustomerRepository and then explicitly call a constructor on the base class.
You don't need to repeat the type parameter in the deriving class, so:
public class CustomerRepository : GenericRepository<Customer>
{
//implementation
}
Is what you need.
It seems that your base class has no constructor without parameters, if so the derived class must declare a.constructor and call the base class constructor with parameter.
class MyBase { public MyBase(object art) { } }
class Derived : MyBase {
public Derived() : base(null) { }
}
In this example if you remove the ctor from Derived you get the same error.
Use can write as:
public class CustomerRepository : GenericRepository<Customer>
{
//implementation
}

Why is a nested public class less accessible than it's parent?

I have the following classes:
public abstract class Navigator<T> where T : Navigator.Route
{
public class Route
{
}
}
public class P2PNavigator : Navigator<P2PNavigator.Route>
{
public class Route : Navigator.Route
{
}
}
During compilation I receive two errors.
Inconsistent accessibility: constraint type 'Navigator.Route' is less accessible than Navigator<T>'
Inconsistent accessibility: base class 'Navigator.Route' is less accessible than class 'P2PNavigator.Route'
Everything has public accessibility. What am I missing to make this work? I realise I could ultimately make them all namespace level classes, but I would prefer to have nesting.
EDIT: Thanks to the answers, I found the root cause of the problem was my partial classes weren't defined properly.
public partial abstract class Navigator<T> where T : Navigator.Route
{
}
partial class Navigator // Different to Navigator<T> and implicitly internal.
{
public class Route
{
}
}
Navigator.Route is not the same as Navigator<P2PNavigator.Route>.Route and should be giving you other errors.
Code that specifies types correctly compiles ok:
public abstract class Navigator<T> where T : Navigator<T>.Route
{
public class Route
{
}
}
public class P2PNavigator : Navigator<P2PNavigator.Route>
{
public class Route : Navigator<P2PNavigator.Route>.Route
{
}
}
You probably have some non-generic Navigator class that makes your sample comile with errors you see.

How to prevent inherited classes instances

I want to force any class not to be able to create a new instance if it inherits a specific base class, so how this base class should look like?
The following code is in java. just to give you an Example
Base class has an exception on the constructor.
public class BaseClass
{
public BaseClass()
{
throw new AssertionError();
}
}
The child class extending the base class but if you create an object of it it will give u an exception.
public class MainClass extends BaseClass
{
public MainClass()
{
}
public static void main(String[] args) {
MainClass c = new MainClass();
}
}
You want to seal your base class.
public sealed class BaseClass
{
public BaseClass(){};
}
public class SubClass : BaseClass
{
public SubClass(){};
}
This will throw a compiler error because you cannot inherit from a sealed base.
You can't specify that in the baseclass, any deriving class is self responseable, if it wants to present the ability to be derived from, than you can't do anything about it.
you can declare the base class as const - that way other classes cant extend it.
You can't do this. Please specify why you want to do this.

Problem with interface implementation in partial classes

I have a question regarding a problem with L2S, Autogenerated DataContext and the use of Partial Classes. I have abstracted my datacontext and for every table I use, I'm implementing a class with an interface. In the code below you can see I have the Interface and two partial classes. The first class is just there to make sure the class in the auto-generated datacontext inherets Interface. The other autogenerated class makes sure the method from Interface is implemented.
namespace PartialProject.objects
{
public interface Interface
{
Interface Instance { get; }
}
//To make sure the autogenerated code inherits Interface
public partial class Class : Interface { }
//This is autogenerated
public partial class Class
{
public Class Instance
{
get
{
return this.Instance;
}
}
}
}
Now my problem is that the method implemented in the autogenerated class gives the following error:
-> Property 'Instance' cannot implement property from interface 'PartialProject.objects.Interface'. Type should be 'PartialProjects.objects.Interface'. <-
Any idea how this error can be resolved? Keep in mind that I can't edit anything in the autogenerated code.
Thanks in advance!
You can solve this by implementing the interface explicitly:
namespace PartialProject.objects
{
public interface Interface
{
Interface Instance { get; }
}
//To make sure the autogenerated code inherits Interface
public partial class Class : Interface
{
Interface Interface.Instance
{
get
{
return Instance;
}
}
}
//This is autogenerated
public partial class Class
{
public Class Instance
{
get
{
return this.Instance;
}
}
}
}
Return types aren't covariant in C#. As you can't change the auto-generated code the only solution I see is to change the interface:
public interface Interface<T>
{
T Instance { get; }
}
And change your partial class accordingly:
public partial class Class : Interface<Class> { }

Categories

Resources