Working with Methods > Creating Methods > Extracting Variables

Extracting Variables
If you click Extract Variable () in the Method ribbon toolbar, the method editor checks the source code at the cursor. If it corresponds to an expression of a certain form, the method editor creates a member field variable and replaces all subsequent uses of the expression with that variable. When you click the button where the method editor can extract a variable, an Extract Variable dialog box opens where you can enter a suitable variable name in the Name field.
For example, enter the code in the following figure and position the cursor on the first occurrence of feature:
Click the Extract Variable button to transform the source code into what the following figure shows:
The expression corresponding to the feature part of the source code has been extracted into the variable mslc1.
In the Methods part of the Preferences dialog box, select the View all code check box under Settings to see that a private member field has been added, as shown below.
If you click the Extract Variable button when the cursor is not at an expression of a supported type, then an error message appears. The expression form that is supported is the one that typically occurs when the model object API is used: a sequence of method invocations on an object such as
<variable name> [ . <method name> (<method arguments (optional)>) ]