Search code examples
c#iosobjectxamarincocossharp

Assign different object to a one object in C#


I'm coding a game with cocosharp in C# for iOS. I want to have an object that will store different objects from different classes and through this object I want call public methods from this objects. The problem is that active object can be from three different classes with public methods with same name. My vision is like this:

//object for store active object
General_class active_object = new General_class();

//my_game_object is active layer now
active_object = my_game_object;


// pop_in() is method that has same name in different classes
active_object.pop_in();

My question is if is something like this even possible and what should be the General_class class.

Thank you

edit

I forgot to mention that my_game_object inherits from CCLayer class from cocossharp library.

edit 2

This thread solves my problem.


Solution

  • Easy, make those three classes implement an interface:

    public interface ILayer
    {
        void pop_in();
    }
    
    // one of your classes
    public class SomeLayer : ILayer
    {
        // ...
    }
    
    //object for store active layer
    ILayer active_layer = new SomeLayer();
    
    // rest of the code works
    

    Although I suspect that probably means your basic knowledge of C# is limited. Perhaps you should grab a book.