Tuesday, June 1, 2010

Using Auto-Properties and Traditional Properties with VB.NET in VS.NET 2010

Once you being using VS.NET 2010 as a VB.NET user, you will soon see the neat feature of Auto-Properties which C# users have enjoyed since VS.NET 2008. I will not go too much into them as the feature has been covered exhaustively across blogs and sites. In a nutshell the Auto-Property feature provides a short hand way of defining properties without having to fully write out the Get and Set code, along with the backing variable.

But what if you do need a more advanced property, where the data is manipulated? How do I get back to the more explicit long-hand format of a property if I need it? At least in VS.NET 2008, after finishing the definition of the property and its type and pressing <enter> the template was generated for me, so how do I recreate this in VS.NET2010?

The answer is to type in 'prop' (without quotes) which will show an Intellisense menu of built in snippets including the needed 'Property' code (shown below):

Type in 'prop' and hit the tab key twice to produce the long-hand explicitly defined version of the Property. In fact even this is 1 up from VS.NET 2008 as it creates the backing variable right on top of the property. All you need to do is tab through the fields to modify the values as needed. The long-hand version of the property is displayed below:

No comments:

Post a Comment