1

Apologies if the title is not quite right.
I've got a set of base classes I am deriving from. There is a main one and a bunch of components I can add onto it.

    public class Entity {
        public EntityPart ep;
    }
    public class EntityPart {
        public void eFunc() {}
    }

My derived classes work perfect for all my virtual functions obviously. But here's the problem I ran into

    public class dEntity : Entity {
        public void someFunc() {
            ep.derivedFunc(); // Error, does not exist on EntityPart
        }
    }
    public class dEntityPart : EntityPart {
        public void derivedFunc() {}
    }

I pretty quickly figured out what was going on. Since ep was still just an EntityPart it didn't have the derived functionality.

I found a solution that worked but felt like something was off. When I asked others they seemed confused that my code didn't work before.

    public class dEntity : Entity {
        public new dEntityPart ep {
            get { return (dEntityPart)base.ep; }
        }
    }

As I understand this is simply declaring a variable named ep as the type dEntityPart. This is a property and has the get function that casts base.ep (which is just EntityPart) as a dEntityPart. The new keyword hides base from within the class.
I'd like to double check first that this understanding is correct.

This worked just as I needed but it seemed messy. I have many sets of derivations of these class, each with many parts. This method feels a little slow and cumbersome. Is there a way to set up this down casting to always be there, or perhaps a way to setup some generic to work like this?

If this is the way to do it, alrighty. I'm interested in perhaps a more elegant method or some better way to set up such code. Thanks!

1
  • looks like you are looking for Generics ?
    – vasily.sib
    Commented Mar 11, 2020 at 7:54

1 Answer 1

3

Does this work for you?

    public class Entity<T> where T : EntityPart {
        public T ep;
    }
    public class EntityPart {
        public void eFunc() {}
    }
    public class dEntity : Entity<dEntityPart> {
        public void someFunc() {
            ep.derivedFunc(); // This won't throw an error anymore.
        }
    }
    public class dEntityPart : EntityPart {
        public void derivedFunc() {}
    }
5
  • That looks to be it! Unsure why I couldn't seem to find the answer. Thank you.
    – Derek C.
    Commented Mar 11, 2020 at 8:08
  • Mind if I stick a little follow up here? I might have many derivations of EntityParts and a single entity might have up to 5 or 6 different parts on it. That seems like the declaration would become a mouthful. Do you think that would be an issue, or is there a way around such a thing?
    – Derek C.
    Commented Mar 11, 2020 at 8:18
  • That would indeed be a problem anyway you choose to proceed. You will either need a Entity<T1,T2,...> declarataion (I have only seen Func<> having so many declarations), or a lot of boilerplate code. If everything is a part, can't you just find the common functionality and use it as inhecritance intended? Commented Mar 11, 2020 at 8:22
  • I will have to put it into my work and see who it goes. While they are all parts they each take care of some different functionality. I'm trying to create a rather modular design. I will get back to you.
    – Derek C.
    Commented Mar 11, 2020 at 8:31
  • What you have will work but sadly with Unity, what I'm using, I can't do this so straightforwardly. Just in case, is there another method to downcast besides what I had?
    – Derek C.
    Commented Mar 11, 2020 at 16:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.