Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to instantly change name of variable for all references in project

I am working on some game in VS10, and I want to change the name of some variable, because I want to expand the game, however there is so much references to it, that I don't know from where to start. Is there any hotkey in VS10 that will do this in a moment for me? I know there is NetBeans hotkey CTRL+H, but this doesn't work for VS10.

like image 902
Takarakaka Avatar asked Jul 05 '12 02:07

Takarakaka


People also ask

How can I change all occurrences of a variable name at the same time in my program?

Place the cursor inside a variable you want to rename and press Ctrl + R . Then type other name and editor will change all occurrences of that variable. Save this answer.

How do you change all variables at once or code?

If you select a variable/method and hit F2, you can edit the name and it will change every instance of that variable's name throughout the entire current working project.

How do I change the variable name in all places in VS Code?

Press F2 and then type the new desired name and press Enter. All usages of the symbol will be renamed, across files.

How do I change multiple variable names in eclipse?

Select the variable you with to change, right click, and select Refactor->Rename. This will highlight all instances of the variable. Changing the variable name is automatically propagated to all of the instances. Thanks.


2 Answers

First, any variable should always be declared and used in the smallest of scope.

In VS2010, whenever you rename a variable, it will prompt you whether you want all references to this variable is to be so renamed. A little red bar will appear underneath the name where you changed it. Move your mouse to that bar and an icon will appear with "Options to update references to the renamed object". You can choose between renaming all right away or with preview.

(The ability to rename all references to the renamed variable disappears when you go and rename another different variable. Perhaps there is a way to go back to an older rename but I have not found it.)

I tried renaming a public property of a class. The automatic renaming works too for references to that public property, at least when both are within the same code file.

like image 101
Old Geezer Avatar answered Oct 20 '22 17:10

Old Geezer


As Retired Ninja said in his comment, you pretty much need add-ons for Visual Studio (like Visual Assist X).

Installing Visual Assist will give you a menu like this:

enter image description here

... where the "Find References" button searches for all references to that variable/function/namespace/etc and replaces it (not just a plain text search).

Other alternative is to open the "Find and Replace" window (press Ctrl+F), and click "Quick Replace". From there you can do a plain text search and replace (there's also wildcard and regex support (under "Find Options->Use:), if that helps).

like image 28
Salami Avatar answered Oct 20 '22 18:10

Salami