0

[Edited for additional clarity]

I have an abstract class where I've defined a property - let's call it 'BulkValueSet'. BulkValueSet is a class which holds a large number of numeric values.

public abstract class Parent 
{
    public BulkValueSet ValueSet {get;set;}
        
    // additional properties and methods

}

BulkValueSet implements several different interfaces. Let's take two of them called ISpecificValueSetA and ISpecificValueSetB.

public class BulkValueSet : ISpecificValueSetA , ISpecificValueSetB
{

    // lots of numbers ! 

}

Now for the crux of the matter: I have two child classes (ChildA and ChildB) that inherit from the Parent class. I would like for these classes to inherit the BulkValueSet property from the Parent class but modify it via the interfaces defined. I've tried hiding the parent property by using the new keyword

public class ChildA : Parent
{

    new public ISpecificValueSetA ValueSet {get;set;}

    // additional stuff

}

public class ChildB : Parent
{

    new public ISpecificValueSetB ValueSet {get;set;}

    // additional stuff

}

the intent being that the property read from the child classes returns the filtered values as provided by the relevant interface. But this isn't really working - Visual studio shows both parent and child properties as being present and accessible and accessing the 'ValueSet' property using the dot operator seems to default to the property defined in the Parent class.

How can I go about this ?

Edit: Adding some additional context - I want to do this with multiple child classes - each with their own interface implementation of BulkValueSet.

Andorrax
  • 33
  • 4

2 Answers2

1

what is wrong with it

public abstract class Parent 
{
     public IBulkValueSet ValueSet {get;set;}
        
    // additional properties and methods

}

update, since the question was changed. Now ISpecificValueSet could be like this

public interface IBulkValueSet : ISpecificValueSetA , ISpecificValueSetB
{

    // lots of numbers ! 

}

public class BulkValueSet : IBulkValueSet
{

    // lots of numbers ! 

}

but it doesn't make much sense to me to inherit a huge class in order to use a part of it.

Serge
  • 40,935
  • 4
  • 18
  • 45
  • Hi Serge, I will have multiple different child classes that inherit from Parent - each with their own unique interface implementation of BulkValueSet. Unless I'm wrong, your solution would work if only one child class were using the BulkValueSet property ? – Andorrax Sep 07 '21 at 23:43
  • @Andorrax , Can you show 2 examples of your child classes, to see how they are differnt pls? – Serge Sep 07 '21 at 23:48
  • Hi @Serge, I've edited the description to provide more clarity – Andorrax Sep 07 '21 at 23:56
  • @Andorrax Thanks. But it is getting even more fuggy. Now I don't understand why do you neeed BulkValueSet IParent.ValueSet { get; set; } at all. Why you just dont make a separate ValueSet for each child. What are you going to win if you inherit ValueSet from parent? – Serge Sep 08 '21 at 00:55
  • @Andorrax it doesn't make any sense to inherit a huge class to use a part of it. You are moving wrong way. It is alway at first small class, after this another class inherit this one , after another previous. But you are trying to do everything upside down. – Serge Sep 08 '21 at 01:01
0

This is the closest that I think I can get to your desired design:

public interface IParent
{
    BulkValueSet ValueSet { get; set; }
}

public abstract class Parent : IParent
{
    BulkValueSet IParent.ValueSet { get; set; }
}

public interface ISpecificValueSetA { }

public interface ISpecificValueSetB { }

public interface ISpecificValueSetC { }

public class BulkValueSet : ISpecificValueSetA, ISpecificValueSetB, ISpecificValueSetC { }

public class ChildA : Parent
{
    public ISpecificValueSetA ValueSet { get => (this as IParent).ValueSet; }
}

public class ChildB : Parent
{
    public ISpecificValueSetB ValueSet { get => (this as IParent).ValueSet; }
}

public class ChildC : Parent
{
    public ISpecificValueSetC ValueSet { get => (this as IParent).ValueSet; }
}
Enigmativity
  • 113,464
  • 11
  • 89
  • 172