Shortcut to create automatic properties using Visual Studio 2008/2010 or Resharper 5

17,217

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.

  1. Turn on Solution-wide error checking (Resharper 5 is considerably better/faster)

  2. Edit Calc referring to all Results properties, leaving the errors reported.

  3. Hit Alt + Shift + PageDown. That will cycle you through all your errors.

  4. Press Alt + Enter and choose Create Property and use the Auto-Property choice in Results, then hit Alt + Shift + PageDown to get back to Calc

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.

Share:
17,217

Related videos on Youtube

Piers Myers
Author by

Piers Myers

C# .net developer at a central bank.

Updated on April 24, 2022

Comments

  • Piers Myers
    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 the Results 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 the Calc class?

  • Piers Myers
    Piers Myers about 14 years
    I 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
    Piers Myers about 14 years
    I 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
    David M about 14 years
    Yes. 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
    Piers Myers about 14 years
    Ah, 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
    Piers Myers about 14 years
    Hmm, I seem to have to do: Alt-Enter, Down, Enter, Tab, Down, Down, Enter - 7 steps :)
  • atconway
    atconway almost 12 years
    The prop shortcut appears to work native to VS.NET 2010 as well without Resharper to create auto-properties.