nosbor
nosbor

Reputation: 2999

Extending nested class which is using as property

I wrote a nested class which is used as a bag for properties. This class is used as property which I named Properties. I want to extend number of properties by interfaces.

I wrote this example:

public interface IFirst {
    int asd { get; set; }
}

public interface ISecond {
    int zxc { get; set; }
}

public class MyClass {
    public class PropertyClass : IFirst {
        public int asd {
            get {
                throw new NotImplementedException();
            }
            set {
                throw new NotImplementedException();
            }
        }
    }

    public PropertyClass Properties; 
}

public class MyNextClass : MyClass {
    public class PropertyClass : MyClass.PropertyClass, ISecond {
        public int zxc {
            get {
                throw new NotImplementedException();
            }
            set {
                throw new NotImplementedException();
            }
        }
    }

    public void test() {
        Properties.zxc = 5; // Here is problem
    }
}

But in this case I cant to read/write new property zxc.

I think because this still is reading a Properties type from parent class - MyClass.PropertyClass and not MyNextClass.PropertyClass.

I want to extending this without creating new property or hiding existing.

Do you have any suggestions?

Upvotes: 0

Views: 1454

Answers (2)

oddy
oddy

Reputation: 1900

Well, depending on what you’re trying to achieve approaches may vary. For instance, using abstract class might feet your needs. Like this:

public interface IFirst
{
    int asd { get; set; }
}

public interface ISecond
{
    int zxc { get; set; }
}

public abstract class MyAbstractClass<T> where T : class
{
    public abstract T Properties {get; set;}
}

public class MyClass : MyAbstractClass<MyClass.PropertyClass>
{
    public class PropertyClass : IFirst
    {
        public int asd
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }
    }

    public override MyClass.PropertyClass Properties
    {
        get { throw new NotImplementedException(); }
        set { throw new NotImplementedException(); }
    }
}

public class MyNextClass : MyAbstractClass<MyNextClass.PropertyClass>
{
    public class PropertyClass : MyClass.PropertyClass, ISecond
    {
        public int zxc
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }
    }

    public override MyNextClass.PropertyClass Properties
    {
        get { throw new NotImplementedException(); }
        set { throw new NotImplementedException(); }
    }

    public void test()
    {
        Properties.zxc = 5;
    }
}

Upvotes: 1

dlev
dlev

Reputation: 48596

You'll have to either ensure that the parent class implements both interfaces, or you'll have to create a new static member in the child class that is of the the nested child type. As you surmise, Properties is declared as being of the parent nested type, and declaring a new type in the child class of the same name that derives from the parent nested type doesn't change that.

Upvotes: 1

Related Questions