The Extract Parameter refactoring is used to add a new parameter to a method declaration and to update the method calls accordingly.
The following table shows two different ways of extracting a function parameter.
In the first of the examples a new parameter is extracted as an optional parameter. So the corresponding function call doesn't change.
In the second of the examples the parameter is extracted as a required parameter. So the corresponding function call changes accordingly.
| || |
| || |
- In the editor, place the cursor within the expression to be replaced by a parameter.
- Do one of the following:
- Press N/A.
- Choose Refactor | Extract | Parameter on the main menu.
- Choose Refactor | Extract | Parameter from the context menu.
- If more than one expression is detected for the current cursor position, the Expressions list appears. If this is the case, select the required expression. To do that, click the expression. Alternatively, use the Up and Down arrow keys to navigate to the expression of interest, and then press N/A to select it.
- In the Extract Parameter dialog:
- Specify the parameter name in the Name field.
- The Value field, initially, contains the expression that you have selected. Normally, you don't need to change this. (Depending on whether the option Optional parameter is selected or not, this will be the value assigned to the new parameter in the function body or passed to the function in the function calls.)
- If, when extracting a new parameter, you don't want to change the function calls, select the Optional parameter check box. If you do so, the value specified in the Value field will be assigned to the new parameter in the function body. The calls to the function (if any) won't change.
If you want to pass the value (specified in the Value field) to the new parameter through the existing function calls, clear the Optional parameter check box. If you do so, all the function calls will change according to the new function signature; no explicit assignment of the parameter value will be added to the function body.
- If more than one occurrence of the expression is found within the function body, you can choose to replace only the selected occurrence or all the found occurrences with the references to the new parameter. Use the Replace all occurrences check box to specify your intention.
- Click OK.