1

I have a property A in all subclasses of base class Base.
How can I generate an abstract property definition of property A into base class Base?

I know ReSharper's refactoring Pull Members Up, but that moves the property to base class.

I need an abstract property in base class and a overriding properties in all sub classes. Is there a refactoring in Visual Studio or in ReSharper that can do it automatically for me?

Dmitry Osinovskiy
  • 9,999
  • 1
  • 47
  • 38
thersch
  • 1,316
  • 12
  • 19

3 Answers3

2

There is a checkbox "Make abstract" for that in ReSharper Pull Members Up dialog : enter image description here

George Mamaladze
  • 7,593
  • 2
  • 36
  • 52
  • Oh had overseen the checkbox. Your answer is exactly that what I was looked for. – thersch Jul 13 '12 at 11:46
  • Unforunatly *Pull Members Up* does not automatically update the other sub classes (by making it *override*). – thersch Jul 13 '12 at 12:01
  • 1
    You can vote for the request to fix it here http://youtrack.jetbrains.com/issue/RSRP-275311 – Dmitry Osinovskiy Jul 18 '12 at 11:41
  • @DmitryOsinovskiy Your YouTrack issue is not the same as my problem in my last comment. My leftover problem is that other subclasses are not updated automatically. (Maybe this was the intention of ReSharper team because ReSharper does not know the implementation of other subclasses.) Your problem is that *virtual* keyword is not converted to *override* in current subclass. But this error does not happen on my machine (VS 2010, ReSharper 5.1.3). – thersch Jul 18 '12 at 12:32
  • My problem is also that other subclasses are not updated automatically (by making them override). Exactly as you said. – Dmitry Osinovskiy Jul 18 '12 at 12:42
  • @DmitryOsinovskiy Sorry, you are right. It is the same problem. Sorry. – thersch Jul 18 '12 at 14:37
0

I'm not sure Resharper can move up and create an abstraction as you want automatically, but you can atleast define it manually like this

In abstract class:

public abstract double A
{
    get;
}

In Sub class:

public override double A
{
    get
    {
        return 3.141;
    }
}

It might be a clearner design to define a new Interface (or use an existing one) and define the property in the interface. That way, your existing subclasses won't have to use override.

Stealth Rabbi
  • 10,156
  • 22
  • 100
  • 176
  • Sorry for my poor question description. I am looking for a tool (e.g. refactoring feature in ReSharper) that do that for me. – thersch Jul 13 '12 at 11:43
  • Ah Yes, I also thought that maybe you weren't sure how to do it manually, either (syntax), so that's what I was trying to show. – Stealth Rabbi Jul 13 '12 at 11:44
0
public interface IInterface {
    string MyProperty { get; }
}

public class Class : IInterface {
    public string MyProperty { get; set; }
}


public abstract class AbstractClass {
    public abstract string Value { get; }
}

public class ConcreteClass : AbstractClass {

    private string m_Value;
    public override string Value {
        get { return m_Value; }
    }

    public void SetValue(string value) {
        m_Value = value;
    }
}

I hope this will be helpful to you.

Hiren Visavadiya
  • 485
  • 1
  • 3
  • 15