Generating a Field and its Property Using Resharper Live Templates

<div class=Section1> <p class=MsoNormal><font size=2 color=navy face=Tahoma>Someone asked me for this template today, so I figured I would just post it. This is a simple template that I use with Resharper to generate my fields and properties. This only applies when I am not using CodeSmith to automatically generate my business entity classes. But when I have to create fields and properties one at a time, I like to use this template. </font></p> <p class=MsoNormal><font size=2 color=navy face=Tahoma> </font></p> <p class=MsoNormal style=’margin-left:.5in’><font size=2 color=blue face=Tahoma>private</font><font color=navy> </font><font color=maroon>$TYPE$</font><font color=navy> </font><font color=maroon>$VARIABLE1$</font><font color=navy>;</font></p> <p class=MsoNormal style=’margin-left:.5in’><font size=2 color=navy face=Tahoma> </font></p> <p class=MsoNormal style=’margin-left:.5in’><font size=2 color=blue face=Tahoma>public</font><font color=navy> </font><font color=maroon>$TYPE$ $VARIABLE2$</font><font color=navy></font></p> <p class=MsoNormal style=’margin-left:.5in’><font size=2 color=navy face=Tahoma>{</font></p> <p class=MsoNormal style=’margin-left:.5in’><font size=2 color=navy face=Tahoma>    </font>get<font color=navy> { </font>return<font color=navy> </font><font color=maroon>$VARIABLE1$</font><font color=navy>; }</font></p> <p class=MsoNormal style=’margin-left:.5in’><font size=2 color=navy face=Tahoma>    </font>set<font color=navy> { </font><font color=maroon>$VARIABLE1$</font><font color=navy> = </font>value<font color=navy>; }</font></p> <p class=MsoNormal style=’margin-left:.5in’><font size=2 color=navy face=Tahoma>} </font></p> <p class=MsoNormal><font size=2 color=navy face=Tahoma> </font></p> <p class=MsoNormal><font size=2 color=navy face=Tahoma>If you have not created Live Templates for Resharper before, go check it out in the Resharper | Options menu and select Live Templates.</font></p> <p class=MsoNormal><font size=2 color=navy face=Tahoma> </font></p> <p class=MsoNormal><font size=2 color=navy face=Tahoma>Available In:</font></p> <p class=MsoNormal><font size=2 color=navy face=Tahoma>I set this setting to “in C# files where member declaration is allowed” so I can enter them at the class level.</font></p> <p class=MsoNormal><font size=2 color=navy face=Tahoma> </font></p> <p class=MsoNormal><font size=2 color=navy face=Tahoma>Abbreviation & Name:</font></p> <p class=MsoNormal><font size=2 color=navy face=Tahoma>I assigned the abbreviation of “fp” to this template and called it “field property”. </font></p> <p class=MsoNormal><font size=2 color=navy face=Tahoma> </font></p> <p class=MsoNormal><font size=2 color=navy face=Tahoma>$TYPE$</font></p> <p class=MsoNormal><font size=2 color=navy face=Tahoma>I wanted the type to be dynamic so I could enter string, int, or ArrayList (for example). And the type has to be used in both the field and in the property. So I used the same placeholder for both the field and the property. I set the macro to “none” . Its pretty generic and I don’t want it to default to a specific type, so that’s fine here. </font></p> <p class=MsoNormal><font size=2 color=navy face=Tahoma> </font></p> <p class=MsoNormal><font size=2 color=navy face=Tahoma>$VARIABLE1$ </font></p> <p class=MsoNormal><font size=2 color=navy face=Tahoma>The first instance of the variable name should be in lowercase. I will type this in myself. I don’t need a macro here but you can set the “suggest a name for variable” macro if  you choose (or some other macro).</font></p> <p class=MsoNormal><font size=2 color=navy face=Tahoma> </font></p> <p class=MsoNormal><font size=2 color=navy face=Tahoma>$VARIABLE2$ </font></p> <p class=MsoNormal><font size=2 color=navy face=Tahoma>The second instance of the variable name should be the same as the first, except that the first letter should be uppercase. So I name the placeholders differently and I assign the macro “value of another variable with the first character in uppercase” to this placeholder and I assign $VARIABLE1$ as the variable that it refers to.</font></p> <p class=MsoNormal><font size=2 color=navy face=Tahoma> </font></p> <p class=MsoNormal><font size=2 color=navy face=Tahoma>Running the Live Template</font></p> <p class=MsoNormal><font size=2 color=navy face=Tahoma>Once the live template is created, I type “fp” and hit the TAB key. This automatically generates the stub for the field and the property. It prompts me for the TYPE, which I entered string. Then it prompts me for the VARIABLE, for which I entered firstName. From these 2 entries it fills in the rest of the template and I end up with the following:</font></p> <p class=MsoNormal><font size=2 color=navy face=Tahoma> </font></p> <p class=MsoNormal><font size=2 color=navy face=Tahoma> </font></p> <p class=MsoNormal style=’text-autospace:none’><font size=2 color=black face=Tahoma>                </font>private<font color=black> </font>string<font color=black> firstName;</font></p> <p class=MsoNormal style=’text-autospace:none’><font size=2 color=black face=Tahoma> </font></p> <p class=MsoNormal style=’text-autospace:none’><font size=2 color=black face=Tahoma>                </font>public<font color=black> </font>string<font color=black> FirstName</font></p> <p class=MsoNormal style=’text-autospace:none’><font size=2 color=black face=Tahoma>                {</font></p> <p class=MsoNormal style=’text-autospace:none’><font size=2 color=black face=Tahoma>                    </font>get<font color=black> { </font>return<font color=black> firstName; }</font></p> <p class=MsoNormal style=’text-autospace:none’><font size=2 color=black face=Tahoma>                    </font>set<font color=black> { firstName = </font>value<font color=black>; }</font></p> <p class=MsoNormal style=’text-autospace:none’><font size=2 color=black face=Tahoma>                }</font></p> <p class=MsoNormal><font size=2 color=navy face=Tahoma> </font></p> <p class=MsoNormal><font size=2 color=navy face=Tahoma> </font></p> <p class=MsoNormal><font size=2 color=navy face=Tahoma>Disclaimer / Notes:</font></p> <p class=MsoNormal><font size=2 color=navy face=Tahoma>Use this at your own risk/pleasure … I do not warranty them </font><font color=navy face=Wingdings>J</font><font color=navy>. Obviously you can change or modify the settings of Live Templates to generate simple or complex code logic. </font></p> </div>

Tweet Post Share Update RSS

Hi, I'm John Papa. I author this blog, create courses for Pluralsight and am a Google Developer Expert and Microsoft Regional Director. I travel speaking at events and train technology thought leaders

Tags:
All