Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 414
Piers Myers Avatar asked Jun 04 '10 14:06

Piers Myers


People also ask

What is the keyboard shortcut to set ReSharper?

Configure individual keyboard shortcutsChoose Tools | Options from the menu and then go to Environment | Keyboard page. Use the shortcut tables to find the alias of the command, to which you are going to assign a new shortcut.

Which shortcut to create a property declaration with Get and Set?

Here I am Showing You That How You Can Get & Set Property Using Shortcut In Visual Studio. Then it will automatically display intelligence & from that select "prop" & then press TAB key from your keyboard then it will automatically add a code snippet of get & set property.

How do I set hotkeys in Visual Studio?

In the Press shortcut key(s) box, enter the shortcut that you want to use. You can create a shortcut that combines a letter with the Alt key, the Ctrl key, or both. You can also create a shortcut that combines the Shift key and a letter with the Alt key, the Ctrl key, or both.


2 Answers

For Resharper 4/5, type prop then tab, then fill in the required parameters.

like image 149
Neil Barnwell Avatar answered Sep 28 '22 11:09

Neil Barnwell


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

like image 31
Brett Veenstra Avatar answered Sep 28 '22 11:09

Brett Veenstra