Blog - Title

Visual Studio 2013 Current Status

  • The Ultimate Visual Studio Tips and Tricks Blog

    Searching Your Favorites in Vista

    • 1 Comments

    Okay so I figured out how to put the .url extension in the advanced indexing options so you can search your favorites only to find out that, hey, you don't have to do all that crap.  If you want to search your favs in IE you just type in a part of the name and it shows up.  Talk about a DUH moment.  So if you haven't played with it much, I suggest you try out the seriously cool search features in Vista.  As for me, I'm off to the Windows Help site to brush up on my searching...

    Searching Favorites in Vista

  • The Ultimate Visual Studio Tips and Tricks Blog

    Comment and Uncomment Code

    • 9 Comments

    Keyboard:  CTRL + K, CTRL + C (comment); CTRL + K, CTRL + U (uncomment)
    Menu:  Edit -> Advanced -> Comment Selection; Edit -> Advanced -> Uncomment Selection
    Command:  Edit.CommentSelection; Edit.UncommentSelection
    Versions:  2008,2010
    Published:  4/13/2010
    Code:  vstipEdit0047

     

    Download the seriously cool Tip of the Day Extension to get the daily tips delivered to your Start Page!

     

    Sometimes it's the simple stuff we forget about.  So I present to you the classic Comment and Uncomment Selection.  Naturally, you have the Comment and Uncomment buttons:

    image

     

    And, of course, we have the Menu items:

    image

     

    But it's the keyboard shortcuts that really rock!  These will, predictably, comment or uncomment lines of code for you.  So, let's say you have some code you want commented out.  Just select it:

    image

     

    Then press CTRL + K, CTRL + C (in this example):

    image

     

    Voila!  It's commented out.  Okay, great, but what if you don't want to use the mouse?  No problem!  Just hold ALT + SHIFT + [UP or DOWN ARROW] to do a vertical selection (NOTE:  In VS2008 you have to go right or left one character before you can go up or down for vertical selection):

    image

     

    Then press CTRL + K, CTRL + U (in this example):

    image

     

    And there you go!  Comment and Uncomment actions anytime you want!

  • The Ultimate Visual Studio Tips and Tricks Blog

    Turn on Line Numbers

    • 19 Comments

    Menu:  Tools -> Options -> Text Editor -> All Languages -> General -> Display
    Versions:  2008,2010
    Published:  3/7/2010
    Code:  vstipEdit0025

     

    Line numbers are not on by default.  To turn on line numbers just go to Tools -> Options -> Text Editor -> All Languages -> General -> Display and check Line numbers:

    image

  • The Ultimate Visual Studio Tips and Tricks Blog

    Visual Studio 2012 Color Theme Choices

    • 2 Comments

     

    The other day I had a customer complain about not liking the Light and Dark color themes that come out of the box with VS2012.  I’m always up for more choices with color schemes so thought I would make sure you all knew about the options for color schemes.  There are actually many, many ways you “could” change your fonts and colors but I’ll show you what I consider to be the top two methods in no particular order.

     

    Visual Studio Color Theme Editor

    http://visualstudiogallery.msdn.microsoft.com/366ad100-0003-4c9a-81a8-337d4e7ace05

    image

     

    One of the more popular extensions we created after VS2012 launched was the Color Theme Editor.  it comes with pre-set themes as well as the ability to customize your themes any way you see fit.  Since it is an extension, you can obtain this feature quickly by following the link to the extension and then clicking the Download link:

    image

     

    Next just click the Open button to install the extension:

    SNAGHTML1819303

     

    Now just follow the on-screen instructions to finish the installation.

     

     

    Using the Theme Editor

    To use the editor you can simply choose a theme by going to Tools | Change Color Theme and selecting a pre-set theme:

    image

     

    Notice that just below Change Color Theme is Customize Colors which allows you to create custom color sets as well.  It’s not very intuitive so let me walk you though a little bit of it.  First you have to pick an existing theme to copy from and then give the custom theme a name and click the Create button:

    image

     

    Now you will be presented with a dizzying array of options you can modify to create a custom look and feel to your environment:

    image

     

     

    DANGER:  It appears that the Color Theme Editor doesn’t like it when you import settings through the IDE and will use the imported settings instead of the expected settings when changing themes.  My advice is to create a custom theme and set it as the current theme then import settings as desired (shown below) so you don’t get messed up themes.

     

     

     

    StudioStyles

    If you haven’t been to http://studiostyl.es/ yet then you have missed out on some seriously cool styles:

    image

     

     

     

    Getting a Style

    Let me walk you though how to use one of these styles.  First, you need to find a style you like.  I suggest you pick from the All-Time Favourites section.  My personal favorite for quite some time has been WekeRoad Ink:

    image

     

     

    Once you select the style you want then select the proper version of Visual Studio and click the download button:

    image

     

    This will give you a .VSSETTINGS file with just font and color information that you can import into the IDE by going to Tools | Import and Export Settings:

    image

     

     

    Choose Import Selected Environment Settings then click the Next button:

    SNAGHTML1a7fb18

     

     

    You should absolutely make sure you BACK UP YOUR CURRENT SETTINGS then click Next:

    SNAGHTML1a9ec4a

     

     

    Use the Browse button to find the .VSSETTINGS file you found then click Next:

    SNAGHTML1adc2ca

     

     

    Click the Finish button on the current screen:

    SNAGHTML1ae836a

     

     

    Choose Close on the last screen and you should have your new color theme:

    image

     

     

    Undoing Any Changes

    You can import your previous settings to change the theme back to any colors you had before.  For more information see my previous post here:

    http://blogs.msdn.com/b/zainnab/archive/2010/07/15/importing-or-changing-your-environment-settings-vstipenv0022.aspx

  • The Ultimate Visual Studio Tips and Tricks Blog

    Turn Off the Uppercase Menu in Visual Studio 2012

    • 41 Comments

    I’ve been seeing a ton of posts lately on this feature so thought I would share here as well.  I also like to give credit where credit is due so I did a search and it looks like the earliest mention of this ability is by Richard Banks in his post 6\1\2012 here:  http://www.richard-banks.org/2012/06/how-to-prevent-visual-studio-2012-all.html  If anyone knows of an earlier one done by someone else let me know but this looks like the first mention of it.  If I were a betting man I would say he most likely used Process Explorer to make this discovery which is my favorite tool for finding things like this.

     

     

    Mixed Case is Your Friend

    Okay so you really, really don’t like the Uppercase menu in VS2012 because you feel like it is shouting at you:

    image

     

     

    You would rather go back to mixed case for your menu:

    image

     

     

     

    Not a problem! Let’s see how:

    WARNING:  Making changes to the registry can cause unexpected results so do any changes at your own risk!

     

    Manual Registry Change

    Open the registry editor and go to HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\11.0\General\
    (For Windows 8 Desktop Express go to HKCU\Software\Microsoft\WDExpress\11.0\General) //special thanks to msm8bball for the update
    (For Web Express go to HKEY_CURRENT_USER\Software\Microsoft\VSWDExpress\11.0\General)



    1. Create a new DWORD value called SuppressUppercaseConversion set to 1
      image
    2. Restart Visual Studio and you should see the change

    NOTE: Watch your spelling if you are typing this manually.

     

     

    PowerShell Goodness

    If you like to “simplify” this a bit you can use PowerShell to run a nice script to make this happen.  However, if you haven’t used PowerShell before this may not be faster for you.

    1. Go to the Start button and type powershell and select the Windows PowerShell Entry:
      image
    2. In the PowerShell window copy the script below and paste it in then press Enter:
      Set-ItemProperty -Path HKCU:\Software\Microsoft\VisualStudio\11.0\General -Name SuppressUppercaseConversion -Type DWord -Value 1

      image
    3. Restart Visual Studio and enjoy!
  • The Ultimate Visual Studio Tips and Tricks Blog

    Calling a PowerShell Script From Your .NET Code

    • 18 Comments

     

    Well, Mike F. reminded me to write this article on 5/29 and I am FINALLY getting around to doing it.  Also wanted to give a shout out to Max T. who provided some inspiration on this one as well.  For the code, I borrow very heavily from the article written by Jean-Paul Mikkers found at: 

    http://www.codeproject.com/KB/cs/HowToRunPowerShell.aspx

     

    NOTE:  There is an async version of his code that he does as a follow up.  I don't use it because I want to remove extra code noise to focus on the act of calling the script itself.  If you want to take a stab at the async version (and there are LOTS of good reasons to do so) then you can go here:

    http://www.codeproject.com/KB/threads/AsyncPowerShell.aspx 

     

    So, on to the goodness!

     

    First, you need to make sure you have Visual Studio (any version) installed and have PowerShell installed.  You can get PowerShell from here:

    http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx

     

    You will also need to do this (WARNING:  MAJOR SECURITY ISSUE HERE AND THIS IS JUST FOR TESTING SO DON'T DO THIS ON PRODUCTION MACHINES):

    http://www.cubiczone.com/Articles/tabid/65/EntryID/17/Default.aspx

     

     

     

    1.   Now, let's crank out a simple PoweShell script that we are interested in calling.  We will call a simple script that takes a couple of numbers and returns the sum of those numbers.  This may seem overly simplistic but it is an easy way to demonstrate a complete round-trip between our app and the script without getting bogged down in extra BS that comes with a fancier script.  I'll call the script AddItUp.ps1 and it is included in the source code download, just put it anywhere you can get to easily.  Feel free to dig into the guts of it later on but for now just assume it does what we need it to do. 

     

    Here is the code for the script if you just want to make your own real quick:

     

    # begin

    function AddStuff($x,$y)
    {
       $x + $y
    }

     

    AddStuff 6 5

    # end

     

    NOTE:  Some inspiration and just a cool site for scripts came from http://www.powershellpro.com just as an fyi

     

     

     

    2.  Test the script by opening PowerShell and navigating to the directory where it is and typing what you see in the graphic, you should get the expected result.

    image

     

     

     

     

    3.  Okay!  We have a script that works but now what?  Well we have to call that puppy from our code so let's create a project and get ready to make our magic happen.  Crank out a new Windows App for us to use.  Call the Project CallMeCS or CallMeVB depending on your language.

     

     

    image

     

     

     

     

    4.  For the interface, just gimme a button and a label.  Resize the form a bit so we don't have wasted space.  Real simple stuff...

     

    image

     

     

     

    5.  Double-click on the button to go into our code.

     

    C#:

    image

     

    VB:

    image

     

     

     

    6.  Now we need to add an assembly that is one of a set we got with our install of PowerShell.  You can find these assemblies at C:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0

     

    image

     

    7.  Right-click on your project and choose Add Reference...

     

    image

     

     

    8.  Select the Browse Tab and locate these assemblies then add a reference to the System.Management.Automation.dll

    NOTE:  If you want to dig deeper into the contents of this namespace, you can check it out here:  http://msdn.microsoft.com/en-us/library/system.management.automation(VS.85).aspx

     

    image

     

     

    9.  Now that we have our reference we need to add some using/imports statements to make getting to the classes we want to use easier.  Make sure to put these at the top of your code page outside any other code. 

     

    C#:

    using System.Collections.ObjectModel;
    using System.Management.Automation;
    using System.Management.Automation.Runspaces;
    using System.IO;

     

    VB:

    Imports System.Collections.ObjectModel
    Imports System.Management.Automation
    Imports System.Management.Automation.Runspaces
    Imports System.Text
    Imports System.IO

     

     

    10.  Okay, this next part is a little funkier.  While I liked the code that Mikkers had, I wanted to be able to load up a file from my file system and use it instead of just putting code into a textbox.  That created some VERY interesting new challenges but the end result worked out well.  So, to that end, we will create two helper methods:  RunScript and LoadScript.  RunScript is the code essentially unchanged from Mikkers' article and LoadScript is my helper function that will load the contents of a script file and return a string.

     

     

    11.  Let's begin with the RunScript method.  We will add this method to the Form1 class to make our life easier. 

     

    C#:

    private string RunScript(string scriptText)
       {
           // create Powershell runspace
           Runspace runspace = RunspaceFactory.CreateRunspace();

           // open it
           runspace.Open();

           // create a pipeline and feed it the script text
           Pipeline pipeline = runspace.CreatePipeline();
           pipeline.Commands.AddScript(scriptText);

           // add an extra command to transform the script output objects into nicely formatted strings
           // remove this line to get the actual objects that the script returns. For example, the script
           // "Get-Process" returns a collection of System.Diagnostics.Process instances.
           pipeline.Commands.Add("Out-String");

           // execute the script
           Collection<PSObject> results = pipeline.Invoke();

           // close the runspace
           runspace.Close();

           // convert the script result into a single string
           StringBuilder stringBuilder = new StringBuilder();
           foreach (PSObject obj in results)
           {
               stringBuilder.AppendLine(obj.ToString());
           }

           // return the results of the script that has
           // now been converted to text
           return stringBuilder.ToString();
       }

     

    VB:

    ' Takes script text as input and runs it, then converts
        ' the results to a string to return to the user
        Private Function RunScript(ByVal scriptText As String) As String

            ' create Powershell runspace
            Dim MyRunSpace As Runspace = RunspaceFactory.CreateRunspace()

            ' open it
            MyRunSpace.Open()

            ' create a pipeline and feed it the script text
            Dim MyPipeline As Pipeline = MyRunSpace.CreatePipeline()

            MyPipeline.Commands.AddScript(scriptText)

            ' add an extra command to transform the script output objects into nicely formatted strings
            ' remove this line to get the actual objects that the script returns. For example, the script
            ' "Get-Process" returns a collection of System.Diagnostics.Process instances.
            MyPipeline.Commands.Add("Out-String")

            ' execute the script
            Dim results As Collection(Of PSObject) = MyPipeline.Invoke()

            ' close the runspace
            MyRunSpace.Close()

            ' convert the script result into a single string
            Dim MyStringBuilder As New StringBuilder()

            For Each obj As PSObject In results
                MyStringBuilder.AppendLine(obj.ToString())
            Next

            ' return the results of the script that has
            ' now been converted to text
            Return MyStringBuilder.ToString()

        End Function

     

     

    12.  Now we want to add in our LoadScript method to make getting the script into a variable easier.

     

    C#:

    // helper method that takes your script path, loads up the script
            // into a variable, and passes the variable to the RunScript method
            // that will then execute the contents
            private string LoadScript(string filename)
            {
                try
                {
                    // Create an instance of StreamReader to read from our file.
                    // The using statement also closes the StreamReader.
                    using (StreamReader sr = new StreamReader(filename))
                    {

                        // use a string builder to get all our lines from the file
                        StringBuilder fileContents = new StringBuilder();

                        // string to hold the current line
                        string curLine;

                        // loop through our file and read each line into our
                        // stringbuilder as we go along
                        while ((curLine = sr.ReadLine()) != null)
                        {
                            // read each line and MAKE SURE YOU ADD BACK THE
                            // LINEFEED THAT IT THE ReadLine() METHOD STRIPS OFF
                            fileContents.Append(curLine + "\n");
                        }

                        // call RunScript and pass in our file contents
                        // converted to a string
                        return fileContents.ToString();
                    }
                }
                catch (Exception e)
                {
                    // Let the user know what went wrong.
                    string errorText = "The file could not be read:";
                    errorText += e.Message + "\n";
                    return errorText;
                }

            }

     

    VB:

    ' helper method that takes your script path, loads up the script
        ' into a variable, and passes the variable to the RunScript method
        ' that will then execute the contents
        Private Function LoadScript(ByVal filename As String) As String

            Try

                ' Create an instance of StreamReader to read from our file.
                ' The using statement also closes the StreamReader.
                Dim sr As New StreamReader(filename)

                ' use a string builder to get all our lines from the file
                Dim fileContents As New StringBuilder()

                ' string to hold the current line
                Dim curLine As String = ""

                ' loop through our file and read each line into our
                ' stringbuilder as we go along
                Do
                    ' read each line and MAKE SURE YOU ADD BACK THE
                    ' LINEFEED THAT IT THE ReadLine() METHOD STRIPS OFF
                    curLine = sr.ReadLine()
                    fileContents.Append(curLine + vbCrLf)
                Loop Until curLine Is Nothing

                ' close our reader now that we are done
                sr.Close()

                ' call RunScript and pass in our file contents
                ' converted to a string
                Return fileContents.ToString()

            Catch e As Exception
                ' Let the user know what went wrong.
                Dim errorText As String = "The file could not be read:"
                errorText += e.Message + "\n"
                Return errorText
            End Try

        End Function

     

    13.  Finally, we just need to add some code for our button's click event.

     

    C#:

    private void button1_Click(object sender, EventArgs e)
            {
                // run our script and put the result into our textbox
                // NOTE: make sure to change the path to the correct location of your script
                textBox1.Text = RunScript(LoadScript(@"c:\users\zainnab\AddItUp.ps1"));
            }

     

    VB:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'run our script and put the result into our textbox
            'NOTE: make sure to change the path to the correct location of your script
            TextBox1.Text = RunScript(LoadScript("c:\users\zainnab\AddItUp.ps1"))
    End Sub

     

    14.  That's it!!  You should be able to run your code and you should get this:

    image

     

     

    Don't sweat it if you think this is a lot to type.  I have included the source code for you to use.  Enjoy!

  • The Ultimate Visual Studio Tips and Tricks Blog

    Visual Studio 2012 New Features: Compatibility (aka Project Round-Tripping)

    • 4 Comments

    image

     

    Now that Visual Studio 2012 is pretty well baked I thought it would be good to start revisiting some of my favorite features again and introduce new ones.  Since project round-tripping is one of my most favorite features I thought we would start with it. 

     

    The Dark Ages

    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 for a long, long time.  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 Information Age

    Visual Studio 2012 changes the game.  You can now create projects in Visual Studio 2010 with Service Pack 1 then open those projects in Visual Studio 2012 and THEN open the project up again in VS2010 SP1.  This works the other way, too!  You can create projects in VS2012 (targeting Framework version 4 or less) then open the project in VS2010 SP1 and THEN open it up in VS2012 again. In other words, we now have project round-tripping capability so you can work with the latest features but still keep the solution compatible with team members using an 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 compatible project (see fine print below) in 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 2012 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 VS2010 SP1 and enjoy project round-tripping.

     

    The Fine Print

    As you probably guessed by now this doesn’t work with every project type and there are some limitations based on the project type you use.  For the full list of project types and limitations you can go here:

    http://msdn.microsoft.com/en-us/library/hh266747(VS.110).aspx

     

    Here is an edited list of the ones you are most likely to use:

    Type of Project

    Compatibility

    C#/Visual Basic Silverlight 4 or 5 Application or Class Library

    If you allow Visual Studio to update the project automatically, you can open it in either Visual Studio 2012 RC or Visual Studio 2010 with SP1.

    C#/Visual Basic Webform or Winform

    You can open the project in both Visual Studio 2012 RC and Visual Studio 2010 with SP1.

    Coded UI test

    If you allow Visual Studio to update the project automatically, you can open it in both Visual Studio 2012 RC and Visual Studio 2010 with SP1. For more information, see Upgrading Tests from Earlier Versions of Visual Studio.

    Model-View-Controller framework

    If the Model-View-Controller (MVC) project uses version 3 or 4, you can open this project in Visual Studio 2012 RC. By default, Visual Studio 2010 with SP1 supports only MVC 2 and 3, and Visual Studio 2012 RC supports only MVC 3 and 4.

    For information about how to automatically upgrade from MVC 2 to MCV 3, see ASP.NET MVC 3 Application Upgrader. For information about how to manually upgrade from MVC 2 to MVC3, see Upgrading an ASP.NET MVC 2 Project to ASP.NET MVC 3 Tools Update. If your project targets the .NET Framework 3.5 with SP1, you must retarget your project to use the .NET Framework 4.

    MSI setup (.vdproj)

    You can’t open this project in Visual Studio 2012 RC because it doesn't support that project type. We recommend that you use InstallShield Limited Edition for Visual Studio (ISLE), a free deployment solution that directly supports most Windows platforms and application runtimes. ISLE also supports importing data and settings from Visual Studio Installer projects. For more information, see Windows Installer Deployment.

    Office 2010 VSTO

    If the project targets the .NET Framework 4, you can open this project in both Visual Studio 2012 RC and Visual Studio 2010 with SP1.

    SharePoint 2010

    You can open the project in both Visual Studio 2012 RC and Visual Studio 2010 with SP1.

    SQL Server 2008 R2 Express

    If SQL Server 2008 R2 Express is installed on the computers that are running Visual Studio 2012 RC and Visual Studio 2010 with SP1, you can open the project in both of those versions of Visual Studio.

    SQL Server Report Project

    You can open the project in both Visual Studio 2012 RC and Visual Studio 2010 with SP1. For local mode only (that is, when not connected to SQL Server), you won’t get the design-time experience for controls that are associated with the viewer in Visual Studio 2010, but the project will function correctly at runtime.

    Caution note Caution

    If you add a feature that’s specific to Visual Studio 2012 RC, the report schema will be upgraded automatically, and you will no longer be able to open the project in Visual Studio 2010 with SP1.

    Unit tests

    You can use Microsoft Test Manager 2010 with SP1 in Visual Studio and Visual Studio 2012 RC to open tests that were created in either of these versions. For more information, see Upgrading Tests from Earlier Versions of Visual Studio.

    Visual C++

    If Visual Studio 2012 RC and Visual Studio 2010 with SP1 are installed on the same computer, you can open the project in both of those versions of Visual Studio. For more information, see How to: Upgrade Visual C++ Projects to Visual Studio 2012.

    Visual Studio 2010 web

    If you allow Visual Studio to upgrade the project automatically, you can open it in both Visual Studio 2012 RC and Visual Studio 2010 with SP1.

    Visual Studio 2010 Database (.dbproj)

    If you convert the project to a SQL Server Data Tools Database project, you can open it in Visual Studio 2012 RC. However, Visual Studio 2012 RC doesn’t support the following artifacts:

    • unit tests

    • data-generation plans

    • data-comparison files

    • custom rule extensions for static code analysis

    • server.sqlsettings

    • .sqlcmd files

    • custom deployment extensions

    • partial projects (.files)

    If you install SQL Server Data Tools, you can open the project in Visual Studio 2010 with SP1 after the migration. For more information, see Microsoft SQL Server Data Tools.

    VS 2010 SQL Server Data Tools

    You can open this project in both Visual Studio 2012 RC and Visual Studio 2010 with SP1.

    Visual Studio Lab Management

    You can use Microsoft Test Manager in Visual Studio 2010 with SP1 or Visual Studio 2012 RC to open environments that were created in either of these versions. However, your version of Microsoft Test Manager must match your version of Team Foundation Server before you can create environments. For more information, see Upgrading Lab Management Components from Visual Studio 2010.

    Visual Studio SDK/VSIX

    If you upgrade the project, you can open it in Visual Studio 2012 RC only. For more information, see How to: Upgrade Visual Studio SDK/VSIX Projects to Visual Studio 2012.

    Windows Communication Foundation, Windows Presentation Foundation

    You can open this project in both Visual Studio 2012 RC and Visual Studio 2010 with SP1.

    Windows Mobile

    You can’t open this project in Visual Studio 2012 RC because it doesn't support the project type.

    Windows Phone

    You can’t open this project in Visual Studio 2012 RC because it doesn't support the project type.

    Other

    You can open most other types of projects in both Visual Studio 2012 RC and Visual Studio 2010 with SP1.

     

    For a more detailed explanation of why this is the case and how the decision making process was done by the VS team make sure to read Richa Prasad’s most excellent blog post here:

    http://blogs.msdn.com/b/visualstudio/archive/2012/03/28/round-tripping-with-visual-studio-11.aspx

     

    Richa does a great job of explaining the overall decisions around this feature as well as showing what happens (and what you can expect to see) in those scenarios where it isn’t as clean. 

     

    For example, here is what you would see when trying to import a setup project that is no longer supported:

    SNAGHTMLa85d81d

     

    And here is the resulting migration report generated by Visual Studio 2012:

    image

     

    And there you have it!  Give project round-tripping a shot yourself and see if you think it’s great, too!

  • The Ultimate Visual Studio Tips and Tricks Blog

    LINQ and Stored Procedures

    • 18 Comments

    Okay so last quarter I was asked if you can call stored procedures using Language Integrated Query (LINQ).  The answer, of course, is YES!  It would be kind of silly if we didn't include this type of functionality. :P

     


    NOTE:  You need the following items to do this example

    SQL Server (pretty much any version will do but this example uses 2005)

    Visual Studio 2008 (Beta 2 was used for this example)

    A little time to sling some code

     

     

    So how exactly DO you call a stored proc using LINQ?  Easy just follow these simple steps:

    1. Create a new Windows Forms Application in Visual Studio 2008, let's call it "LINQ_SP_Coolness"

      image
    2. Next, make sure you have a connection to some data source in your Server Explorer.  You may need to add a new connection.

      image
    3. For the purposes of this discussion I am using the Northwind Database on SQL Server.

      image
    4. Take a look at the existing stored procedures because what you need may already be there.

      image
    5. Aaaaaand since I'm bored watching re-runs tonight, why don't we make our own stored procedure?

      image
    6. Just write a simple SELECT to yank out some data AND remember to save :)

      image
    7. Just to make sure, refresh the stored procedure folder on your data source in Server Explorer to see your shiny, new stored proc listed...

      image  
    8. You might even want to test it :P

      image
    9. To get this to a point we can manipulate it using LINQ we need to have our classes and mappings in place.  Fortunately, this is a VERY easy thing to do.  Just right-click your project, choose Project...Add New Item from the Menu Bar.  Let's add a "LINQ to SQL Classes" item with the default name of "DataClasses1.dbml".

      image
    10. Click and drag the stored procedure from Server Explorer on to the LINQ design surface.

       image
    11. You should see the procedure showing up on the upper right of the design surface.

      image
    12. Now let's slap some controls on our Windows Form.  How about a list box and a button?  Live on the edge!

      image 
    13. Now we need to work with database by establishing a context to it.  Next, we need to create a query expression that treats the result of the stored procedure as a table and query against it selecting only the LastName column.  Just for fun why don't we sort all the last names as well?  Finally, we will take the list of names returned and loop through them adding each one to our listBox as we go along.

      Double-click on the button to code for the Click event and write the following lines of code:
      (NOTE: make sure to substitute your server name in place of "zero_cool" and if you are using SQL Express you need to modify the server piece to read like this "SQLEXPRESS\myserver")

       image
    14. If all goes well, you should get the following result:

      image
    15. And that's how you use a stored procedure with LINQ.  This was a very simple example but if you really want to see a great series on LINQ go to my buddy Benko's site.   He has a webcast series on LINQ that kicks major butt! :)



      [Updated:  added tags]
  • The Ultimate Visual Studio Tips and Tricks Blog

    Change Your Visual Studio Color Scheme

    • 0 Comments

    Menu:  Tools -> Import and Export Settings
    Command:  Tools.ImportandExportSettings
    Versions:  2008,2010
    Published:  8/13/2010
    Code:  vstipEnv0034

     

    Folks I want to get your input on the possible title for the new book.  Give me your comments at http://blogs.msdn.com/b/zainnab/archive/2010/08/09/proposed-book-title.aspx

     

    Ever see a cool set of colors your friend or co-worker has and wish you could get it too?  Ever go to http://studiostyles.info/ and want some of those cool color schemes?

    image

     

     

    Well you can get the colors you want!  Let's walk through how it's done:

     

    Seeing What You Like

    First, you see a seriously cool color scheme on someone's screen or at the Studio Styles site:

    image

     

     

     

    Getting the Goods

     

    On Someone's Computer

    Now that you see what you like, get them to export their Fonts and Colors.  Go to Tools -> Import and Export Settings:

    image

     

     

    Click Next and ONLY export the Fonts and Colors NOTHING ELSE:

    image

     

     

    Click Next then give the settings a cool name and click Finish:

    image

     

     

    On the Studio Styles Site

    Find the style you want and click on it:

    image

     

    Pick your Visual Studio version

    image

     

    Click on "Download this scheme" then follow the instructions in the next section.

     

     

    Changing Your Colors

    Put the settings file on a USB key or somewhere you can get to it from your computer.  While you can put the file anywhere you want on your system, I prefer to put it with the other settings files located at "C:\Users\<user>\Documents\Visual Studio <version>\Settings":

    image

     

     

    Now just go to Tools -> Import and Export Settings on your machine:

    image

     

     

    Click Next.  If you haven't backed up your settings in a while, feel free to do so.  Check out vstipEnv0034 if you want more information on exporting your settings:

    image

     

     

    Pick the settings file that has the color scheme you want:

    image

     

     

    Click Next.  Verify that the file is ONLY importing Fonts and Colors then click Finish:

    image

     

     

     

    Danger Will Robinson

    That's it!  You should have your new colors.  If things get bad (i.e. you get funky colors and don't have a backup) and you need to get the default colors back all you have to do is go to Tools -> Options -> Fonts and Colors and click "Use Defaults".  Be warned this is a nuclear option for your colors and wipes out any custom colors used:

    image

  • The Ultimate Visual Studio Tips and Tricks Blog

    Code Metrics – Cyclomatic Complexity

    • 8 Comments

    SKU: Premium, Ultimate

    Versions: 2008, 2010

    Code: vstipTool0131

     

    When working with code metrics, one of the least understood items seems to be cyclomatic complexity. Essentially, with cyclomatic complexity, higher numbers are “bad” and lower numbers are “good”. We use cyclomatic complexity to get a sense of how hard any given code may be to test, maintain, or troubleshoot as well as an indication of how likely the code will be to produce errors. At a high level, we determine the value of cyclomatic complexity by counting the number of decisions made in our source code. In this tip we will start out with a simple example of cyclomatic complexity to understand the concept quickly then give some additional information on actual usage and suggested limits. Finally, I’ve added a section of citations that can be used to dig deeper into this subject.

     

    Cyclomatic Complexity: A Simple Example

    Cyclomatic complexity is defined as measuring “the amount of decision logic in a source code function” [NIST235]. Simply put the more decisions that have to be made in code, the more complex it is.

    Let’s see it in action. Create a new console application and immediately calculate your code metrics by going to Analyze | Calculate Code Metrics for Solution:

    image

     

    Notice the cyclomatic complexity is at 2 (the lowest value possible). If I add non-decision code, notice the complexity doesn’t change:

    image

     

     

    If I add a decision, the cyclomatic complexity value goes up by 1:

    image

     

    When I change the if statement to a switch statement with 4 decisions to be made then it goes from the original 2 to 6:

    image

     

    Let’s take a look at a larger code base. Here are the metrics from the Tailspin Toys sample code (http://msdn.microsoft.com/en-us/library/aa645517(VS.71).aspx):

    image

     

    Notice that most of the items, as we drill down into the Products_Related class, have a value of 1 but a couple of them have a complexity of 5. By itself this may not be a big deal, but given that most other members have a 1 in the same class I would definitely look closer at those two items and see what is in them. You can do this by right-clicking the item and choosing Go To Source Code from the context menu. Let’s take a closer look at Product.set(Product):

    image

     

    Given all the if statements, we can see why the cyclomatic complexity is at a 5. At this point we may decide that this is an acceptable level of complexity or we might refactor to reduce the complexity.

     

     

    The Magic Number

    As with many metrics in our industry, there is no exact cyclomatic complexity limit that fits all organizations. However, [NIST235] does indicate that a limit of 10 is a good starting point:

    “The precise number to use as a limit, however, remains somewhat controversial. The original limit of 10 as proposed by McCabe has significant supporting evidence, but limits as high as 15 have been used successfully as well. Limits over 10 should be reserved for projects that have several operational advantages over typical projects, for example experienced staff, formal design, a modern programming language, structured programming, code walkthroughs, and a comprehensive test plan. In other words, an organization can pick a complexity limit greater than 10, but only if it is sure it knows what it is doing and is willing to devote the additional testing effort required by more complex modules.” [NIST235]

     

     

    Cyclomatic Complexity and Line Numbers

    As I mentioned in vstipTool130 (“Code Metrics – Lines of Code”), just looking at the number of lines of code by itself is, at best, a very broad predictor of code quality. There is some basic truth to the idea that the more lines of code in a function, the more likely it is to have errors. However, when we combine cyclomatic complexity with lines of code then we have a much clearer picture of the potential for errors.

    As described by the Software Assurance Technology Center (SATC) at NASA:

    “The SATC has found the most effective evaluation is a combination of size and [Cyclomatic] complexity. The modules with both a high complexity and a large size tend to have the lowest reliability. Modules with low size and high complexity are also a reliability risk because they tend to be very terse code, which is difficult to change or modify.” [SATC]

     

     

    Code Analysis

    When using code analysis, the Design Guideline rule sets contain maintainability areas:

    image

     

    Inside the maintainability area is a rule for complexity:

    image

     

    This rule issues a warning when the cyclomatic complexity reaches 25 so it is a good thing to have to keep you from having excessive complexity. You can learn more about the rule here: http://msdn.microsoft.com/en-us/library/ms182212.aspx

     

     

    Special Note on Visual Studio 2010

    There may be differences when calculating code metrics using Visual Studio 2010 that don’t apply to Visual Studio 2008. The online documentation (http://msdn.microsoft.com/en-us/library/ee703787.aspx) gives the following reasons:

    • The function contains one or more catch blocks. In previous versions of Visual Studio, catch blocks were not included in the calculation. In Visual Studio 2010, the complexity of each catch block is added to the complexity of the function.
    • The function contains a switch (Select Case in VB) statement. Compiler differences between Visual Studio 2010 and earlier versions can generate different MSIL code for some switch statements that contain fall-through cases.

     

     

    Putting It All Together

    The bottom line is that a high complexity number means greater probability of errors with increased time to maintain and troubleshoot. Take a closer look at any functions that have a high complexity and decide if they should be refactored to make them less complex.

     

     

    Citations

    [MCCABE5]

    McCabe, T. and A. Watson (1994), Software Complexity (CrossTalk: The Journal of Defense Software Engineering).

     

    [NIST235]

    Watson, A. H., & McCabe, T. J. (1996). Structured Testing: A Testing Methodology Using the Cyclomatic Complexity Metric (NIST Special Publication 500-235). Retrieved May 14, 2011, from McCabe Software web site: http://www.mccabe.com/pdf/mccabe-nist235r.pdf

     

    [SATC]

    Rosenberg, L., Hammer, T., Shaw, J. (1998). Software Metrics and Reliability (Proceedings of IEEE International Symposium on Software Reliability Engineering). Retrieved May 14, 2011, from Penn State University web site: http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.104.4041&rep=rep1&type=pdf

Page 1 of 135 (1,345 items) 12345»