0

I am trying to call a method from class A and apply it to a value in class C:

class A
{
    //my code here
    public virtual string calculatebnft()
    {
        string bnft = "";
        //my code here
        return bnft;
    }
}

class B : A
{
    //my code here
    public override string calculatebnft()
    {
        string bnft = "";
        //my code here
        return bnft;
    }
}

class C : B
{
    //my code here
}

In my Main method:

C c1=new C();
//my code here
string bnft=c1.calculatebnft();
MessageBox.Show(bnft);

When I run program it always runs class B's method calculatebnft(). How can I call calculatebnft() from A instead? The rest of code from B "which is working correctly".

2
  • If the runtime type of your object is at least B it will always run the method from B (unless it is declared as new, but then it's actually a different method) Commented Jan 30, 2014 at 19:08
  • Even though you can achieve it, you might want to reconsider. I think most folks would (understandably) expect that if your class inherits from B, then it should utilize B's implementation of methods; otherwise, it makes more sense as a parallel implementation of A. Commented Jan 30, 2014 at 19:11

3 Answers 3

1

You can't do that without changing B code. You have to change calculatebnft method from override to new:

class B : A
{
    //my code here
    public new string calculatebnft()
    {
        string bnft = "";
        //my code here
        return bnft;
    }
}

with that you could do following:

string bngt = ((A)c1).calculatebnft();
Sign up to request clarification or add additional context in comments.

Comments

1

From the outside there is no way to call a base class instance of a virtual method. It is simply not possible because it is forbidden by the CLR (possible, but not verifiable). The only way to get access to it is for A or B to give you another method on which to call the functionality. For example

class A {
  public string callMe() { 
   return ...;
  }

  public virtual string calculatebnft() {
    return callMe();
  }
}

Now a caller who wants the A version of calculatebnft can use callMe instead.

In general though I'd consider this bad practice. If there is a situation where you really wanted the base class version of a virtual method then it's probably not the best method to be virtual.

4 Comments

+1 exactly what I was about to post. Can you elaborate a bit on possible, but not verifiable?
@SriramSakthivel at an IL level you can issue a .call (non virtual method of calling) on A::calculatebnft and it would do the trick. This essentially how a base.calculatebnft would be issued. But when done from outside the class hierarchy it's considered unverifiable code
What you mean by unverifiable here? CLR will throw InvalidProgramException in that case? or something else?
@SriramSakthivel tools like peverify will say that it's incorrect. It can be loaded in the CLR under full trust but I doubt it would load under say partial trust
0

I don't think that's possible this way.

But for example you can do something like this:

class A
{
    //my code here
    public virtual string calculatebnft()
    {
        string bnft = "A";
        //my code here
        return bnft;
    }
}

class B : A
{
    public string calculatebnftFromA()
    {
        return base.calculatebnft();
    }

    //my code here
    public override string calculatebnft()
    {
        string bnft = "B";
        //my code here
        return bnft;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.