Change default grouping of private variables with connected properties

ReSharper newbie here, playing with it against an existing .NET (C#) solution. So far, I'm pretty impressed with all the refactoring suggestions. There's just one *teensy* style where my opinion differs, and I can't figure out how to change the default behavior. I'm sure there's a way, I just can't come up with the right search incantation.

Consider the following (ultra-simplified) construct:

private Foo _myVar = null;
public Foo MyVar
{
  get
  {
    if(_myVar == null)
      _myVar = GetFoo();
    return _myVar;
  }
}

By default, R# will shift the private variable to the top of the class, along with all the other private variables. *I* prefer to keep the private variable right next to the property that controls it. Ignoring for the moment the argument about which way is "right", is there an easy way to persuade R# to do what I want? Assuming there is, what would I lose if I chose to enforce this (I assume I have to lose something in the compromise).

Brad.

3 comments
Comment actions Permalink

I still have ReSharper 5.1.
There is a member layout template file in ReSharper->Options->Languages->C#->Type Members Layout.
Maybe you find something you can need there.

0
Comment actions Permalink

I tried following with my ReSharper 5.1.
1. Open dialog ReSharper->Options->Languages->C#->Type Members Layout.
2. Deselect "Use DefaultPattern"
3. Comment out the instance field entry:

   <!--instance fields-->
<!--

    <Entry>
      <Match>
        <And>
          <Kind Is="field"/>
          <Not>
            <Static/>
          </Not>
        </And>
      </Match>
      <Sort>
        <Readonly/>
        <Name/>
      </Sort>
    </Entry>
-->

4. Add fields to properties entry:

    <!--properties, indexers-->
    <Entry>
      <Match>
        <Or>
          <Kind Is="property"/>
          <Kind Is="indexer"/>
          <Kind Is="field"/>
        </Or>
      </Match>
    </Entry>

A short test with two property-field-pairs was successfull. But I'm a novice in that pattern format.

0
Comment actions Permalink

Yup, that did the trick. The referenced pattern is identical in my v6 copy, so the solution will work with the latest version of ReSharper.

Many thanks!
Brad.

0

Please sign in to leave a comment.