C# question, can you override a virtual object?
Community Forums/General Help/C# question, can you override a virtual object?
| ||
Ok, so I understand how you override functions. However, I can't seem to do it with an object. Anyone know how I can successfully do this? public class NameA : MonoBehaviour { public virtual MyAmazingClassObject param_name; } public class NameB : NameA { public override AnotherAmazingClassObject param_name; } public class NameC : NameA { public override YetAnotherAmazingClassObject param_name; } Thanks :) |
| ||
See here, here, and arguably here. Firstly, you don't use "virtual" or "override" with data members, only methods (and properties, which are cunningly-concealed methods, not fields). Use nothing on the base class, and "new" on the derived class. Instead of overriding in the traditional sense, "new" provides a class with both members and only shadows the base version when the object is accessed through a variable of the derived type (this is also usually intended for methods; it replicates "traditional" C++ behaviour). Secondly, changing the type of the member doesn't appear to be allowed, even though the new/override distinction means the compiler could probably handle it if forced, as it's apparently a violation of the base's type contract (in turn raising questions about the design that lead you to this question..?). You can create as many new param_name slots as you like, but they all have to be of type MyAmazingClassObject, and will be "physically" new fields that don't interact. I'm not sure what actual use there is for this option. |
| ||
Ok, what I am trying to do. I have an object in an inventory slot. The first class I have is a Card Slot. That's all finished. But now I have an Item slot, I want to be able to transfer this code so I can call the exact same variable names. However inside the functions they are mostly different (but have shared calls) therefore I am overriding some of these functions. Problem is I can't override 3 arrays of card slot variables. Instead of having card_slot, item_slot, weapon_slot etc. I just want to declare them all slot So I want Card[] slot Item[] slot Weapon[] slot But as it is I am going to have to pretty much have a copy and paste for every single item. Card[] card_slot Item[] item_slot etc. So that is my reason for wanting to use the same variable name for entirely different data structures. Last edited 2012 |
| ||
Why are you trying to inherit Item from Card? If Card is a functionality-class that actually does what you want, it should really be closed and not subject to further inheritance. I have the feeling (I still don't quite understand) that the best way to go about what you want is to either inherit from, or implement, an InventoryItem base class or interface, probably the latter. If the code handling all three classes is sufficiently identical that it can be shared, it should also be sufficiently generic to expect a more abstract type. What you're describing at the moment not only sounds rather difficult to achieve, but I don't think it's actually type-correct. You shouldn't be able to do what you're describing without violating type contracts. Last edited 2012 |
| ||
Yasha, I have figured out how to do what I want. However the solution was so embarrassingly stupid, I almost don't want to tell you :) It was staring me in the face all along. I simply did this. public class NameA : MonoBehaviour { } public class NameB : NameA { public AnotherAmazingClassObject param_name; } public class NameC : NameA { public YetAnotherAmazingClassObject param_name; } That's right, the objects I am creating didn't even need to exist in the main class at all. :O *facepalm* Please give me a facepalm Yasha, I deserve it. :) |
| ||
*facepalm* |