Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
public class A
{
   public string prop 
   {
         get{  ...some logic...; }
   }

   public string method()
   {
       string p =  do something calculation using prop.
       return p;
   }
}  

public class B
{
   // i would like to override property prop,
   // so when i call B.method from client, the return value will be calculated 
   //based on overrided property
}


What I have tried:

if the property can be override, or only the method can be.
Is there any idea to implement this if keep the property.
Posted
Updated 9-Oct-17 4:08am

1 solution

Properties can be overridden:
public class A
    {
    public virtual string prop
        {
        get { return "A:prop"; }
        }

    public string method()
        {
        string p = prop + " Added";
        return p;
        }
    }

public class B : A
    {
    // i would like to override property prop,
    // so when i call B.method from client, the return value will be calculated 
    //based on overrided property
    public override string prop
        {
        get { return "B:prop"; }
        }
    }

private void MyButton_Click(object sender, EventArgs e)
    {
    A a = new A();
    A b = new B();
    Console.WriteLine("{0},{1}", a.prop, a.method());
    Console.WriteLine("{0},{1}", b.prop, b.method());
Will generate:
A:prop,A:prop Added
B:prop,B:prop Added
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900