Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make partial virtual property?

Design -- in a perfect world

I have one abstract base class A with property Value, and two derived classes W and R. It is always possible to read the Value (and the method is always the same -- reading stored value), so I would put getter directly to A.

However writing to Value is not always possible, and thus there are W and R. W provides also a setter, while R does not -- it just relies on features that come from A.

The problem is, no matter what I tried, I cannot make such design come to life in C#. I cannot override Value property in W because there is no setter in base class (and there is not, because adding a setter to base class means it would "leak" to R as well).

Reality -- the ugly workaround (dont't read)

I added a setter in A which throws an exception. In W I override setter while in R I do nothing.

This is ugly, because of the design, because setter in R is public and because now such code compiles:

R reader;
reader.Value = 5;

The question

So how to make a property with only getter in base class, and add a setter in derived class?

The story

Because there is always at least one curious soul, I explain -- those classes are data holders/managers. The R class is dynamic data "holder" -- it computes value using given formula (similar to Lazy Eval class). Most of of stuff is common among W and R -- filtering values, sending notifications, and so on. The only difference is in R you cannot set the value directly, because the formula in one-way (similar to WPF converter with only one method defined, Convert).

Solution

Thank to Marc I solved this issue, here is the code:

  abstract class A {
      public int Value { get { return ... } }
  }

  class R : A { }

  class W : A {
      new public int Value {
          get { return base.Value; }
          set { .... }
      }
  }

The difference to Marc version is lack of setter in base class (it was the whole point of the question), the difference to my workaround is shadowing the property, not overriding it. Small, but crucial piece.

Thank you Marc for enlightenment :-).

like image 777
greenoldman Avatar asked Dec 15 '10 08:12

greenoldman


People also ask

How do you create a partial class?

A partial class is created by using a partial keyword. This keyword is also useful to split the functionality of methods, interfaces, or structure into multiple files.

Can a property be virtual?

Virtual real estate is unique land that has a deed and exists in a digital world. You can think of this world like a video game, but with more social and community components.

What is AC partial class?

Partial Class is a unique feature of C#. It can break the functionality of a single class into many files. When the application is compiled, these files are then reassembled into a single class file. The partial keyword is used to build a partial class.

What is partial method in C#?

A partial method has its signature defined in one part of a partial type, and its implementation defined in another part of the type. Partial methods enable class designers to provide method hooks, similar to event handlers, that developers may decide to implement or not.


1 Answers

abstract class A {
    public int Value {get; protected set;}
}
class R : A { }
class W : A {
    new public int Value {
        get { return base.Value; }
        set { base.Value = value; }
    }
}
like image 117
Marc Gravell Avatar answered Oct 06 '22 01:10

Marc Gravell