Encapsulate field

Hi,

when I write a field declaration to my class like

private DateTime lastUpdated = DateTime.Now;

and use R# to encapsulate this field with a property the property is
generated correctly but somewhere else and not directly under the field
declaration. When working with regions the field and property should be
at least in the same region, especially when the regions are named by
logic categories and not "properties", "methods" etc.

I have to jump to the generated code, cut it and paste it were I need or
will have it. A better way would be moving the cursor to the line where
the code should be generated (or directly after that line), open
Encapsulate field dialog with the mouse or a shorcut, choose the field
from a list containing all fields of that class (or all fields not
having properties with the same name in pascal case) and generate the code.

Marco

2 comments
Comment actions Permalink

I quite like the current behaviour - new properties are being created
near already existing properties. In the sample below, a new property
for the field 'field3' would be created right below the other properties.

#region fields
field1
field2
field3
#endregion


#region properties
property1
property2

#endregion


However, one sidenote (slightly OT): If I implement an interface an RS
creates fields for interfaced properties, the order of the fields is
inverted.


Cheers, Philipp



0
Comment actions Permalink

Hello Philipp,
if you name your regions like your example it is ok, of course.


#region fields
field1
field2
field3
#endregion


#region properties
property1
property2

#endregion

But if you name your regions like this it is something different:

#region caches
cacheField1
cacheProperty1
cacheField2
cacheProperty2
cacheField3
cacheProperty3
#endregion

#region services
serviceField1
serviceProperty1
serviceField2
serviceProperty2
serviceField3
serviceProperty3
#endregion

Because may be you want divide this into partial classes MyClass.cs,
MyClass.Caches.cs, MyClass.Services.cs etc.

Marco


However, one sidenote (slightly OT): If I implement an interface an RS
creates fields for interfaced properties, the order of the fields is
inverted.


Cheers, Philipp


0

Please sign in to leave a comment.