Major Sándor
Major Sándor

Reputation: 51

Visual Studio refactoring variable type

Is it possible to refactor a variable type just like its name in Visual Studio?

Upvotes: 4

Views: 3470

Answers (2)

Mike
Mike

Reputation: 171

No, unfortunately with a vanilla Visual Studio instance there isn't a way to refactor a variable type. it would be almost impossible for automation to do it, at least if you mean change a string to an int for example.

Consider this refactor

        string str = "I'm a string";

        //Refactor to int would be

        int str = "I'm a string";

The above would then cause an error you would have to manually fix. however when using var you can refactor to explcit types.

        var str = "I'm a string";

        //Refactor to Explicit would be

        string str = "I'm a string";

You can however right click a variable and click find all references which will perhaps be helpful in at least identifing all instances where you will have to rename and clicking each will take you straight to them.

Upvotes: 3

Michał Turczyn
Michał Turczyn

Reputation: 37367

The closest you can get is to find all references to a variable and then go through every occurence of variable and change sorrounding code to match new datatype (if necessary).

enter image description here

Upvotes: 2

Related Questions