views:

75

answers:

5

Hi, Is it possible to invoke method A.F() from instance of B class except of using refactoring. Thanks..


  class Program
  {
    public class A
    {
      public virtual void F()
      {
        Console.WriteLine( "A" );
      }
    }
    public class B : A
    {
      public override void F()
      {
        Console.WriteLine( "B" );
      }
    }

    static void Main( string[] args )
    {
      B b = new B();  

      //Here I need Invoke Method A.F() , but not overrode..      

      Console.ReadKey();
    }
  }
+1  A: 

you can use base.method() in the derived class to call the method in base class.

Kangkan
Yes, I know this... But I want to execute A.F() from Main function, not from derived class..
Yuriy
+2  A: 

You need base.F();

gkrogers
A: 

I'm not sure if you can call the base method directly (might be a crazy trick out there that I am not aware of). As a slight alternative, can you add methods to class B? If so, add:

public void F (int i)
{
    base.F();
}

And call it using:

b.F(1);
Edward Leno
+1  A: 

You can only call the base method from within the class definition itself. So if you have a need to do it, you'll have to do it there. It may come down to you have to overload that method to call the base method from an external source (similar to how @Edward Leno mentiods).

class Program 
  { 
    public class A 
    { 
      public virtual void F(bool useBase) 
      { 
        Console.WriteLine( "A" ); 
      } 
    } 
    public class B : A 
    { 
      public override void F(bool useBase) 
      { 
        if(useBase) base.F();
        else Console.WriteLine( "B" ); 
      } 
    } 


    static void Main( string[] args ) 
    { 
        B b = new B();   

        //Here I need Invoke Method A.F() , but not overrode..       
        b.F(true);

        Console.ReadKey(); 
        } 
    }
  }
Joel Etherton
+6  A: 

You may use the new keyword to have another definition of the same (named) method. Depending on the type of reference, you call A's of B's implementation.

public class A
{
  public void F()
  {
    Console.WriteLine( "A" );
  }
}
public class B : A
{
  public new void F()
  {
    Console.WriteLine( "B" );
  }
}

static void Main( string[] args )
{
  B b = new B();  

  // write "B"
  b.F();

  // write "A"
  A a = b;
  a.F();
}

If you feel that new is not the right solution, you should consider to write two methods with a distinguished name.


Generally, you can't call the base implementation from outside the class if the method is properly overridden. This is an OO concept. You must have another method. There are four ways (I can think of) to specify this distinguished method:

  • write a method with another name.
  • write a method with same name but another signature (arguments). It's called overloading.
  • write a new definition of the method with the same name (use the new keyword) It's called hiding.
  • Put it onto interfaces and implement at least one explicitly. (similar to new, but based on interface)
Stefan Steinegger