4

is this possible to somehow, have this scenario, where A.N inherits code from A with this code example?

The reason for setting it up like this, is that I need multiple classes that inherit from Base<TType> and the Nested : Base<TType> where the server has the base only, and the client has the extended Nested. This way, it would be easy to use the code, where they would have some shared code between themselves & each other.

The problem is that I would have to write identical code inside the

A and A.N  
B and B.N  
C and C.N 

etc.

I have solved this temporarily, by replacing the Nested abstract class, with an Interface and doing
A.N : A, INested, but now I have to rewrite the Base<TType>.Nested code again inside all the Nested classes. For now, the nested class is small & managable.

hope this isn't a confusing question...

public abstract class Base<TType> where TType : class
{
    public TType data;
    internal void CommonCodeForAll() { }
    public abstract void Update();
    public abstract class Nested : Base<TType>
    {
        public abstract void Input();
    }
}

public class A : Base<someClass>
{
    public float Somevariable;

    public void SpecificFunctionToA() { }

    public override void Update()
    {
        // code that gets executed on server & client side that is unique to A
    }

    public class N : A.Nested
    {
        public override void Input()
        {
            if (data.IsReady()) { Somevariable *= 2; }
            SpecificFunctionToA();
        }
    }
}
public class B : Base<anotherClass>
{
    public float Somevariable;
    public int index;
    public int[] Grid;

    public void SomethingElse() { }

    public override void Update()
    {
        // code that gets executed on server & client side that is unique to B
    }

    public class N : B.Nested
    {
        public override void Input()
        {
            if (Grid[index] == -1) { SomethingElse(); }
            data.Somevariable = Grid[index];
        }
    }
}

Edit: I updated the code example to show what I'm trying to achieve.
Why I am trying to do this, is to keep the physics, networking & User input seperate. There are multiple different controllers where each one has their own pack & unpacking functions, controller identity & access to the physics engine.

Eli
  • 85
  • 8
  • Nice question. For those who do not understand you want to use the implemented base method in the implemented nested method. You want to call `Update()` from `Input()` for example as implemented by `class A`. – John Alexiou Feb 05 '14 at 02:14
  • @ja72 Yes, + additional methods and variables specific to A, B, C etc. Just like if A.N was inheriting directly from A, and B.N would directly inherit from B and so on. – Eli Feb 05 '14 at 02:51
  • Maybe instead of inheritance you need class encapsulation. I will try to put together a solution for illustration. – John Alexiou Feb 05 '14 at 13:33

1 Answers1

1

I have a solution using ecapsulation of classes instead of inheritance.

public abstract class BaseGeneric<T>
{
    T data;
    // ctor
    protected BaseGeneric(T data)
    {
        this.data=data;
    }
    // methods
    public abstract void Update();
    // properties
    public T Data
    {
        get { return data; }
        set { data=value; }
    }

    // base nested class
    public abstract class BaseNested<B> where B : BaseGeneric<T>
    {
        protected B @base;
        // ctor
        protected BaseNested(B @base)
        {
            this.@base=@base;
        }
        // methods
        public abstract void Input(T data);
        public void Update() { @base.Update(); }
        // properties
        public T Data
        {
            get { return @base.data; }
            set { @base.data=value; }
        }
    }
}

// implementation base
public class Base : BaseGeneric<int>
{
    // ctor
    protected Base(int data) : base(data) { }
    //methods
    public override void Update()
    {
        this.Data+=1;
    }
    // implemented nested class
    public class Nested : Base.BaseNested<Base>
    {
        // ctor
        public Nested(int data) : base(new Base(data)) { }
        public Nested(Base @base) : base(@base) { }
        // methods
        public override void Input(int data)
        {
            this.Data=data;
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        // new implemented class with value 0
        var nested=new Base.Nested(0);
        // set value to 100
        nested.Input(100);
        // call update as implemented by `Base`.
        nested.Update();
    }
}
John Alexiou
  • 28,472
  • 11
  • 77
  • 133
  • This work in a way by using @base to change variables inside Base from Nested, I could work with this. I'm gonna leave this open a few more hours to see, if more solutions pop up while I'm experimenting. – Eli Feb 05 '14 at 17:03
  • It might work with `BaseNested` or just `BaseNested`. It depends if you want `@base` to be of type `BaseGeneric` or `Base`. I opted for the latter and thus added the generic type in the nested type. – John Alexiou Feb 05 '14 at 18:13