Shortcut to create automatic properties using Visual Studio 2008/2010 or Resharper 5
Solution 1
Sounds like you'd like to stay in the Calc
class and create your properties in Results
in one go.
Here's what you do.
Turn on Solution-wide error checking (Resharper 5 is considerably better/faster)
Edit
Calc
referring to allResults
properties, leaving the errors reported.Hit Alt + Shift + PageDown. That will cycle you through all your errors.
Press Alt + Enter and choose Create Property and use the Auto-Property choice in
Results
, then hit Alt + Shift + PageDown to get back toCalc
Solution 2
For Resharper 4/5, type prop
then tab, then fill in the required parameters.
Solution 3
As soon as you've pressed Alt + Enter to create the property, and then Enter to accept the data type, you actually get a drop-down list of ways of implementing it if you're on a recent version of ReSharper. One of them is auto property.
Related videos on Youtube
Comments
-
Piers Myers about 2 years
I have a class that contains a load of properties that contain results of some calculations e.g:
public class Results { public double Result1 { get; set; } public double Result2 { get; set; } }
In a different class I am doing calculations to populate the above properties, e.g:
public class Calc { private Results Calc() { Results res = new Results(); res.Result1 = ... some calculation res.Result2 = ... some other calculation res.Result3 = ... // not yet defined in 'Results' class return res; } }
When I am writing the
Calc
class,Result3
will be highlighted in red as it is not yet defined in theResults
class.Currently I am using the Resharper ALT + Enter shortcut, selecting "Create Property 'Result3'" which will create the following code int the
Results
class:public double Result3 { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } }
Which I need to manually change to:
public double Result3 { get; set; }
Then I use the CTRL + Shift + Backspace shortcut to take me back to the
Calc
class.How can I easily create automatic properties in the
Results
class if they are not yet defined directly from theCalc
class? -
Piers Myers about 14 yearsI am using Resharper 5 and I only see four options when I Alt-Enter on it: create field ... create property ... Change all ... Change all local ...
-
Piers Myers about 14 yearsI know about the 'prop' template but that means I have to go to the Results class and create it manually. I want to be able to do this as I am writing the Calc class.
-
David M about 14 yearsYes. Create property. Then it creates it and takes you to it. The data type will be highlighted. Press enter to accept the data type. Then you see the choice of property implementation.
-
Piers Myers about 14 yearsAh, I see that now, thanks for highlighting that feature. I can use it to remove one step. Would still like to do this all in as few key presses as possible.
-
Piers Myers about 14 yearsHmm, I seem to have to do: Alt-Enter, Down, Enter, Tab, Down, Down, Enter - 7 steps :)
-
atconway almost 12 yearsThe
prop
shortcut appears to work native to VS.NET 2010 as well without Resharper to create auto-properties.