How to change an instance at runtime?

I want a concept solution (not a logic solution) for the below question

Question ::

My question is...

Assume a window application
Assume the code is

<<<<<<<<<<<<<<<
Class ClassOne{
int getSingleDigitValue(){return 1;}
..
}

ClassOne c1;

(On the click event of the answer button the below code is executed)
a = c1.getSingleDigitValue();
>>>>>>>>>>>>>>>>

 


now a class ClassTwo is added.
Then the code is

<<<<<<<<<<<<<<<<
Class ClassOne{
int getSingleDigitValue(){return 1;}
..
}

Class ClassTwo{
int getSingleDigitValue(){return e;}
..
}

ClassOne c1; //Object of ClassOne

(On the click event of the answer button the below code is executed)
a = c1.getSingleDigitValue(); //Now c1 should be any of ClassOne or ClassTwo

(Here manually selecting 'ClassOne' or 'ClassTwo'
(On the click event of the Choose button a operation should be done and object 'c1' should be the selected class)
>>>>>>>>>>>>>>>>>

(I WANT A SOLUTION OTHER THAN INTERFACE AND ADDING ANY OHTER TEMPLATE AS THE COMBINATION OF BOTH CLASS)



Answer this question

How to change an instance at runtime?

  • egghead2006

    Create an interface that is implemented by both classes and use that as a reference instead of both classes:



    interface IProvidesSingleDigitValue
    {
         int getSingleDigitValue()
         {
            get;
         }
    }
    class ClassOne : IProvidesSingleDigitValue
    {
        public int getSingleDigitValue()
        {
            get { return 1; }
        }
    }
    class ClassTwo : IProvidesSingleDigitValue
    {
        public int getSingleDigitValue()
        {
            get { return 2; }
        }
    }
    public void MyMethod(IProvidesSingleDigitValue obj)
    {
        int i = obj.getSingleDigitValue();
    }

     



  • mattdawg

    Hi, i want a solution using other than interface.


  • akodad

    Another solution would be to use a delegate. (Think of a delegate asa typesafe function pointer.) You could then select from between the two (or more) implementations. The only requirement for a delegate is that the method signatures match exactly. Even the names can be different. If you don't know the types/methods at compile-time, you may have to resort to reflection to identify the methods based on method signature, naming convention, or custom attribute.
     
    public class MyClass
    {
        public delegate int GetValue();
        public static void Main()
        {
            GetSingleDigitValue gsdv
    = new GetValue(ClassOne.GetSingleDigitValue);
            gsdv();
        }
    }

    public class ClassOne {
       
    public static int GetSingleDigitValue(){return 1;}
    }

    public class ClassTwo{
       
    public static int GetSingleDigitValue(){return 2;}
    }


  • nick0123

    Depending on what exactly you're doing, you could also implement a Strategy pattern (http://www.dofactory.com/Patterns/PatternStrategy.aspx)

    That's a concept solution for ya ;)

    -E



  • WereWolf

    Why don't you want to use interfaces They are specifically designed to allow different classes to provide common functionality, exactly what you want to do. Using David's example:

    interface IProvidesSingleDigitValue
    {
         int getSingleDigitValue()
         {
            get;
         }
    }
    class ClassOne : IProvidesSingleDigitValue
    {
        public int getSingleDigitValue()
        {
            get { return 1; }
        }
    }
    class ClassTwo : IProvidesSingleDigitValue
    {
        public int getSingleDigitValue()
        {
            get { return 2; }
        }
    }

    IProvidesSingleDigitValue c1;
    int _value;

    public
    void Choice_Click(...)
    {
        if (something)
            c1 = new ClassOne();
        else
            c1 = new ClassTwo();

        _value = c1.getSingleDigitValue();

    }


  • spudstrawb

    Besides them inheriting off a common base class, the only other way I can think of is to use reflection:



     class ClassOne
     {
      public int GetValue()
      {
       return 1;
      }
     }
     class ClassTwo
     {
      public int GetValue()
      {
       return 2;
      }
     }
     public class ValueGetter
     {
      public static int GetValue(object obj)
      {
       MethodInfo method = obj.GetType().GetMethod("GetValue");
       return (int)method.Invoke(obj, null);
      }
     }

     

    Basically the ValueGetter.GetValue method attempts to find a method called GetValue at runtime and invokes it (calls) and returns its result.



  • How to change an instance at runtime?