Easy Properties in Visual Studio

In a Visual Studio 2012 class file, type “prop” and then press tab twice.  There are a few options, but at least check out “prop” and “propfull”.  This will create a template getter/setter property for you!  When you modify the datatype and the variable names, they’ll update in both.  Try it!

Properties are especially useful in Web Development for accessing Query Strings.  In my opinion, Query Strings should never be accessed directly, but only through properties.  That way you can validate the data!  Like this:

public int ProjectId
{
	get
	{
		int temp = -1;
		object o = Request.QueryString["id"];				
		if (o != null)
		{
			int.TryParse(o.ToString(), out temp);
		}
		return temp;
	}
}

There tons of other Code Snippets in Visual Studio that can make development easier.  You can see all the code snippets that are currently installed, plus their location on disk, by clicking Tools/Code Snippets Manager.  If you’re not using them, you’re not getting the full benefits of Visual Studio!


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *