I once used a code snippet I saw/found in the past that would turn my single statement into a private/public getter/setter, I've been so far unable to repeat that find since reinstalling my machine.
for example:
private string serverSMTP = string.empty;
I could then press Ctrl k + and turn it into this:
private string serverSMTP = string.Empty;
public string ServerSMTP
{
get { return serverSMTP; }
set
{
serverSMTP = value;
RaisePropertyChanged("ServerSMTP");
}
}
Any ideas on how I can create something to do that or an extension/snippet to take care of it for me? In larger projects this would save me a lot of time.
If you are already using the MVVM Light framework you can install the code snippets that ship with it that will do something similar. Specifically the "mvvminpc" snippet will do what you are looking for, although it will not take an existing field declaration and convert it to a property with a propertychanged notification.
http://mvvmlight.codeplex.com/sourcecontrol/latest#Installer/InstallItems/Snippets/CSharp/mvvmInpc.snippet
Code snippets to speed up the addition of new properties (Visual Studio only):
mvvminpc adds a new bindable property to a ViewModel.
mvvmlocatorproperty adds a new ViewModel to a ViewModeLocator.
mvvmpropa adds a new attached property to a DependencyObject (WPF only).
mvvmpropdp adds a new dependency property to a DependencyObject (WPF only).
mvvmslpropa adds a new attached property to a DependencyObject (Silverlight only).
mvvmslpropdp adds a new dependency property to a DependencyObject (Silverlight only).
put this snippet:
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Shortcut>propn</Shortcut>
<Title>
Notify Property
</Title>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>type</ID>
<ToolTip>Type</ToolTip>
<Default>int</Default>
</Literal>
<Literal>
<ID>field</ID>
<ToolTip>Field name</ToolTip>
<Default>fieldName</Default>
</Literal>
<Literal>
<ID>property</ID>
<ToolTip>Propery Name</ToolTip>
<Default>PropertyName</Default>
</Literal>
</Declarations>
<Code Language="CSharp">
<![CDATA[
private $type$ $field$;
public $type$ $property$
{
get { return $field$; }
set
{
if($field$ != value)
{
$field$ = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("$property$"));
}
}
}
$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
inside a file propn.snippet, in this folder: C:\Users[YOUR_USERNAME]\Documents\Visual Studio 2010\Code Snippets\Visual C#\My Code Snippets
and afterwards you'll be able to use this snippet using the (propn + tab + tab) shortcut.
the snippet xml is very easy to understand on your own, so you can easily adjust it to whatever you need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With