0

Is there a way of telling Visual Studio to create the variable it will store the result of a function call into automatically?

So for example... You type:

this.unitOfWork.MySQLRepo.GetUsers();

Then you press some magic button that makes it say this instead:

List<User> [snippet style typeahead] = this.unitOfWork.MySQLRepo.GetUsers();
7
  • 3
    ReSharper can do this Commented Apr 30, 2015 at 11:56
  • is prepending var [myVarname] = (so in the end just var and = on top) really such a pain? Commented Apr 30, 2015 at 11:57
  • @CarstenKönig -- your comment is unhelpful. Commented Apr 30, 2015 at 11:58
  • Is doing the dishes really such a pain? No... But I'll get a dish washer anyway. Commented Apr 30, 2015 at 12:00
  • Even with ReSharper you have to write var and the variable name in this case, ReSharper only helps when the name is predictable like foreach loops or switch. Maybe I miss understood what you want, but do you want something to make up names for the variable? Commented Apr 30, 2015 at 12:06

2 Answers 2

2

This is pretty offtopic for Stack Overflow, but I'm not sure the existing answer is what you were asking for. Here is what ReSharper offers by way of comparison:

I start off with this line of code, with the caret at the end:

enter image description here

I press Alt+Enter, ReSharper's standard combination, and get this menu:

enter image description here

I choose "Introduce variable", and this happens, allowing me to choose either var or List<User> for the type, or in the standard snippet way, just overtype it with something entirely different if I choose:

enter image description here

I hit Tab, and the same thing happens on the generated variable name:

enter image description here

again you can choose from the pre-generated names (which, if you follow standard .NET naming conventions, are usually pretty good - taking into account things like plural/singular returns, lists etc), or overtype it with your own variable name. Hit Tab again and you're back to the caret at the end of the line.

Sign up to request clarification or add additional context in comments.

3 Comments

You may perceive ReSharper as being expensive. I perceive it as being a fantastic tool in my box that has paid for itself many times over.
I assume you've just got the trial... this really is just scratching the surface of what it does!
Yes, when I have more time, I will look further into it. But it's exactly what I wanted man!
1

I know one thing about rehsarper - it is expensive.

I personally have used this Free Snippet Designer and liked it too.

Snippet Designer

enter image description here

  • Access it by opening any .snippet file or going to File -> New -> File -> Code Snippet File It uses the native Visual Studio code editor so that you can write the snippets in the same enviorment you write your code.
  • It lets you easily mark replacements by a convenient righ click menu.
  • It displays properties of the snippet inside the Visual Studio properties window.
  • Supports C#, Visual Basic, JavaScript, HTML, XML and SQL.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.