0 Replies - 2924 Views - Last Post: 20 January 2015 - 06:49 AM

#1 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6504
  • View blog
  • Posts: 14,355
  • Joined: 02-June 10

A pair of property snippets

Posted 20 January 2015 - 06:49 AM

This first part is a simply drag-drop block of code that I keep in my Toolbox. When first making a class I drop at at the bottom to handle the INotifyPropertyChanged interface. This block uses the pre-processor define directive to act correctly if you compiling against .NET 4.5 versus an earlier version. If you're not familiar with keeping code in the toolbox... Select a block from your code file and drag it to the General tab of the toolbox. Then right-click and choose Rename to give it a meaningful name. When you want it again, just drag it out. Its that easy.

Attached Image
		#region INotifyPropertyChanged Members
#define RUNNING_NET45 // Put this as first line of file

		public event PropertyChangedEventHandler PropertyChanged;
		// .NET 4.5 implementation doesn't require the property to send its name
		// Requires using System.Runtime.CompilerServices;
		private void NotifyPropertyChanged([CallerMemberName]string prop = "")
			if (PropertyChanged != null)
				PropertyChanged(this, new PropertyChangedEventArgs(prop));
		// Pre .NET 4.5 implementation
	   private void NotifyPropertyChanged(string propertyName)
			if (PropertyChanged != null) 
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

The second part of this is an actual .snippet to add to Visual Studio. Its shortcut is nuprop.
With this snippet you only have to provide the property name {tab} property type {enter} and you're done.

Attached Image

If you aren't familiar with actual .snippets, make a new text document on your desktop. Paste in the code below. Save. Change the extension from .txt to .snippet - Copy it into Visual Studio's snippet directory which for Visuals Studio 2013 should be:
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC#\Snippets\1033\Visual C#\
The restart Visual Studio so it will load the new snippet at launch.

<?xml version= "1.0" encoding= "utf-8" ?>
<CodeSnippets  xmlns= "http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet ">
	<CodeSnippet Format= "1.0.0 ">
			<Title> New Property</Title>
			<Shortcut> nuprop</Shortcut>
			<Description> Code snippet for a property that raises PropertyChanged event in the setter (to assist in INotifyPropertyChanged implementation).      </Description>
			<Author> Clint StLaurent</Author>
				<SnippetType> Expansion</SnippetType>
					<ID> type</ID>
					<ToolTip> Property type</ToolTip>
					<Default> string</Default>
					<ID> property</ID>
					<ToolTip> Property name</ToolTip>
					<Default> MyProperty</Default>
			<Code Language= "csharp ">
				#region $property$
				private $type$ m_$property$; 
	public $type$ $property$     
							//if($property$ == null) $property$ = new $type$();
							return m_$property$; }  
		if (m_$property$ == value) return;
		m_$property$ = value;
				#endregion $property$


This post has been edited by tlhIn`toq: 20 January 2015 - 08:51 AM

Is This A Good Question/Topic? 3
  • +

Page 1 of 1