Skip to content

Change Classes while runtime?

Metty

Asked Jan 3, 2006, 2:55 PM EST1 answer
Share
LinkedInX
See: http://web48.isp4net.de/other/class.jpg I have an Class A, which is in the core, so I cant change it. Its function is called by an function which is in the core too. Now i inheritanced everything from Class A to a new Class B and created an override of the function called by the core. Now i want to change my A to the class B, so that the new function is called and not the function of Class A. How to do this without changing the Core? Class A is a "public class" which inheritated from another class. The function of Class A is inheritated ("public override void"). The Original function is a "public virtual void". Class B inheritated everything from Class A. Class B overrides the function of Class A => "public override void". Now i just have to change the type of..lets say "Test" from A to B...but how? I have to change it while running and i can't edit the core, which originally calls the function of A....

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0

    You can cast to derived class after superclass object is created. Here's couple examples:

    Java
    
    ClassA superClassObject = new ClassA();
    
    superClassObject.Test();
    
    
    //create new ClassB object by casting
    
    ClassB derivedObject = (ClassB)superClassObject;
    
    derivedObject.Test();
    
    
    //temporaly cast to ClassB
    
    ((ClassB)superClassObject).Test();
    
    
    //re-intanciate superClassObject as ClassB object (of course deletes the superClassObject's data)
    
    superClassObject = new ClassB();

    Other way you might want to try is delegates. Create delegate type with appropriate parameters and return value, intanciate it and set different methods to it.

    0