Skip to main content

Create Setter Method

Creates a setter method for a private field and optionally replaces all field assignments with the calls to the newly created method.

#Purpose

It’s a common situation when you need value validity checks or value pre-processing when assigning to a private field. As the result, you may face the code that has the same or nearly the same value processing code before each assignment. To avoid code duplication in such cases, use the Create Setter Method refactoring. It automatically creates the setter method for the selected private field and optionally replates all the assignments with the setter method calls.

#Availability

Available from the context menu or via shortcuts:

  • when the caret is on the private field name within the field declaration statement.

#Options

  • You can specify where the setter method should be created. The options are: at the top of the parent code block, below the field declaration or at a location chosen with a target picker.

#Example

private string myField;
Private myField As String

Result:

private string myField;
internal void SetMyField(string myField)
{
    this.myField = myField;
}
Private myField As String
Friend Sub SetMyField(ByVal myField As String)
    Me.myField = myField
End Sub

#Animation

rsCreateSetterMethodCSharp