NET Refactor - Insert Const & Insert StringRefactor Multiple Instances of Embedded StringsMany times you will find that a string value is being repeated in a method or class. Not only is this wasteful of resources, it is sloppy coding. Also, developers fall into the habit of using in-line strings when they could be using constants. Constants reside in the "intern pool". Once used, the CLR does not have to recreate them. So, it normally is better to use constants than string variables.
The Insert Const or String feature provides the ability to automatically clean up your methods and classes by moving a selected string value, or any other value that creates a constant or static string. The dialog, shown below, shows a simple example where two long, identical strings are being used in the same method.

If you select a string value, as shown above, and then select the Insert Const or String menu option, found under the Rename menu in NET Refactor, the following dialog will be displayed.

Here, you have a couple of simple steps to follow. First, enter a meaningful name (required) in the place of "newName". Next, if you want to create a string instead of a constant (default), check the String option button. You now have a choice of whether to create the new constant or string at the method level or the class level. Where you place the new object determines the scope of changes. If you create it at the method level, then only the method will be searched for replacing the selection with the name of the new object. If you select the Class Level option, all occurrences of the selected string will be replaced with a reference to the new Const or String variable. So, if you have been using an embedded string constant numerous times, in your Class or Method, all but one of the instances can be removed and the references to the one instance will be updated automatically. Finally, click the Save button to complete the replacement. A message box will be displayed showing the number of replacements made.
The picture, displayed below, shows the completed code in a method. The long embedded string has been converted to a Const. The two references have been changed to point to the new Const. This could have worked at the Class Level for numerous instances of the same string, and all references, in all methods would be updated automatically.

Try NET Refactor Free for 30 days or purchase now by clicking Download or Purchase.
New Features are being added to NET Refactor, Version 2, and as they are added, you can download the new features without additional cost.
Top of Page
|