Reputation: 61606
I've settled on the following style of creating properties (with a backing field):
private _firstName;
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
Given that the name of the property is similar to the name of the backing field, I've improved the built in prop
snippet to the following:
<?xml version="1.0"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>prop</Title>
<Author>Frank Rizzo</Author>
<Description>Code snippet for property and backing field - changed one (not the original).</Description>
<Shortcut>prop</Shortcut>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>type</ID>
<ToolTip>Property type</ToolTip>
<Default>int</Default>
</Literal>
<Literal>
<ID>property</ID>
<ToolTip>Property name</ToolTip>
<Default>MyProperty</Default>
</Literal>
<Literal>
<ID>field</ID>
<ToolTip>The variable backing this property</ToolTip>
<Default>myVar</Default>
</Literal>
</Declarations>
<Code Language="CSharp"><![CDATA[private $type$ _$field$;
public $type$ $field$
{
get { return _$field$;}
set { _$field$ = value;}
}
$end$
]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
This works to a degree, but the casing of the backing field and the property name is always the same, where as in my convention, the backing field is camel-cased, where as the property name is pascal-cased.
So my question is this: does the snippet syntax have a way to change the first letter of the property, so that the snippet might comply with my convention?
Upvotes: 5
Views: 1471
Reputation: 5
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<Header>
<Title>Code File Header</Title>
<Author>Муся</Author>
<Shortcut>codehead</Shortcut>
<Description>Гавнокодец</Description>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>Name</ID>
<Default>Header</Default>
</Literal>
<Literal>
<ID>LowerName</ID>
<Default>header</Default>
</Literal>
<Literal>
<ID>Type</ID>
<Default>object</Default>
</Literal>
</Declarations>
<Code Language="CSharp">
<![CDATA[
public $Type$ $Name$
{
get { return $LowerName$; }
set
{
$LowerName$ = value;
OnPropertyChanged("$Name$");
}
}
$Type$ $LowerName$;
]]>
</Code>
</Snippet>
</CodeSnippet>
Upvotes: -2
Reputation: 887275
Unfortunately, this is not possible (yet).
Hopefully, it will be possible in a future version of Visual Studio.
Upvotes: 6