Sling Alibi | ASP.NET Tip 4. Auto-generating variables in Visual Studio 2010
Bing

ASP.NET Tip 4. Auto-generating variables in Visual Studio 2010

by asli 20. August 2009 15:00

For more tips like this, LIVE and IN PERSON, come see us at the MSDN Roadshow !

 

Why declare variables (by hand)?  Long gone are the days where no one bothered with Option Explicit or carelessly left variables undefined.  It is a common , albeit sometimes tedious, practice to declare variables before assigning values, even if you are doing it all with one line of code.  However, for the cases where you want to keep your variable declarations separate; Visual Studio 2010 has an auto-generation feature where you can immediately assign variables, and have VS2010 automatically create the declaration line for you.

Simply type in a line of code that uses a variable that you have yet to declare. Immediately, you’ll see a red squiggly when the design-time background compiler (introduced in VS 2008 SP1) realizes that this variable declaration doesn’t yet exist.

   1: blogauthor = "asli";

Immediately IntelliSense will give you a red squiggly line below the variable; indicating that it is unrecognized in the current context of code that you are working with. Visual Studio 2010 IntelliSense Unrecognized field

Simply right click on that squiggly and select Generate—>Field, and Visual Studio will automatically recognize the data type – in this case it recognizes that the value is a String – and will automatically create the declaration for you. 

Important note: The declaration will be created at the very end of your code, so if you have a lot of lines of code, it may not show up in your current view pane. Scroll down to the bottom of your code to find the declaration and move it to a correct location. It would be nice to have this generation occur at the top of the file, where typically member variables would belong; although parsing through Using statements and mid-code inserts make it harder to find the declaration.Visual Studio 2010 Generate Field 
Visual studio 2010 field

Technorati Tags: ,,

Tags: , ,

ASP.NET Tips

Comments

Add comment


IMPORTANT NOTE: Comments not directly related to the content in the blog entries on my site are charged a $100 fee per advertisement/comment if submitted to this site.


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Based on BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

© 2010 | SlingAlibi | Content Copyright | Comment Policy | Privacy Policy

Calendar

<<  July 2010  >>
MoTuWeThFrSaSu
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

View posts in large calendar

RecentComments

Comment RSS

Blogroll

Download OPML file OPML

Most comments