Encapsulate Field


It's common to have a private field in your object from which you need to create a property. These fields might have been built as private because they were only used internally to the object. Alternatively, a developer may have simply defined a public field instead of encapsulating it as a property. In either case, if you need to make an actual property out of a field, you can do so with the Encapsulate Field refactor operation.

Accessing Encapsulate Field

The Encapsulate Field operation allows you to quickly generate properties from a given field. Properties, of course, allow you to protect the field from direct access and to know when the given field is being modified or accessed. To encapsulate a field, you simply position your cursor over the field and select the Encapsulate Field option from the Refactor menu. You can also do so from the context menu (right-click) or the Class Designer.

Tip

To invoke the Encapsulate Field operation from the keyboard, first position your cursor on the field that you want to encapsulate. Next, play the keyboard chord Ctrl+R, Ctrl+F.


The Encapsulate Field Dialog Box

The Encapsulate Field dialog box, shown in Figure 8.22, allows you to set a few options for this refactor. First, it presents the field you are refactoring in a read-only text box. Next, it allows you to define a name for the new property. The good news is that the tool will try to name your property correctly. For example, if you have a private field named _score, the tool will choose Score for the property name by default.

Figure 8.22. The Encapsulate Field dialog box.


An additional option on this dialog box is the choice of which references you would like to have updated. This refers to existing references to the field. Suppose you have a public field. This field may be called both from within the object that defines the field as well as by other, external objects. You may want to force external callers to use the new property. For this, you would use the External setting. In this case, the object that contains the field would still reference the local, private field (and not the property). Setting the Update Reference option to All results in both the external and internal callers using the property.

When you apply the encapsulation, the tool changes your internal field to private (if it was not already private) and then generates a property. The property includes both get and set accessors for the field. If the field was declared as read-only, the encapsulation generates only a get accessor.

Let's look at the code. Suppose you have the following field declaration:

private int _rating;


You want to encapsulate this private into a public property. Figure 8.22 shows the selected options for the encapsulation. The code that is generated is as follows:

private int _rating; public int Rating {     get { return _rating; }     set { _rating = value; } }


In addition, internal callers to the field (the field was private) are now updated to use the property internally (due to the All selection for the Update References option).




Microsoft Visual Studio 2005 Unleashed
Microsoft Visual Studio 2005 Unleashed
ISBN: 0672328194
EAN: 2147483647
Year: 2006
Pages: 195

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net