Results 1 to 2 of 2

Thread: VB.NET IntelliSense Property Snippet

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    VB.NET IntelliSense Property Snippet

    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.
    Last edited by dday9; Apr 21st, 2023 at 09:06 AM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: VB.NET IntelliSense Property Snippet

    Thanks dday9

    In addition to manual editing there is a Snippet Editor which make it much easier.



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width