In case you are not familiar with snippets, they automatically populate code if you type a keyword and press Tab or if you manually insert a snippet by right-clicking in the code file and selecting "insert snippet".
This snippet will automatically create a property for you, check if the value has changed, and if so raise an overridable OnChanged method and also raise a Changed event.
This is the snippet:
Code:
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Advanced Properties</Title>
<Author>David Day. AKA - dday9</Author>
<Description>Creates a property with an overridable OnChanged method and Changed event.</Description>
<Shortcut>Property</Shortcut>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>MemberName</ID>
<ToolTip>Replace with the private member name.</ToolTip>
<Default>_newProperty</Default>
</Literal>
<Literal>
<ID>PropertyName</ID>
<ToolTip>Replace with the property name.</ToolTip>
<Default>NewProperty</Default>
</Literal>
<Literal>
<ID>PropertyType</ID>
<ToolTip>Replace with the property type.</ToolTip>
<Default>String</Default>
</Literal>
</Declarations>
<Code Language="VB">
<![CDATA[Private $MemberName$ As $PropertyType$
Public Property $PropertyName$ As $PropertyType$
Get
Return $MemberName$
End Get
Set(value As $PropertyType$)
If (Not $MemberName$.Equals(value)) Then
$MemberName$ = value
On$PropertyName$Changed(EventArgs.Empty)
End If
End Set
End Property
Protected Overridable Sub On$PropertyName$Changed(e As EventArgs)
RaiseEvent $PropertyName$Changed(Me, e)
End Sub
Public Event $PropertyName$Changed As EventHandler]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
To use the snippet follow these instructions: Tools > Code Snippets Manager... > Import... > navigate to the snippet file select it > Open > OK.
Now anytime you want to create a property type the keyword Property and hit the tab key twice(assuming that this snippet appears at the top of the list otherwise use your arrow keys to navigate to the snippet) and the following code will be generated for you:
Code:
Private _newProperty As String
Public Property NewProperty As String
Get
Return _newProperty
End Get
Set(value As String)
If (Not _newProperty.Equals(value)) Then
_newProperty = value
OnNewPropertyChanged(EventArgs.Empty)
End If
End Set
End Property
Protected Overridable Sub OnNewPropertyChanged(e As EventArgs)
RaiseEvent NewPropertyChanged(Me, e)
End Sub
Public Event NewPropertyChanged As EventHandler
I have the member name and property name set as substitutes so when you change _newProperty and NewProperty it will change throughout the rest of the auto-generated code too.