Refactoring with Encapsulate Field is useful when you need to restrict actions other users can perform on a member field. Encapsulation involves providing methods to read and write the field rather than letting users access the field directly. The accessor methods are typically called "get" and "set" methods and have public visibility while the field they encapsulate becomes private.
If you have an integer field called
and need to restrict access to it, hover over the declaration of the field and select Encapsulate Field from the refactoring menu.
Accessors are created and named automatically, and the original field remains intact. The parameter type of the set method and return type of the get method are the type of the original field.
Accessors Remain in Header for C/C++
For C/C++, the accessor methods are placed in the header with the declaration and can remain in the header for inline execution. If you prefer implementations to be in a source file, follow Encapsulate Field with two invocations of another refactoring: Move Implementation to Source File
. You see only declarations in the header.
... and implementations are moved to a source file.
Use the Navigate
commands of Visual Assist X
to jump between the header and source after moving implementations. Immediately after a move, Alt+Left returns you to the header. Alt+Right returns you to the implementation.
Format of Accessors
The names of the accessor methods are generated automatically. They are capitalized, non-hungarian versions of the member field. Leading and trailing underscores are stripped.
You can modify the format of the accessors by editing the VA Snippet
Refactor Encapsulate Field
. There are separate VA Snippets for C++, C# and VB.
The VA Snippets entries use special characters expanded only when encapsulating a field. These special characters are:
|$SymbolName$||Name of member field|
|$SymbolType$||Type of member field|
|$GeneratedPropertyName$||Generated name of accessor methods|
If you delete a VA Snippets entry and invoke Encapsulate Field, a default entry is created for you.
Updating References to Use Accessors
Making code use new accessors is accomplished separately from encapsulation. You should pass an argument to an accessor to set the value of a field, and use a return value to get its value. Statements guarding your field are moved typically to the body of the accessor, and constructs such as increment must be rewritten.
While the updating of references is not automated, use Find References
to locate all places in your code which require updating.
If you want to force callers outside of your class to use the accessors, change visibility of the accessors to private.
If you want to force code within your class to use the accessors, rename the member field after updating references so compiling of existing code fails to resolve references to the old name. After verifying with Find References
the only references to the old name are those within the accessors themselves, eliminate your old name with Rename
Encapsulate Field is available only from a declaration in C++, and only from a definition in C# and VB. The refactoring is not available from references to symbols.
Encapsulation of an array or char *
may require subsequent modification of the method that writes the array, e.g. use of memcpy() or strcpy().