PositiveGuy
PositiveGuy

Reputation: 47743

Property shortcut in VS

I type prop then tab in VS 2008 (or whatever VS). I'm using Resharper.

I tab, give it a type, tab again and have to delete the text for the property name before I can start typing it. OK, fine.

Now here is the question, at this point I'm left with this:

public string Maxlength { get; set; }

is there a quick way to get it like this so that I can start filling in my get and set?

public string Maxlength
{
    get{;}
    set{;}
}

for some reason, I'm not good yet with stubbing out props quick. Just have to get the hang of the shortcut or whatever as well as with Resharper.

Upvotes: 1

Views: 1179

Answers (3)

Mark
Mark

Reputation: 10206

ReSharper might be taking over that property snippet. You might want to look at the ReSharper | Live Templates... menu.

Upvotes: 0

tanascius
tanascius

Reputation: 53944

When you want to "fill" an autoproperty go onto the variable name and press Alt+Ins and select "To property with backing fields" - it will result in:

private string m_Maxlength;
public string Maxlength
{
  get { return m_Maxlength; }
  set { m_Maxlength = value; }
}

Or you can change the live template (ReShaper -> Live Templates -> Predefined Templates -> C# -> prop" - then edit:

public $TYPE$ $NAME$ { get {$END$;} set{;} }

Upvotes: 1

Andrew Hare
Andrew Hare

Reputation: 351526

In Visual Studio 2008 the prop snippets have been changed to output automatically implemented properties. To use the older snippet which expands the get and set blocks you will need to create a new snippet to do so.

Here is one I created that is a bit more flexible than the default one that Visual Studio gives you:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>propfull</Title>
            <Shortcut>propfull</Shortcut>
            <Description>Code snippet for creating a property</Description>
            <Author>Andrew Hare</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>type</ID>
                    <Default>String</Default>
                    <ToolTip>property type</ToolTip>
                </Literal>
                <Literal>
                    <ID>fname</ID>
                    <Default>name</Default>
                    <ToolTip>field name</ToolTip>
                </Literal>
                <Literal>
                    <ID>pname</ID>
                    <Default>Name</Default>
                    <ToolTip>property name</ToolTip>
                </Literal>
                <Literal>
                    <ID>access</ID>
                    <Default>public</Default>
                    <ToolTip>property visibility</ToolTip>
                </Literal>
            </Declarations>
            <Code Language="csharp">
                    <![CDATA[$type$ $fname$;

            $access$ $type$ $pname$
            {
                get { return this.$fname$; }
                set { this.$fname$ = value; }
            }$end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

Upvotes: 1

Related Questions