Blog - Title

Tips Search
Visual Studio 11 Current Build

  • The Ultimate Visual Studio Tips and Tricks Blog

    Surround with a Code Snippet (C# Only)

    • 0 Comments

    Keyboard:  CTRL + K, CTRL + S
    Menu:  Edit | IntelliSense | Surround With
    Command:  Edit.SurroundWith
    Versions:  2008,2010
    Published:  4/21/2010
    Code:  vstipEdit0052

     

     

    This is one that even people who know about snippets tend to forget.  You can actually put a snippet AROUND existing code.  Assuming you have some code selected:

    clip_image002

     

    Just press CTRL + K, CTRL + S:

    clip_image004

     

    Then type the statement you want to surround the code with.  In this case, I’ll use an “if” statement:

    clip_image006

     

    Hit your TAB key once and you get your result:

    clip_image008

     

    Now you can put in your condition and any additional logic you want.  Pretty cool stuff!

  • The Ultimate Visual Studio Tips and Tricks Blog

    Visual Studio 11 Developer Preview: Code Clone Detection (aka Code Clone Analysis)

    • 0 Comments

    Versions: Visual Studio 11 Developer Preview

     

    Make sure to get your copy of the new book from Sara and me:

    Coding Faster: Getting More Productive with Microsoft Visual Studio

     

     

    <Disclaimer>

    Note: As always with pre-release software, some of the features may not make it into the final version or may change significantly before RTM. Also, although I will only show features that are publicly available, I may be using a slightly older or newer version of the build than you are so there may be slight differences in the feature set I show and the feature set you currently have.

    </Disclaimer>

     

     

    In my travels across the country, with my fellow Evangelist, Clint Edmonson, talking about Visual Studio we often come across great stories to tell. One of our favorite true stories is of a customer that had a web application running very slow. We ran code metrics against it and, sure enough, the Page_Load event had 9,000 lines of code in it. Naturally we were curious so we opened it up to see that it was basically the same if statement copied over and over. Apparently they needed to find out who was coming into the website in order to show customized content and the solution they came up with was this massive set of statements.

     

    For better or worse we have all had code that gets copied throughout our solutions. Until now there was no tool to tell us there were copies and, instead, we had to rely on other metrics such as lines of code to hopefully reveal any code smells. Now, however, we have the new Code Clone Detection (aka Code Clone Analysis) feature.

     

    According to the documentation:

    Code clones are separate fragments of code that are very similar. They are a common phenomenon in an application that has been under development for some time. Clones make it hard to change your application because you have to find and update more than one fragment. Visual Studio can help you find code clones so that you can refactor them.”

    http://msdn.microsoft.com/en-us/library/hh205279%28v=vs.110%29.aspx

     

     

    Specific Clones

    You can find clones of specific code by selecting the segment you are interested in:

    image

     

    Then Right-click on the selection and choose Find Matching Clones in Solution from the context menu:

    image

     

    Visual Studio will search for code clones and produce the result in the new Code Clone Search Results window:

    image

     

    The original line of code is put in a group on its own and then all the matches are put into a different group. You can expand the groups to see the specific locations of the matches:

    image

     

     

     

    Solution Clones

    Besides looking for specific clones you can also just look for code clones for the entire solution. This will search the entire solution for duplicate code and display the results. To use this feature go to Analyze | Analyze Solution for Code Clones:

    image

     

    This creates a result set for the entire solution:

    image

     

    By default it groups and sorts the results by the strength of the match. Exact matches come first then those matches that may be close but not exact come next. As you can see the other terms used are Strong, Medium, and Weak in this example.

     

     

     

    Reviewing Matches

    Once you have the result set, there are a couple of ways you can compare them against each other.

     

    Comparison Tools

    Although I don’t show it here, if you have a comparison tool configured you can Right-click on any two items and select Compare from the shortcut menu. You would know if you have this feature available by going to Tools | Options | Source Control | Team Foundation Server and click on Configure User Tools.

     

     

    Manual Comparison

    If you don’t have a comparison tool you can do manual comparisons between two entries in the list. If the clones are in different files then you can just double-click each one and file tabs will be available for you to look at for comparison:

    image

     

     

    When it comes to comparisons in the same file I’ve only found one good way to accomplish this so far. Granted, I have just started playing with this feature so there might be something coming or something I missed that makes this easier. Here is a series of steps to compare two items in the same file.

     

    First, find the first entry you want to look at and double-click on it to open a file tab and highlight the code segment:

    image

     

    Now make a copy of the current code window by going to Window | New Window:

    image

     

    Next, go to the second entry you are interested in and double-click it. The result should be one code segment on each tab so you can compare the two:

    image

     

    You can do this for as many entries in the list as you like. Just repeat these steps for each entry you want to compare.

     

     

     

    What Is Found

    You are probably curious as to what is found by this tool. The heuristics for finding clones will find duplicates even if the following changes have happened:

    · Renamed identifiers.

    · Insert and delete statements added.

    · Rearranged statements.

     

     

     

    What Is Not Found

    There are some rules for what is not found as well. I have taken this list from the documentation pretty much verbatim.

    · Type declarations are not compared. For example, if you have two classes with very similar sets of field declarations, they will not be reported as clones. Only statements in methods and property definitions are compared.

    · Analyze Solution for Code Clones will not find clones that are less than 10 statements long. However, you can apply Find matching clones in solution to shorter fragments.

    · Fragments with more than 40% changed tokens.

    · If a project contains a .codeclonesettings file, code elements that are defined in that project will not be searched if they are named in the Exclusions section of the .codeclonesettings file.

    · Some kinds of generated code are excluded:

    · *.designer.cs, *.designer.vb

    · InitializeComponent methods

    · However, this does not automatically apply to all generated code. For example, if you use text templates, you might want to exclude the generated files by naming them in a .codeclonesettings file.

     

     

     

    Code Clone Settings and Exclusions

    A settings file is available to configure this feature at the project level. I tried to use it at the solution level but it didn’t work so this is definitely a per-project activity. Currently we have only announced the ability to do exclusions in the file but there will most likely be other elements that are added later on. The file is just XML with a .CODECLONESETTINGS extension. The only requirement for use is that the file exists in the top level directory of the project.

     

    The base elements consist of a CodeCloneSettings element with an Exclusions child:

    image

     

    Within the Exclusions element you can have the following children:

    <File>

    This element is used to indicate files that should be excluded from analysis. Path names can be absolute or relative and you can use wildcards as well. So, for example, to ignore all the C# text template files that have been put in their own directory (called MyTextTemplates) you might have the following:

    image

     

     

    <Namespace>, <Type>, and <FunctionName>

    You can also exclude namespaces, types, and functions. Just like files these items can use absolute names or names with wildcards in them. Here is an example of what it might look like:

    image

     

    Example Scenario

    In the Tailspin Toys sample that I have there is some generated code in the TailSpin.SimpleSqlRepository project that is the bulk of the duplications:

    image

     

    Code clone analysis doesn’t automatically know to ignore text templates so I have created an XML file called TailSpinRepository.codeclonesettings and inserted an entry like this:

    image

     

    Now if I run clone analysis here is the result:

    image

     

    As you can see the results are significantly less than the first time the analysis ran. It’s common to create several exclusions in different projects to weed out noise in the analysis results.

     

     

     

    Finally

    Code Clone Detection is a great new tool to add to your arsenal for improving code quality. Combined with Code Analysis and Code Metrics, this will help quickly find potential issues.

  • The Ultimate Visual Studio Tips and Tricks Blog

    Greatest Hits: Using the New IntelliSense - Pascal Case

    • 0 Comments

    Keyboard:  CTRL + J 
    Menu:  Edit | IntelliSense | List Members
    Command:  Edit.ListMembers
    Versions:  2010
    Published:  2/9/2010
    Code:  vstipEdit0017

     

     

    Have you ever been in a situation where you wanted to use IntelliSense to get a method but there are a TON of methods that start with same word and you have to type almost the entire method name? 

    clip_image002

     

    Well those days are over!  Let's say you want the SetWindowSize Method but really, really don't want to type it out or even scroll down to get the method.  IntelliSense now supports Pascal Case!  All you have to do is type "SWS" and you are all set:

    clip_image004

  • The Ultimate Visual Studio Tips and Tricks Blog

    Greatest Hits: Using Visual Studio 2010 IntelliSense with Keywords

    • 3 Comments

    Keyboard:  CTRL + J
    Menu:  Edit –> IntelliSense –> List Members
    Command:  Edit.ListMembers
    Versions:  2010
    Published:  2/8/2010
    Code:  vstipEdit0016

     

    The one feature we regularly use more than just about anything else in Visual Studio is IntelliSense.  It has been our friend for many years.  Well it just got more friendly!  To show you the new feature, let’s take a look at VS2008 IntelliSense.  Notice when I type Console.Key what happens:

    clip_image002

     

    It does what you would expect it to do and highlights the first (in this case—only) item that begins with the word “Key”.  That’s great but what if I don’t know what I am looking for but I know that it has the word “Key” somewhere in it?  Well, I can go search in the Object Browser, of course OR I can use the new IntelliSense in VS2010.  Watch what happens when i do the same thing in VS2010:

    clip_image004

     

    Voila!  It now shows only those items that have the word “Key” in them AND doesn’t care where the word is in the name of the member!  So not only do it get items that begin with “Key” but I get ANYTHING that has the word “Key” in it. 

  • The Ultimate Visual Studio Tips and Tricks Blog

    Visual Studio 11 Developer Preview: Multiple / Floating Tab Wells

    • 2 Comments

    Versions: Visual Studio 11 Developer Preview

     

     

    Make sure to get your copy of the new book from Sara and me:

    Coding Faster: Getting More Productive with Microsoft Visual Studio

     

    Note: As always with pre-release software, some of the features may not make it into the final version or may change significantly before RTM. Also, although I will only show features that are publicly available, I may be using a slightly older or newer version of the build than you are so there may be slight differences in the feature set I show and the feature set you currently have.

     

    Note: This feature was first introduced in the VS2010 Productivity Power Tools extension found here:

    http://visualstudiogallery.msdn.microsoft.com/d0d33361-18e2-46c0-8ff2-4adea1e34fef?SRC=Home

     

     

    Visual Studio 11 Developer Preview

    Continuing with our look at Visual Studio 11 Developer Preview we will examine the concept of multiple/floating tab wells that was originally created by Radames Cruz Moreno from the VS Platform Team. In VS2010 we introduced the idea of floating document windows. The only problem was, once detached; the window was a completely independent entity. This was great if all you wanted was one window but what if you wanted, say, 20 windows on one screen and 10 on another? Things get pretty out of hand fast when dealing with multiple windows beyond a certain point. Visual Studio 11 Developer Preview comes with the ability to not only detach windows but have them in multiple tab wells to better organize your windows. In this tip I’ll show you how to take advantage of this feature.

     

    Quick Walkthrough

    Open up Visual Studio 11 Developer Preview and get a couple of tabs in the tab well like I have here:

    image

     

    Now click and drag one of the tabs outside the IDE:

    image

     

    You should notice a new window is created, with a new tab well, and the tab is inside the well:

    image

     

    Now you can add more tabs to the new area and treat it just as you would the original tab well:

    image

     

     

    Menu Commands

    I think the hardest part for people to get used to is that certain menu items impact the current active window regardless of where it is. For example, Window | New Window which makes a copy of the current window:

    image

     

    Will work regardless of where the active window is located:

    image

     

    Also, as I am sure you have guessed by now, you can have multiple independent tab wells:

    image

     

    So there you have it! This is a huge advantage that allows you to move and organize your VS windows the way you want.

     

     

    Turning It Off

    Note: When I tried this on my system the change didn’t take effect so you may not be able to turn this feature off in your pre-release build.

     

    If you don’t like this feature it can be turned off by searching on “tabs” in the Quick Launch area (CTRL + ` [back tick]):

    image

     

    Selecting the entry for Environment -> Tabs and Windows:

    image

     

    Then turn off Enable Independent Floating Tab Wells:

    image

     

    This will return you to the VS2010-like experience.

    For those who like menu commands the path to this option is Tools | Options | Environment | Tabs and Windows | Floating Tab Well.

  • The Ultimate Visual Studio Tips and Tricks Blog

    O’Reilly / MS Press Cyber Monday Ebook Deal

    • 0 Comments

     

    Now that I’m published the marketing folks send me all kinds of stuff on book specials.  Most of them are cheezy but this one looks pretty cool so thought I would share.  Here is the tweet they are sending out:

     

    CyberMonday Deal: Save 60% on 60 new and top ebooks! Today only. Shop now: http://bit.ly/s3vkkJ

     

    Naturally the book I did with Sara is on there for like 12 bucks or something as well as many other titles.  Good times Smile

  • The Ultimate Visual Studio Tips and Tricks Blog

    Greatest Hits: Make IntelliSense Transparent

    • 3 Comments

    Keyboard:  CTRL
    Versions:  2008,2010
    Published:  10/17/2010
    Code:  vstipEdit0077

     

    I thought it would be cool to rerun some of the items that many people have really liked in the past.  This is the first of the posts I’ll call the Greatest Hits for folks to get exposure to tips they may have missed the first time around.  With that said, on with the tip…

     

    Sometimes when you are cranking code you find yourself in a situation where IntelliSense is covering up some code you want to see:

    image

     

    You can easily make it temporarily opaque by pressing and holding the CTRL key:

    image

     

    This way you don't have to completely get rid of IntelliSense, see the code, then bring IntelliSense back.  Just press the CTRL key, get the information you need, and move on.

  • The Ultimate Visual Studio Tips and Tricks Blog

    Visual Studio 11 Developer Preview: Preview Tab

    • 2 Comments

    Shortcut: CTRL + ALT + Home (promote); ALT (do not preview in Solution Explorer)

    Versions: Visual Studio 11 Developer Preview

     

    Make sure to get your copy of the new book from Sara and me:

    Coding Faster: Getting More Productive with Microsoft Visual Studio

     

    Note: As always with pre-release software, some of the features may not make it into the final version or may change significantly before RTM. Also, although I will only show features that are publicly available, I may be using a slightly older or newer version of the build than you are so there may be slight differences in the feature set I show and the feature set you currently have.

     

     

    Preview Tab: Common Scenarios

    We all explore code and need move quickly between files. In prior versions of Visual Studio you had to open a file to look at the contents which often resulted in many open files (tabs) after debugging or reviewing some code. The Preview Tab eliminates the need to open certain files when browsing code. Most likely you’ll first encounter the Preview Tab when you are looking at files with Solution Explorer. When you click on a supported file, you will see the contents of that file in the new preview tab:

    image

     

    The point of the preview tab is to let you view the contents of a file without actually opening up a new tab in the tab well. As you look at different files the preview tab only shows the contents of the file you are currently on. This keeps the environment from getting cluttered with open tabs and allows you to focus on only those files that are interesting to you. Solution Explorer isn’t the only place you can use the preview feature. It turns up in several situations where you might need to look at file content. For example, when using Find (CTRL + F) to locate information you will see the preview tab:

    image

     

     

     

    Promoting Previews

    At some point you may decide to promote the preview to an opened tab in the Tab Well so you can do additional work on the file. There are a few ways you can make this happen.

     

    Click the Promote button

    Just click the promote button on the preview and it will open up a tab for that file:

    image

     

    Press CTRL + ALT + HOME

    With the cursor in the file, just press CTRL + ALT + HOME to open a tab for the file you are currently viewing.

     

    Change the File

    While previewing a file, if you make any change to the file it will automatically be promoted to an open tab so that you can make additional changes and do any other actions you need to perform.

     

     

     

    Turning Preview Off

    To see the options you have for using the preview feature, just type preview in Quick Launch (CTRL + ` [back quote]) and click Environment -> Tabs and Windows:

    image

     

    This will take you to Tools | Options | Environment | Tabs and Windows:

    image

     

     

    To turn off the preview feature uncheck the Allow New Files to be Opened in the Preview Tab option. Also notice you can decide if a single click open the files in the preview and/ or use ALT to prevent a file from being previewed when you click on it. While I see the value in using ALT to prevent preview from happening; I haven’t yet found any reason for turning off the single-click option as it, in effect, makes the feature unusable.

     

     

     

    Finally

    The Preview Tab is one of the great new additions to the Visual Studio IDE that, I believe, will be an exceptional productivity enhancement. Try it yourself and let me know what you think.

  • The Ultimate Visual Studio Tips and Tricks Blog

    Docking External Document Windows

    • 0 Comments

    Windows:  CTRL + Double Click on Title Bar
    Menu:   Window | Dock As Tabbed Document
    Command:  Window.DockAsTabbedDocument

    SKU: Professional, Premium, Ultimate
    Versions:  2010, Visual Studio 11 Developer Preview
    Code:  vstipEnv0065

     

     

    In Visual Studio 2010 we introduced the ability to take document windows outside the IDE:

    clip_image002

     

     

    However, the old trick of double-clicking on the Title Bar to dock the window back into the tab well doesn’t work anymore. It turns out that CTRL + Double-Click is the new shortcut to dock document windows back into the IDE:

    clip_image004

     

     

    If you have installed the Productivity Power Tools (http://visualstudiogallery.msdn.microsoft.com/d0d33361-18e2-46c0-8ff2-4adea1e34fef) or have Visual Studio 11 Developer Preview then you have floating tab wells:

    clip_image006

    You can CTRL + Double-Click on the window’s Title Bar to dock all the tabs in the external tab well.

     

    The strange part comes when you have multiple tabs in an external tab well. If CTRL + Double-Click on an individual tab in the external tab well when there is more than one tab it creates a new tab well with only one tab in it. When you CTRL + Double-Click on a tab that is the only one in the current tab well then it will dock it. Not sure if this is intentional behavior or a bug.

  • The Ultimate Visual Studio Tips and Tricks Blog

    Visual Studio 11 Developer Preview - Project Backward Compatibility (Round-Tripping)

    • 8 Comments

    image

     

    Probably one of the most welcome new features in VS11DP is the new project backward compatibility feature also known as project round tripping.  Let’s dig into the details.

     

    The Old Days

    In all prior incarnations of Visual Studio if you had an older version, VS2008 for example, and opened up the project in a newer version, VS2010 in this case, you had the option to convert the project.  If you did the conversion it was a one-way trip.  You couldn’t open the project again in VS2008 if you tried to go back.  This has been the source of a lot of pain and suffering for developers who have to work with multiple versions of Visual Studio.  We want the cool features of the latest IDE but still want others on our team to be able to open the solution in the older version of Visual Studio when they get the files from source control.

     

    The New, Happy Days

    You can now create projects in Visual Studio 2010 with Service Pack 1 then open those projects in Visual Studio 11 Developer Preview and THEN open the project up again in VS2010SP1.  In other words, we now have full project round-tripping capability so you can work with the latest features but still keep the solution compatible with and older version of Visual Studio. 

     

    Putting it to the Test

    Here are the steps you can take to test this for yourself:

    1. Create a project with Visual Studio 2010 with Service Pack 1 and put in a little sample code.
    2. Save and close the solution.
    3. Open the solution (CTRL + SHIFT + O) in Visual Studio 11 Developer Preview and add some more code.  Notice there is no longer a prompt to convert the project that appears.
    4. Save the close the solution.
    5. Open the project up again in VS2010SP1 and bask in the glory of project round-tripping.

     

    Limitations / Observations

    • I know this works with Visual Studio 2010 Service Pack 1 but haven’t tested VS2010 with out the service pack.  I believe it will not work unless you have the service pack but am checking with the team.
    • You can’t use this feature with versions older than VS2010.
    • When you use any feature specific to the new version of Visual Studio, like changing the Framework to the latest version, then the project cannot be opened in the prior version.  I just changed the framework version from 4.0 to 4.5 for a project and saved it without many any other change and was not able to open the solution in VS2010.
Page 1 of 124 (1,238 items) 12345»