Sunday, October 24, 2010

Auto generation of Properties (Shortcut keys)

In this post I give you some useful tips for auto generation of the properties.The code snippet which is quite useful are
  1. prop
  2. propa
  3. propdp
  4. propfull
  5. propg

1- prop :
when you type prop in the visual studio editor and hit tab it will create property of int type with the name MyProperty, which you can see in the Image 1 as shown below. You can see in the image that the data type and the name of the property are of yellow background , mean you can change the data type and the name of the property.

Image 1

2- propa :
The real benefit of using the code snippet is when you write propa and press tab key, you can see the code generated  in the Image 2, here you can see that data type is int and the name of the property is GetMyProperty. In the Image 2 you can see the default focus is on the data type of the property and all the reference of the data type in the generated code are highlighted with dotted rectangle as you can see in the Image 2. When you change the data type and press tab key all the highlighted data types are changed to the data type which you entered in this case I have changed it to the string data type.
Image 2
Now it time to change the name of the property. As you can see in the Image 2 that property is the GetMyProperty. When focus is on the GetMyProperty then all the occurrence of the MyProperty are highlighted with dotted rectangle now change the MyPrperty in GetMyProperty to whatever name you want to give, I have changed it to FirstName and hit tab key all the occurrence of  MyProperty is changed to FirstName. Which you can see in the Image 3 as below.
Image 3
The last thing you have to do is provide the name of the class who is owning this property and you can provide it in the ownerclass which is underline red in the Image 2 and Image 3.

3- propdp :
When you write propd and press the tab key then code for the dependency property is generated which can be seen in the Image 4.  Here you can see the default data type is int and the default name of the property is MyProperty.

Image 4
Now you can change the data type and the dependency property name which is described in propa for the attached property.

4- propfull :
When you type propfull and press the tab key then it will generate the code which is shown in the image 5, here you can see that it has private variable and public property, by default the data type of the variable is int and the name of the variable is myVar and the property name is MyPropery.
Image 5
As you can see in the Image the data type, the variable name and the property name are background of yellow mean you can change them by changing the data type first and press tab key then the variable name and press key and at the end property name and press key, you don't need to use the mouse for this, all this can be done by using the tab key.

5- propg :
When type propg and hit the tab key the code generated is shown in the Image 6, here you can see that property is generated with private setter.
Image 6



As we use more properties in our applications it is quite easy to remember these code snippet and generate code for the properties , attached properties and dependency properties.

All and any comments / bugs / suggestions are welcomed!


1 comment:

Anonymous said...

That was a pretty sweet little tidbit, thanks!