Blog - Title

Visual Studio 2013 Current Status

  • The Ultimate Visual Studio Tips and Tricks Blog

    Using the Call Hierarchy (C# Only)

    • 13 Comments

    Keyboard:  CTRL + ALT + K (anywhere); CTRL + K,T (cursor must be on a member name)
    Menu:  View -> Call Hierarchy
    Command:  View.CallHierarchy; EditorContextMenus.CodeWindow.ViewCallHierarchy
    Versions:  2010
    Published:  1/19/2010
    Code:  vstipTool0005

    For the C# folks this is one heck of a great feature:  The Call Hierarchy.  It allows you to visualize all calls to and from a selected method, property, or constructor.  To see how it works just right-click on any method, property, or constructor in the Editor and select View Call Hierarchy:

    image

    You should get a window similar to this one (amount of information will vary):

    image

    Notice there are "Calls To" and "Calls From" areas related to your selection.  You can expand them out:

    image

     

    When you click on a node in the tree, the Call Sites window updates so you can visit the call if you want to:

    image

     

    You can continue expanding the hierarchy to see more "Calls To" and "Calls From" information:

    image

     

    The best part is you can right-click on a symbol and get all kinds of options:

    image

     

     

     

     

     

    Here are some options you may come across:

    Context Menu Item

    Description

    Add As New Root

    Adds the selected node to the tree view pane as a new root node.

    Remove Root

    Removes the selected root node from the tree view pane. This option is available only from a root node.

    You can also use the Remove Root toolbar button to remove the selected root node.

    Go To Definition

    Runs the Go To Definition command on the selected node. This navigates to the original definition for a method call or variable definition.

    You can also press F12 to run the Go To Definition command on the selected node.

    Find All References

    Runs the Find All References command on the selected node. This finds all the lines of code in your project that reference a class or member.

    You can also use SHIFT+F12 to run the Find All References command on the selected node.

    Copy

    Copies the contents of the selected node (but not its subnodes).

    Refresh

    Collapses the selected node so that re-expanding it displays current information.

  • The Ultimate Visual Studio Tips and Tricks Blog

    Using the WPF Tree Visualizer

    • 13 Comments

    Versions:  2010
    Published:  1/29/2010
    Code:  vstipDebug0004

     

    For those not familiar with WPF Trees, check out this article:  http://msdn.microsoft.com/en-us/library/ms753391(VS.100).aspx

     

    Now.  For those that ARE familiar with them have we got something for you:  WPF Tree Visualizer.  Yes, it started out as a CodePlex project and ended up in the product itself as a visualizer.  But how do you use it? 

     

    1. Enter debug mode using any method you prefer.
    2. Once you are there take a look at either a DataTip, the Watch window, the Autos window, or the Locals window.  For this example, I will use the Autos window.
    3. Choose any control in the Autos window and then click on the magnifying glass way over to the right of the control name.
      image
    4. You will see a drop down list.  Choose the WPF Tree Visualizer to get this dialog:
       image
    5. This thing has a lot of different parts so let's take a look at each one.  First, the Visual Tree shows you the hierarchy of the controls:
      image
    6. Clicking on any particular node of the tree will show you the Rendering view just below:
      image
    7. Also, the selected control will have its properties displayed in the large area to the right:
      image

    8. In, both, the Visual Tree and the Properties area you can search/filter the results by typing into the "Search" or "Filter" textboxes respectively:
      image

    9. WARNING:  Watch out for the results as they may not be what you expect.  See the extra items in the list that don't have the word "context" in them?  How did they get there?  Well, if I scroll to the right and look at other properties you can see how it happened:
      image 

      Currently, there is no way that I am aware of to change this behavior.
  • The Ultimate Visual Studio Tips and Tricks Blog

    Coding Faster: Give Me Your Thoughts

    • 13 Comments

    Folks,

    Just an update on the book.  It appears that it may be over 300 pages as originally planned so I am seeing if that is an issue with the publisher.  Would you have a problem if the book was larger? 

     

    Also, I've gotten another draft of the book as we move along and, just as I have always done, I would like to get your thoughts.  I've attached the PDF for your review.  Let me know your thoughts.  Bear in mind is it a rough draft and some things will change.  For example, "Free Your Document Windows" will become something like "Multi-Monitor Support" as I review the titles. 

     

    Also, I will most likely be trimming out the more trivial tips.  The tricky part is that one person's "trivial" is another person's "cool" so I'm erring on the side of too much information rather than not enough.

     

    Z

  • The Ultimate Visual Studio Tips and Tricks Blog

    Code Metrics – Cyclomatic Complexity

    • 12 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

  • The Ultimate Visual Studio Tips and Tricks Blog

    Keyboard Shortcuts for the Book

    • 12 Comments

     

    Folks,

     

    We just had a meeting the other day and are getting closer to the final draft for the book.  The piece I am most concerned about is how best to convey keyboard shortcut information so it is easy for you to use.  Here is the table structure we were originally thinking:

    image

     

    Notice the actions are on the left and keyboard settings are along the top.  During the discussion I thought it would be more logical to put the keyboard settings along the left and the actions across the top.  My thinking is that when you use the table you will start with your setting and then want to move right along actions you can take.  What do YOU think?  Do you like it the way it is or with the settings and actions switched?

  • The Ultimate Visual Studio Tips and Tricks Blog

    The Immediate Window: Working with Members

    • 12 Comments

    Keyboard:  CTRL + ALT + I
    Menu:  Debug -> Windows -> Immediate
    Command:  Debug.Immediate
    Versions:  2008,2010
    Published:  9/28/2010
    Code:  vstipTool0095

     

    When using with the Immediate Window, you can work with class and object members directly:

     

    Debug

    You can use any method or property as long as it is in context.  So, for example, when you are in debug mode, you can call any method that is in scope:

    image

     

     

    Design

    A less-known feature is you can work with properties and methods while in design mode.  If you have static methods on a class, for example, you can just execute them right away:

    image

     

     

    For object members, obviously, you need to create an instance of the object before working with the members:

    image

     

    WARNING:  When working with members at design-time a build will occur.  This could have unintended consequences so make sure you have experimented with this feature a bit before you use it.

  • The Ultimate Visual Studio Tips and Tricks Blog

    Track Changes in the Editor

    • 12 Comments

    Menu:  Tools -> Options -> Text Editor -> General
    Versions:  2008,2010
    Published:  4/14/2010
    Code:  vstipEdit0048

     

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

     

    image

    To use this feature you need to have "Track Changes" and "Selection Margin" checked. 

     

    Okay, so ever wonder how those colors to the left of your code actually work?

    image

     

    Let's begin with a clean slate.  Notice there are no colors:

    image

     

    Now let me add a couple of lines of code:

    image

     

    So, all new code will turn yellow to show you what part of the document is "dirty" (unsaved).  If we save the code then we get this:

    image

     

    The "clean" (saved) indicator will remain as long as you have the file open.  When you close and reopen the file, this is what you get:

    image

     

    New in VS2010:  There is now an orange indicator that is used to show a change that is different from the saved version.  This was added for the scenario where a user does an undo after a save operation:

    image

     

     

    Here is a grid to help keep the scenarios straight:

    Marker Different from file saved on disk? Different from file that was opened?
    Nothing No No
    Yellow Yes Yes
    Green No Yes
    Orange Yes No

     

    Very cool stuff!

  • The Ultimate Visual Studio Tips and Tricks Blog

    Creating and Using Synchronization Services with Visual Studio 2008

    • 12 Comments

     

    So, thanks to Rob S. for staying on me for this one.   This is a demo that I did during the Heroes Happen Here Launch Events and just absolutely dropped the ball on getting out to folks.  Here I am now trying to redeem myself, so let's get after it.

     

    If you want to learn more about Sync Services you can go here:  http://msdn.microsoft.com/en-us/library/bb726031(SQL.100).aspx

     

    Also, I use the sample databases (old school) for SQL Server in this demo that can be found here:  http://msdn.microsoft.com/en-us/library/ms143221.aspx

     

    You can watch me do this session and other sessions by using the links here:  http://blogs.msdn.com/zainnab/pages/heroes-happen-here-launch-resources.aspx

     

    Specifically, check out the Defy Occasionally Connected Challenges with Smart Client Applications session.

     

     

    Annnnd, away we go...

     

    1. Create a new Windows Application in Visual Studio 2008 (either language is fine) and call it SynchCoolnessCS or SynchCoolnessVB as appropriate:

      image

    2. Resize your from then add a DataGridView and a Button to your form like this:

      image

    3. Set the Anchor property of the DataGridView so that it is anchored to all sides:

       image

    4. Now to the fun part.  We will configure a local database cache to work in our disconnected scenario.
    5. Right-click on your project and choose Add...New Item...

      image

    6. Let's add a Local Database Cache leaving the name set to it's default value:

      image

    7. Now you should be in the Configure Data Synchronization Dialog:

      image

    8. I'm going to assume you don't already have a connection to your database server but if you do, just select it from the dropdown.
    9. For those that don't have a connection already, click the New... button for Server Connection, fill out the appropriate information, then
      click OK

      image

    10. In my case, I chose to use the pubs database on the local server ( . = local server)
    11. The Client Connection information should "automagically" fill in for you and you should now have something like this:

      image

    12. Notice the + Add button in the lower left?  Click on that puppy.  In the Configure Tables for Offline Use Dialog, check the authors table (or
      any other table you want).  Notice the options to the right?  Take a minute to check them out and/or make sure you watch the video for an
      explanation of these.  At this point, just click on the OK Button.

      image

    13. You should notice that your Cached Tables has been updated:

      image

    14. VERY IMPORTANT!!  Make sure you click on the Show Code Example in the lower right corner.

      image

    15. Then click on Copy Code to the Clipboard Button a couple of times to make sure it is definitely copied:

      image

    16. Click on Close and then click on OK to get out of all the dialog boxes.
    17. It should synchronize then present you with a new dialog box (note the DataSet name at the botttom), check the Tables box, then click Finish:

      image

    18. You should now see some new files in Solution Explorer:

      image

    19. Double-click on your button and then paste your code inside the button:

      VB
      image


      CS
      image

    20. Let's pause for a moment to consider what to do if you lose the sample text from the clipboard.  Do you start over? NO!
      All you have to do is Right-Click the .sync file and choose view designer to get back to the dialog where you can copy the
      code again:

      image

    21. Now we have to make the DataGridView aware of your cache.  In Design View, set the source of the DataGridView to the local
      authors table:

      image

    22. You should see something like this:

      image

    23. Now go back into your code and let's finish out what we need to do.

    24. What you do from here depends on your needs.  By default, the data will only synch from the server to the client and
      NOT the other direction.  If that is all you want to do then go back into your code and add this line after the TODO for
      reloading your server data:

    25. VB
      ' TODO: Reload your project data source from the local database (for example, call the TableAdapter.Fill method).
      Me.AuthorsTableAdapter.Fill(Me.PubsDataSet.authors)


      CS
      // TODO: Reload your project data source from the local database (for example, call the TableAdapter.Fill method).
      this.authorsTableAdapter.Fill(this.pubsDataSet.authors);

    26. If, however, you want the data to flow in, say, both directions you need to modify your code to look like this:


      VB
    27. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
              ' Update the dataset
              Me.AuthorsTableAdapter.Update(Me.PubsDataSet.authors)

              ' Call SyncAgent.Synchronize() to initiate the synchronization process.
              ' Synchronization only updates the local database, not your project’s data source.
              Dim syncAgent As LocalDataCache1SyncAgent = New LocalDataCache1SyncAgent()

              ' Set synch direction, default is DownloadOnly
              syncAgent.authors.SyncDirection = Microsoft.Synchronization.Data.SyncDirection.Bidirectional

              ' Synch up
              Dim syncStats As Microsoft.Synchronization.Data.SyncStatistics = syncAgent.Synchronize()

              ' TODO: Reload your project data source from the local database (for example, call the TableAdapter.Fill method).
              Me.AuthorsTableAdapter.Fill(Me.PubsDataSet.authors)
          End Sub


      CS

      private void button1_Click(object sender, EventArgs e)
              {
                  // update the DataSet
                  this.authorsTableAdapter.Update(this.pubsDataSet.authors);

                  // Call SyncAgent.Synchronize() to initiate the synchronization process.
                  // Synchronization only updates the local database, not your project’s data source.
                  LocalDataCache1SyncAgent syncAgent = new LocalDataCache1SyncAgent();

                  // Set synch direction, default is DownloadOnly
                  syncAgent.authors.SyncDirection = Microsoft.Synchronization.Data.SyncDirection.Bidirectional;

                  // Synch Up
                  Microsoft.Synchronization.Data.SyncStatistics syncStats = syncAgent.Synchronize();

                  // TODO: Reload your project data source from the local database (for example, call the TableAdapter.Fill method).
                  this.authorsTableAdapter.Fill(this.pubsDataSet.authors);
              }

    28. That's it!  Run that sucker:

      image


    29. Change Green to something else like Purple:

      image
    30. Click the button.  One of two things will happen:  (1) if you didn't set the direction for the synch it will replace Purple with Green since the direction
      is DownloadOnly (Server to Client only) and therefore doesn't accept updates from the client; or (2) it will appear to do nothing and leave Purple there:

      image
    31. Don't be fooled.  It has synched with the database.  You can verify by going into your authors table and seeing for yourself:

      image
    32. And, my intrepid adventurers, that is how you use ADO.NET Data Synch goodness.  Hope you enjoyed the ride! :)

  • The Ultimate Visual Studio Tips and Tricks Blog

    Using Visual Studio 2008 / VSTO / Outlook to Pull Out RFC 822 Header Data

    • 12 Comments

     

    Mike B. asked me to show how to get header information from emails using VSTO.  So here is a step-by-step tutorial on how to create a Form Region that will show header info:

     

    1. Open up the New Project dialog, choose Office...2007 under Project Types (either language is fine), and click on Outlook 2007 Add-in 

       image
    2. Name the project HeaderStuffCS or HeaderStuffVB, depending on which language you use, then click on OK
    3. Now let's add our Task Pane.  Right-click on the project in Solution Explorer, choose Add... New Item...

       image
    4. Under Categories choose Office then under Template select Outlook Form Region

       image
    5. For the Name, call this ShowHeaderInfo and click on Add
    6. This will start a series of dialogs to create the Form Region
    7. The first dialog just let's us choose to create a new form or use an existing one.  Just go with the default (new form) and click Next.

      image
    8. Now we get to choose our region type.  For this one we will go with Adjoining then click on Next

      image
    9. For the descriptive text and display messages, deselect the Inspectors that are in compose mode option and click Next

      image
    10. Finally, we get to choose which of the eight standard classes we want to use this region with or we can specify a custom class.  We will leave the default setting here (mail message) and click on Finish

      image
    11. You will see ShowHeaderInfo.cs or ShowHeaderInfo.vb in Solution Explorer and a design surface should be visible.  Resize the surface to make room for a couple of controls.

      image
    12. In this case we will throw on a button and a textbox.  Set the multiline property of the textbox to true and resize it a little.  Also, press F4 to bring up the properties and set its ScrollBars property to Vertical.

      image
    13. Now comes the fun part :)  Double-click the button to crank some code for its click event.

      image

      image
    14. To get to the header information that we may want we will use the PropertyAccessor.  Essentially this will give us a way to dig into a variety of different information available in the message.
    15. Type the following inside the click event:

      CS

      // get a reference to our mail item
      Outlook.MailItem curMail = (Outlook.MailItem)this.OutlookItem;
    16. // use the property accessor to get info into our textBox
      textBox1.Text = (string) curMail.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E");


      VB

      ' get a reference to our mail item

      Dim curMail As Outlook.MailItem = Me.OutlookItem

      ' use the property accessor to get info into our textbox

      TextBox1.Text = curMail.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E")

    17. Press F5 to run your project.  It should start up Outlook and when you read any mail you should see your region at the bottom.  Click on the button to see the header information.

      image

    18. Close Outlook when you are done.
    19. That's it!  Congratulations you can now get header information!

     

    At this point you are probably pissed because you don't know what the heck that whole GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E") thing did.  Well, basically there are a variety of ways you can get info using the PropertyAccessor.  Take a look at http://msdn.microsoft.com/en-us/library/bb147567.aspx and you will see a listing of these ways.

     

    You accessed the information described in RFC 822.  Specifically, you dumped out the entire SMTP message envelope.  To do this, I used the "mapi/proptag/{some hex value}" option.  But I needed to know the hex value so where did it come from?  Here is where you can get the values that can be used:  http://msdn.microsoft.com/en-us/library/ms530451.aspx 

     

    If you click on the link for PR_TRANSPORT_MESSAGE_HEADERS it will take you to a description of the value(s) you will get and the values you need to get them.  You will note there is a Property Tag entry where we can get the hex value we use to get our information:

    image

    If we plug the hex into our code, we get: GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E")  and we are in business

     

    Now it's up to you to play with the different values to see what you can get back.  Enjoy! :)

  • The Ultimate Visual Studio Tips and Tricks Blog

    Code Trippin (Red Hot Chili Peppers Would Be Proud)

    • 12 Comments

    So we have a new initiative in the West that my buddy Bags is a part of:  Code Trip.  In addition to being a cool name the "tour" concept really rocks.  Check it out at

    buswrap

    http://thecodetrip.com

Page 5 of 135 (1,345 items) «34567»