<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Professional Visual Studio &#187; Tips and Tricks</title>
	<atom:link href="http://www.professionalvisualstudio.com/blog/tags/tips-and-tricks/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.professionalvisualstudio.com/blog</link>
	<description>Tips, Tricks, and Best Practices for professional .NET developers</description>
	<lastBuildDate>Sun, 15 Aug 2010 02:44:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Debugging Tip of the Week #7: The DebuggerNonUserCode and DebuggerStepperBoundary Attributes</title>
		<link>http://www.professionalvisualstudio.com/blog/2008/09/24/debugging-tip-of-the-week-7-the-debuggernonusercode-and-debuggerstepperboundary-attributes/</link>
		<comments>http://www.professionalvisualstudio.com/blog/2008/09/24/debugging-tip-of-the-week-7-the-debuggernonusercode-and-debuggerstepperboundary-attributes/#comments</comments>
		<pubDate>Wed, 24 Sep 2008 14:52:35 +0000</pubDate>
		<dc:creator>Dave Gardner</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Tips and Tricks]]></category>

		<guid isPermaLink="false">http://www.professionalvisualstudio.com/blog/?p=162</guid>
		<description><![CDATA[We&#8217;re quickly approaching the last of the Debugger attributes in the System.Diagnostics namespace. After this week there are only two more to go, but don&#8217;t despair, because I&#8217;ve saved the best till last. However for now let&#8217;s take a look at the DebuggerNonUserCode and DebuggerStepperBoundary attributes. The DebuggerNonUserCode attribute has the same effect as using [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re quickly approaching the last of the Debugger attributes in the <code>System.Diagnostics</code> namespace. After this week there are only two more to go, but don&#8217;t despair, because I&#8217;ve saved the best till last. However for now let&#8217;s take a look at the <strong>DebuggerNonUserCode</strong> and <strong>DebuggerStepperBoundary</strong> attributes.</p>
<p>The <strong>DebuggerNonUserCode</strong> attribute has the same effect as using both the <a href="http://www.professionalvisualstudio.com/blog/2008/08/29/debugging-tip-of-the-week-5-the-debuggerhidden-attribute/">DebuggerHidden</a> and <a href="http://www.professionalvisualstudio.com/blog/2008/09/09/debugging-tip-of-the-week-6-the-debuggerstepthrough-attribute/">DebuggerStepThrough</a> attributes at the same time. In the default Visual Studio configuration, code marked with this attribute will appear as external code in the call stack as shown below. As with the DebuggerStepThrough attribute, breakpoints cannot be set in blocks of code marked with this attribute. Stepping through code will step into any code called by that block of code in the same way it does for the DebuggerHidden attribute.</p>
<p><img title="Call stack with DebuggerNonUserCode" src="http://www.professionalvisualstudio.com/blog/wp-content/uploads/2008/09/debuggernonusercode_applied.png" alt="Call stack showing the effect of the DebuggerNonUserCode attribute" /></p>
<p>Code that is marked with the DebuggerNonUserCode attribute will be visible in the call stack if you disable the Just My Code option under <em>Tools-&gt;Options-&gt;Debugger-&gt;Enable Just My Code (Managed Only)</em>.</p>
<p><strong>DebuggerStepperBoundary</strong> is probably the most obscure of all of the Debugger attributes, as it only comes into effect under quite specific conditions. It is used to avoid a misleading debugging experience that can occur when a context switch is made on a thread within the boundaries of the DebuggerNonUserCode attribute. It is entirely possible in this scenario that the next user-supplied code module stepped into may not actually relate to the code that was in the process of being debugged. To avoid this undesirable situation, the DebuggerStepperBoundary, when encountered under this scenario, will escape from stepping through code to running code.</p>
<p>As with all of the debugger attributes, both the DebuggerNonUserCode and DebuggerStepperBoundary attributes are ignored by the CLR and as such have no affect during normal execution. So you do not need to worry about any implications of leaving any of these attributes in place in production code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.professionalvisualstudio.com/blog/2008/09/24/debugging-tip-of-the-week-7-the-debuggernonusercode-and-debuggerstepperboundary-attributes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Debugging Tip of the Week #6: The DebuggerStepThrough Attribute</title>
		<link>http://www.professionalvisualstudio.com/blog/2008/09/09/debugging-tip-of-the-week-6-the-debuggerstepthrough-attribute/</link>
		<comments>http://www.professionalvisualstudio.com/blog/2008/09/09/debugging-tip-of-the-week-6-the-debuggerstepthrough-attribute/#comments</comments>
		<pubDate>Mon, 08 Sep 2008 23:07:40 +0000</pubDate>
		<dc:creator>Dave Gardner</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Tips and Tricks]]></category>

		<guid isPermaLink="false">http://www.professionalvisualstudio.com/blog/?p=151</guid>
		<description><![CDATA[The DebuggerStepThrough attribute is another of the debugger attributes that should be used carefully and only on well-tested code. Like the DebuggerHidden attribute, when the DebuggerStepThrough attribute is applied to a piece of code, that code is stepped over during debugging. Similar to the DebuggerHidden attribute, breakpoints cannot be set within a block of code [...]]]></description>
			<content:encoded><![CDATA[<p>The <strong>DebuggerStepThrough</strong> attribute is another of the debugger attributes that should be used carefully and only on well-tested code. Like the <a href="http://www.professionalvisualstudio.com/blog/2008/08/29/debugging-tip-of-the-week-5-the-debuggerhidden-attribute/">DebuggerHidden attribute</a>, when the DebuggerStepThrough attribute is applied to a piece of code, that code is stepped over during debugging.</p>
<p>Similar to the DebuggerHidden attribute, breakpoints cannot be set within a block of code marked with the DebuggerStepThrough attribute. However, within the call stack the attributed code will be marked as <em>external code</em>. For example, consider the following code snippet where the DebuggerStepThrough attribute has been set on <code>HiddenMethod</code>:</p>
<pre>private void ClickHandler(object sender, EventArgs e)
{
   HiddenMethod();
}

[DebuggerStepThrough()]
public void HiddenMethod()
{
   Console.WriteLine("Can't set a breakpoint here");
   NotSoHiddenMethod();
}

public void NotSoHiddenMethod()
{
   Console.WriteLine("Can set a breakpoint here!");
}</pre>
<p>If you run this code with a breakpoint set in <code>NotSoHiddenMethod</code>, the call stack will appear as shown below:</p>
<p><img title="Call stack with DebuggerStepThrough" src="http://www.professionalvisualstudio.com/blog/wp-content/uploads/2008/09/debuggerstepthrough_applied.png" alt="Call stack showing the effect of the DebuggerStepThrough attribute" /></p>
<p>Visual Studio 2008 supports the Just My Code option, configurable from the Debugging node in the Options dialog (select Tools &gt; Options). Unchecking this option makes all code contained within your application appear in the call stack, as shown below. This includes designer and other generated code that you might not want to debug. Once this option is unchecked, breakpoints can also be set in blocks of code marked with this attribute.</p>
<p><img title="Just My Code with DebuggerStepThrough" src="http://www.professionalvisualstudio.com/blog/wp-content/uploads/2008/09/debuggerstepthrough_justmycode.png" alt="Call stack showing the effect of the DebuggerStepThrough attribute combined with the Just My Code setting" /></p>
<p>DebuggerStepThrough is very useful when working with code that calls a lot of properties, for example, if you are calling a method that passes in a large number of properties as parameters. If you add the DebuggerStepThrough attribute to all the properties, and then you won’t step into all of the properties when debugging the method call.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.professionalvisualstudio.com/blog/2008/09/09/debugging-tip-of-the-week-6-the-debuggerstepthrough-attribute/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Debugging Tip of the Week #5: The DebuggerHidden Attribute</title>
		<link>http://www.professionalvisualstudio.com/blog/2008/08/29/debugging-tip-of-the-week-5-the-debuggerhidden-attribute/</link>
		<comments>http://www.professionalvisualstudio.com/blog/2008/08/29/debugging-tip-of-the-week-5-the-debuggerhidden-attribute/#comments</comments>
		<pubDate>Fri, 29 Aug 2008 14:18:15 +0000</pubDate>
		<dc:creator>Dave Gardner</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Tips and Tricks]]></category>

		<guid isPermaLink="false">http://www.professionalvisualstudio.com/blog/?p=144</guid>
		<description><![CDATA[Continuing on with an exploration of the various debugger attributes found in the System.Diagnostics namespace, this week I&#8217;d like to present the DebuggerHidden attribute. This is something you probably only want to apply to VERY well tested code. Code marked with the DebuggerHidden attribute will be stepped over during debugging, and the method call will [...]]]></description>
			<content:encoded><![CDATA[<p>Continuing on with an exploration of the various debugger attributes found in the System.Diagnostics namespace, this week I&#8217;d like to present the <strong>DebuggerHidden</strong> attribute.</p>
<p>This is something you probably only want to apply to VERY well tested code. Code marked with the DebuggerHidden attribute will be stepped over during debugging, and the method call will not even appear in the Call Stack window. However, if some code marked with this attribute makes a call to another method, the debugger steps into that method.</p>
<p>It&#8217;s also worth noting that any breakpoints that have been set in code marked with the DebuggerHidden attribute will be ignored. For example, in the following code snippet, a breakpoint can be set in both <code>ClickHandler</code> and <code>NotSoHiddenMethod</code>:</p>
<pre>private void ClickHandler(object sender, EventArgs e)
{
   HiddenMethod();
}

[DebuggerHidden()]
public void HiddenMethod()
{
   Console.WriteLine("Can't set a breakpoint here");
   NotSoHiddenMethod();
}

public void NotSoHiddenMethod()
{
   Console.WriteLine("Can set a breakpoint here!");
}</pre>
<p>If you step through this code, the debugger goes from the call to <code>HiddenMethod</code> in the <code>ClickHandler</code> method straight to the <code>NotSoHiddenMethod</code>. The call stack at this point is shown below, and you can see that <code>HiddenMethod</code> does not appear in the stack.</p>
<p><img title="Call stack with DebuggerHidden" src="http://www.professionalvisualstudio.com/blog/wp-content/uploads/2008/08/debuggerhidden.jpg" alt="Call stack showing the effect of the DebuggerHidden attribute" /></p>
<p>As with all of the System.Diagnostic attributes, the CLR will ignore this, so you will still see the method call in the stack trace of any an exceptions thrown at runtime.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.professionalvisualstudio.com/blog/2008/08/29/debugging-tip-of-the-week-5-the-debuggerhidden-attribute/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Hiding the Output Window</title>
		<link>http://www.professionalvisualstudio.com/blog/2008/08/19/hiding-the-output-window/</link>
		<comments>http://www.professionalvisualstudio.com/blog/2008/08/19/hiding-the-output-window/#comments</comments>
		<pubDate>Tue, 19 Aug 2008 12:41:02 +0000</pubDate>
		<dc:creator>Dave Gardner</dc:creator>
				<category><![CDATA[IDE Settings]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.professionalvisualstudio.com/blog/?p=116</guid>
		<description><![CDATA[Hands up if you think the Output window is annoying in its default configuration. I&#8217;m referring to how every time you run a build the Output window pops up and interrupts you, obscuring half the code window. Sometimes I get the feeling that it was starved for attention as a child There are a couple of [...]]]></description>
			<content:encoded><![CDATA[<p>Hands up if you think the Output window is annoying in its default configuration. I&#8217;m referring to how every time you run a build the Output window pops up and interrupts you, obscuring half the code window. Sometimes I get the feeling that it was starved for attention as a child <img src='http://www.professionalvisualstudio.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p><img title="The Output Window getting in the way" src="http://www.professionalvisualstudio.com/blog/wp-content/uploads/2008/08/outputintheway.jpg" alt="Output Window in the way" /></p>
<p>There are a couple of easy solutions to this problem. Firstly, you can simply stop the Output window from being automatically shown during a build. Open the Options dialog (Tools-&gt;Options) and select <strong>Projects and Solutions</strong>. You will find an option under here called <strong>Show Output window when build starts</strong>. Simply uncheck this option and you will never be interrupted again.</p>
<p>However many people, myself included, have found that the Output window is actually quite useful to have around. If that&#8217;s the case, you may want to have it automatically open, but in a different way. My suggestion is to try changing the Output window to display as a Tabbed Document. Simply right-click on the window title bar and select Tabbed Document as shown below:</p>
<p><img title="Changing the Output window to a Tabbed Document" src="http://www.professionalvisualstudio.com/blog/wp-content/uploads/2008/08/outputtotabbeddocument.png" alt="Output window to Tabbed Document" /></p>
<p>Once you&#8217;ve made this change, the Output window will open at the same level as the active code window. It will still come to the foreground, which I find useful, however to me it feels like it does this in a much more natural and <a title="Coding Horror: Reducing User Interface Friction" href="http://www.codinghorror.com/blog/archives/000866.html">frictionless</a> way. I&#8217;d be very interested to hear whether anyone else prefers this also.</p>
<p><img title="The Output window as a Tabbed Document" src="http://www.professionalvisualstudio.com/blog/wp-content/uploads/2008/08/outputastabbeddocument.jpg" alt="Output window as Tabbed Document" /></p>
<p>To wrap up this post, here&#8217;s a couple more things about the Output window that are useful to know:</p>
<ul>
<li>Press Ctrl+Alt+O to display and bring the Output window into focus;</li>
<li>Press Ctrl+S when the Output window has focus to save the text in the Output window to an external file;</li>
<li> If you do choose to not automatically display the Output window during a build, you can redirect all Diagnostic messages (e.g. Debug.Print) to the Immediate Window, as mentioned in my <a href="http://www.professionalvisualstudio.com/blog/2008/07/24/debugging-tip-of-the-week-1-the-immediate-window/">first Debugging Tip of the Week</a>.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.professionalvisualstudio.com/blog/2008/08/19/hiding-the-output-window/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Debugging Tip of the Week #4: The DebuggerDisplay Attribute</title>
		<link>http://www.professionalvisualstudio.com/blog/2008/08/15/debugging-tip-of-the-week-4-the-debuggerdisplay-attribute/</link>
		<comments>http://www.professionalvisualstudio.com/blog/2008/08/15/debugging-tip-of-the-week-4-the-debuggerdisplay-attribute/#comments</comments>
		<pubDate>Fri, 15 Aug 2008 11:50:46 +0000</pubDate>
		<dc:creator>Dave Gardner</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Tips and Tricks]]></category>

		<guid isPermaLink="false">http://www.professionalvisualstudio.com/blog/?p=125</guid>
		<description><![CDATA[Last week, we started to explore the first of the Debugger attributes that are found in the System.Diagnostics namespace by looking at the DebuggerBrowsable attribute. I&#8217;d like to continue that theme this week with the DebuggerDisplay attribute. No doubt you&#8217;ve seen that hovering your mouse over a variable while you are in Break mode will [...]]]></description>
			<content:encoded><![CDATA[<p>Last week, we started to explore the first of the Debugger attributes that are found in the <code>System.Diagnostics</code> namespace by looking at <a href="http://www.professionalvisualstudio.com/blog/2008/08/07/debugging-tip-of-the-week-3-the-debuggerbrowsable-attribute/">the DebuggerBrowsable attribute</a>. I&#8217;d like to continue that theme this week with the <strong>DebuggerDisplay</strong> attribute.</p>
<p>No doubt you&#8217;ve seen that hovering your mouse over a variable while you are in Break mode will cause a tooltip to be displayed, which shows the type of object you are hovering over. You can see this below, where the datatip for a Customer object is shown.</p>
<p><img title="Default datatip for a Customer object" src="http://www.professionalvisualstudio.com/blog/wp-content/uploads/2008/08/debuggerdisplay_none.png" alt="Customer class with a default datatip" /></p>
<p>Unfortunately this information is not particularly useful, as most of the time you have a fairly good idea about the type of object you are dealing with. It would be better for this single line to contain more useful information about the object, as is the case with simple types such as strings or integers where the actual value is displayed.</p>
<p>This is where the DebuggerDisplay attribute comes in handy. It can be used to change this single line representation of the object from the default full class name. The attribute takes a single parameter, which is a String. The format of this string can include the { and } braces, which can contain a field, property, or method that returns a value.</p>
<p>For example, you could apply the DebuggerDisplay attribute to the Customer class as follows:</p>
<p><code>[DebuggerDisplay("Customer {CustomerName} has {Orders.Count} orders")]<br />
public class Customer</code></p>
<p>If you apply the attribute in this way, it will result in the following datatip:</p>
<p><img title="The datatip for the Customer class that has a DebuggerDisplay attribute" src="http://www.professionalvisualstudio.com/blog/wp-content/uploads/2008/08/debuggerdisplay_applied.png" alt="Customer class with DebuggerDisplay attribute" /></p>
<p>If you are a C# developer, then in addition to specifying properties and methods in the DebuggerDisplay format string, you can also include a general expression within the braces. For example, the following attribute shows how you could use an expression with a ternary operator:</p>
<p><code>[DebuggerDisplay("Customer {CustomerName} has orders? {(Orders.Count &gt; 0) ? \"Yes\" : \"No\"}")]</code></p>
<p>Which would result in the following datatip:</p>
<p><img src="http://www.professionalvisualstudio.com/blog/wp-content/uploads/2008/08/debuggerdisplay_expression.png" alt="Customer class showing datatip with DebuggerDisplay attribute expression" title="The datatip for the Customer class that has a DebuggerDisplay attribute with an expression" /></p>
<p>One final thing to note, in addition to Classes, the DebuggerDisplay attribute can be applied to any Struct, Delegate, Enumeration, Field, Property, or even an entire Assembly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.professionalvisualstudio.com/blog/2008/08/15/debugging-tip-of-the-week-4-the-debuggerdisplay-attribute/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Debugging Tip of the Week #3: The DebuggerBrowsable Attribute</title>
		<link>http://www.professionalvisualstudio.com/blog/2008/08/07/debugging-tip-of-the-week-3-the-debuggerbrowsable-attribute/</link>
		<comments>http://www.professionalvisualstudio.com/blog/2008/08/07/debugging-tip-of-the-week-3-the-debuggerbrowsable-attribute/#comments</comments>
		<pubDate>Thu, 07 Aug 2008 14:10:23 +0000</pubDate>
		<dc:creator>Dave Gardner</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Tips and Tricks]]></category>

		<guid isPermaLink="false">http://www.professionalvisualstudio.com/blog/2008/08/07/debugging-tip-of-the-week-3-the-debuggerbrowsable-attribute/</guid>
		<description><![CDATA[In my latest instalment of the Debugging tip of the week, I&#8217;d like to introduce the debugging attributes. These handy attributes, found in the System.Diagnostics namespace, can be applied to your source code in order to control the way the debugger steps through it. Some of the debugging attributes can also be used to customise [...]]]></description>
			<content:encoded><![CDATA[<p>In my latest instalment of the Debugging tip of the week, I&#8217;d like to introduce the debugging attributes. These handy attributes, found in the <code>System.Diagnostics</code> namespace, can be applied to your source code in order to control the way the debugger steps through it. Some of the debugging attributes can also be used to customise the data tips that appear when you hover over a variable in Break mode, or view it in a Watch window.</p>
<p>The first attribute we will cover is the <code>DebuggerBrowsable</code> attribute. This attribute takes a <code>DebuggerBrowsableState</code> enumeration value as a parameter that determines how the member is displayed in the variable tree. In the following code snippet, the field Orders in the Customer class is set to Collapsed:</p>
<p><code>using System.Diagnostics;</code></p>
<p><code>public class Customer<br />
{<br />
&nbsp;&nbsp;&nbsp;[DebuggerBrowsable(DebuggerBrowsableState.Collapsed)]<br />
&nbsp;&nbsp;&nbsp;public List&lt;Order&gt; Orders;<br />
}<br />
</code><br />
Collapsed is the default DebuggerBrowsableState that you would normally see in Visual Studio. If you apply this to a member then it will be displayed, but not expanded by default, as shown below.</p>
<p><img src="http://www.professionalvisualstudio.com/blog/wp-content/uploads/2008/08/debuggerbrowsablestate_collapsed.png" alt="DebuggerBrowsableState.Collapsed" /></p>
<p>The following shows the same snippet with DebuggerBrowsable set to the RootHidden value. In this case the actual Orders item does not appear as a top-level entry in the data window, just the contents of the collection.</p>
<p><img src="http://www.professionalvisualstudio.com/blog/wp-content/uploads/2008/08/debuggerbrowsablestate_roothidden.png" alt="DebuggerBrowsableState.RootHidden" /></p>
<p>Finally, the Never value will hide the member completely from the data window. This can be quite useful if you want to hide your private properties. You can see the effect of this below, where the Orders member does not appear at all.</p>
<p><img src="http://www.professionalvisualstudio.com/blog/wp-content/uploads/2008/08/debuggerbrowsablestate_never.png" alt="DebuggerBrowsableState.Never" /></p>
<p>In .NET Framework 2.0, the <code>DebuggerBrowsable</code>  attribute is only interpreted by the C# debugger and has no effect when it is applied to Visual Basic code. This limitation has been removed in newer versions of the .NET Framework.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.professionalvisualstudio.com/blog/2008/08/07/debugging-tip-of-the-week-3-the-debuggerbrowsable-attribute/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Debugging Tip of the Week #2: Design-time Expression Evaluation</title>
		<link>http://www.professionalvisualstudio.com/blog/2008/07/31/debugging-tip-of-the-week-2-design-time-expression-evaluation/</link>
		<comments>http://www.professionalvisualstudio.com/blog/2008/07/31/debugging-tip-of-the-week-2-design-time-expression-evaluation/#comments</comments>
		<pubDate>Thu, 31 Jul 2008 12:43:04 +0000</pubDate>
		<dc:creator>Dave Gardner</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Tips and Tricks]]></category>

		<guid isPermaLink="false">http://www.professionalvisualstudio.com/blog/2008/07/31/debugging-tip-of-the-week-2-design-time-expression-evaluation/</guid>
		<description><![CDATA[Last week I talked about how useful the Immediate Window is during debug-mode. A less-known, but totally awesome, feature of the Immediate Window is that it can be used at design-time to test/debug your code. This is particularly useful if you are working on a particular class or method that you want to test without [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I talked about how <a href="http://www.professionalvisualstudio.com/blog/2008/07/24/debugging-tip-of-the-week-1-the-immediate-window/">useful the Immediate Window is during debug-mode</a>. A less-known, but totally awesome, feature of the Immediate Window is that it can be used at design-time to test/debug your code. This is particularly useful if you are working on a particular class or method that you want to test <strong><em>without running the entire application</em></strong><em>.</em></p>
<p>The best way to understand this is to see it in action. The screenshot below shows a new Customer object being created in a C# project within the Immediate window.</p>
<p><img src="http://www.professionalvisualstudio.com/blog/wp-content/uploads/2008/07/designtimedebugging.png" alt="Design Time Expression Evaluation in the Immediate Window" /></p>
<p>When you declare a new object in the Immediate window at design-time, Visual Studio will create an instance of that object. However even though it has created this object, Visual Studio will remain in design-mode (For those that are interested, it actually uses the Object Test Bench in the background). If you invoke a method or property that contains an active breakpoint, Visual Studio will change to debug mode and break at the breakpoint. Once you resume execution from the break Visual Studio will return to design-mode.</p>
<p>There are a few gotcha&#8217;s and caveats that you need to be aware of when performing design-time expression evaluation.</p>
<p>Firstly, within a Visual Basic project you can’t use an explicit variable declaration (for example, <code>Dim myCust as Customer</code>). Instead variables are declared implicitly using the assignment operator (for example, <code>myCust = new Customer()</code>).</p>
<p>Secondly, if you are using C#, IntelliSense is not available in the Immediate window during design-time expression evaluation. Visual Basic programmers are a little more spoilt, as they are given a limited form of IntelliSense here.</p>
<p>Thirdly, any statements you enter into the Immediate Window will be evaluated against whatever project is selected in the Solution Explorer. If no project is selected,Visual Studio will evaluate it against the startup project. If the code doesn&#8217;t exist is that project, you will get an error.</p>
<p>Finally, you can&#8217;t use design-time expression evaluation in any project that  requires an execution environment. This includes Web projects, VSTO projects, Smart Device projects, and SQL projects. However as I mentioned earlier, the scope of this is per-project. Therefore, if you have a solution that contains a web project and a separate class library project for your business logic, you can use this method to test any of the business logic code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.professionalvisualstudio.com/blog/2008/07/31/debugging-tip-of-the-week-2-design-time-expression-evaluation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Debugging Tip of the Week #1: The Immediate Window</title>
		<link>http://www.professionalvisualstudio.com/blog/2008/07/24/debugging-tip-of-the-week-1-the-immediate-window/</link>
		<comments>http://www.professionalvisualstudio.com/blog/2008/07/24/debugging-tip-of-the-week-1-the-immediate-window/#comments</comments>
		<pubDate>Thu, 24 Jul 2008 15:34:04 +0000</pubDate>
		<dc:creator>Dave Gardner</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Tips and Tricks]]></category>

		<guid isPermaLink="false">http://www.professionalvisualstudio.com/blog/2008/07/24/debugging-tip-of-the-week-1-the-immediate-window/</guid>
		<description><![CDATA[This post has been a long time coming, as I previously announced I would start this series following my user group presentation back in June. There&#8217;s a myriad of reasons why I let it go so long, however the impending release of Professional Visual Studio 2008 has kicked me into action. For the first of [...]]]></description>
			<content:encoded><![CDATA[<p>This post has been a long time coming, as I previously announced I would start this series following my <a href="http://www.professionalvisualstudio.com/blog/2008/06/03/debugging-tips-and-tricks/">user group presentation back in June</a>. There&#8217;s a myriad of reasons why I let it go so long, however the impending release of <a href="http://www.professionalvisualstudio.com/books/ProVS2008/">Professional Visual Studio 2008</a> has kicked me into action.</p>
<p>For the first of what will become an ongoing weekly series of debugging tips I&#8217;d like to start off with one of my favourite windows in the whole of Visual Studio &#8211; the Immediate Window. This useful window can be used during both debugging and at design-time. The window is displayed using the keyboard shortcut Ctrl+Alt+I, or by selecting it from the Debug-&gt;Windows menu. You can also type <code>immed</code> in the Command window and it will display and switch focus to the immediate window.</p>
<p>Quite often when you&#8217;re debugging an application, you&#8217;ll want to evaluate a simple expression either to test a bit of functionality or to remind yourself of how something works. This is one situation where the Immediate window comes in very handy. This window enables you to run expressions as you type them, as shown below.</p>
<p><img src="http://www.professionalvisualstudio.com/blog/wp-content/uploads/2008/07/immediatewindow.png" alt="The Immediate Window" /></p>
<p>To make life easier, the Immediate window supports IntelliSense, and you can use the arrow keys to track back through the history of previous commands executed.</p>
<p>The Immediate window also allows you to execute any Visual Studio command. To submit a command you must enter a greater than symbol &gt; at the start of the line. There is an extremely large set of commands available; in fact almost any action that can be performed within Visual Studio is accessible as a command. Fortunately IntelliSense makes navigating this list of available commands a little more manageable.</p>
<p><a href="http://www.professionalvisualstudio.com/blog/2007/10/22/do-you-have-an-alias-for-that/">Nick blogged last year about aliases</a>, which are available under both the Command and Immediate windows. One of the more well-known aliases is “?”, which is a shortcut for the Debug.Print command that prints out the value of a variable. As long as you have the Immediate window open, this is generally quicker and more convenient than bringing up a watch window, or hovering over a variable and waiting for the data tip to display.</p>
<p>If you start to become really dependant on the Immediate window for your debugging, you should consider enabling the option to <em>Redirect all Output window text to the Immediate window</em>. This option can be enabled under  Tools-&gt;Options-&gt;Debugging. Although the option says it redirects <strong><em>all </em></strong>Output window text, it will actually only redirect the debug messages (e.g. Debug.Print).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.professionalvisualstudio.com/blog/2008/07/24/debugging-tip-of-the-week-1-the-immediate-window/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Coming clean</title>
		<link>http://www.professionalvisualstudio.com/blog/2008/04/02/coming-clean/</link>
		<comments>http://www.professionalvisualstudio.com/blog/2008/04/02/coming-clean/#comments</comments>
		<pubDate>Tue, 01 Apr 2008 16:30:29 +0000</pubDate>
		<dc:creator>Dave Gardner</dc:creator>
				<category><![CDATA[Announcements]]></category>
		<category><![CDATA[Tips and Tricks]]></category>

		<guid isPermaLink="false">http://www.professionalvisualstudio.com/blog/2008/04/02/coming-clean/</guid>
		<description><![CDATA[I guess I&#8217;d better come clean in case people weren&#8217;t paying close attention and took me seriously on my Useful .NET coding tip post. Yesterday was April 1st and this was my addition to the general fun and trickery. There were some subtle clues due to the very silly statements I made in the post: [...]]]></description>
			<content:encoded><![CDATA[<p>I guess I&#8217;d better come clean in case people weren&#8217;t paying close attention and took me seriously on my <a href="http://www.professionalvisualstudio.com/blog/2008/04/01/useful-net-coding-tip/">Useful .NET coding tip</a> post. Yesterday was <a href="http://en.wikipedia.org/wiki/April_fools_day">April 1st</a> and this was my addition to the <a href="http://www.google.com.au/intl/en/gday/index.html">general</a> <a href="http://www.google.com/virgle/index.html">fun</a> and <a href="http://a.viary.com/blog/posts/dodo-web-based-time-machine">trickery</a>.</p>
<p>There were some subtle clues due to the very silly statements I made in the post:</p>
<blockquote><p>&#8220;Whilst this implementation might have been ok back in the late 90’s&#8221;</p></blockquote>
<p>[Editors note: The first version of .NET wasn't released until 2002]</p>
<blockquote><p>&#8220;&#8230;the <code>sender</code> parameter is the one you want, as it will give you the details on who invoked the event (i.e. who was the “sender”)&#8221;</p></blockquote>
<blockquote><p>&#8220;&#8230;as you probably know string comparisons can be very slow, so for performance reasons I recommend you compare the <code>User</code> property.&#8221;</p></blockquote>
<p>and this one which is my favourite, because it is so wrong, but sounds like I know what I&#8217;m talking about:</p>
<blockquote><p>&#8220;&#8230;Windows XP Service Pack 2, which changed the code for the WindowsIdentity class from running in User mode to running in Kernel mode.&#8221;</p></blockquote>
<p>Hopefully my somewhat dry sense of humour didn&#8217;t catch anyone too much off guard. As far as April Fools Day jokes go, the clues on this one weren&#8217;t particularly obvious, so don&#8217;t feel bad if you weren&#8217;t paying enough attention to realise. I solemnly promise that everything I post from now on will be totally legitimate with no trickery involved (for the next 364 days at least)!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.professionalvisualstudio.com/blog/2008/04/02/coming-clean/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Useful .NET coding tip</title>
		<link>http://www.professionalvisualstudio.com/blog/2008/04/01/useful-net-coding-tip/</link>
		<comments>http://www.professionalvisualstudio.com/blog/2008/04/01/useful-net-coding-tip/#comments</comments>
		<pubDate>Mon, 31 Mar 2008 18:29:47 +0000</pubDate>
		<dc:creator>Dave Gardner</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Tips and Tricks]]></category>

		<guid isPermaLink="false">http://www.professionalvisualstudio.com/blog/2008/04/01/useful-net-coding-tip/</guid>
		<description><![CDATA[The other day I found myself explaining a solution to a .NET coding issue that seems to pop up from time to time, particularly with those new to WinForms programming. Since there doesn&#8217;t seem to be particularly good search results pointing to a solution elsewhere, I thought I&#8217;d share it here. The Scenario You have [...]]]></description>
			<content:encoded><![CDATA[<p>The other day I found myself explaining a solution to a .NET coding issue that seems to pop up from time to time, particularly with those new to WinForms programming. Since there doesn&#8217;t seem to be particularly good search results pointing to a solution elsewhere, I thought I&#8217;d share it here.</p>
<h2>The Scenario</h2>
<p>You have a control on a form, say a Checkbox, that you need to set to a specific value when the form is opened. However you also have a handler attached to the changed event that needs to take action when the user checks/unchecks it. Here&#8217;s what a (obviously simplified) typical implementation of this would look like in Visual Basic:</p>
<pre>Public Class Form1
  Private Sub Form1_Load(ByVal sender As Object, _
                         ByVal e As System.EventArgs) _
                         Handles Me.Load
    Me.CheckBox1.Checked = True
  End Sub

  Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, _
                                       ByVal e As System.EventArgs) _
                                       Handles CheckBox1.CheckedChanged
    If Me.CheckBox1.Checked Then
      MessageBox.Show("I've been checked!")
    End If
  End Sub
End Class</pre>
<p>Any guesses as to what&#8217;s going to happen here? You&#8217;ve probably worked out that when you set the <code>CheckBox1.Checked </code>value to true in the <code>Form1_Load()</code> method, it will cause the <code>CheckBox1.CheckedChanged</code> event to be fired, thereby running your logic.</p>
<p>This is probably not what you wanted to happen. What you want is some way of distinguishing whether the checkbox was changed by an end user, or programmatically by the application itself.</p>
<h2>The Solution</h2>
<p>There are several ways to code around this, including some really nasty hacks involving the AddHandler/RemoveHandler statements. However if you look at the parameters on the CheckedChanged event hander, you might get a inkling as to what is the most elegant solution for this.</p>
<p>Yes, it&#8217;s the <code>sender</code> parameter. This little beauty is often overlooked in favour of the usually quite useful <code>EventArgs</code> parameter. However in this particular case the <code>sender</code> parameter is the one you want, as it will give you the details on who invoked the event (i.e. who was the &#8220;sender&#8221;).</p>
<p>Now you may have noticed that the <code>sender</code> parameter is of type System.Object. This is where the really elegant part of the solution comes into play. What you need to do is attempt to cast the <code>sender</code> object to a <code>System.Security.Principal.WindowsIdentity</code>, which is the class that represents a Windows user. Make sure you use the TryCast statement (&#8220;as&#8221; keyword for you C# developers) so that it doesn&#8217;t raise an exception if the conversion fails. As long as the TryCast doesn&#8217;t return Nothing, you know the checkbox was changed by a user.</p>
<p>Now don&#8217;t go and implement it just yet! As regular readers know, I have a fairly strong interest in security. Whilst this implementation might have been ok back in the late 90&#8242;s, these days with all the spyware, malware, and viruses that are infecting end users machines it would be wise to include a little bit of defensive programming here.</p>
<p>So how do we know whether the checkbox was changed by a &#8220;real&#8221; user, as opposed to some nasty malware that is impersonating the user?</p>
<p>It turns out to be quite simple. All you need to do is compare the WindowsIdentity that you retrieved from the sender with the WindowsIdentity that is returned by a call to <code>System.Security.Principal.WindowsIdentity.GetCurrent()</code>. You can use the <code>Name</code> property for the comparison &#8211; that will return the username as a string in the format domain\username. However as you probably know string comparisons can be very slow, so for performance reasons I recommend you compare the <code>User</code> property, which returns the SID of the Windows account. If they match then it was changed by the real user.</p>
<p>Now hang on a minute I hear you say. What&#8217;s to stop the malware from hijacking the <code>System.Security.Principal.WindowsIdentity.GetCurrent()</code>?</p>
<p>Well in 2002 Microsoft completely reviewed the security of Windows XP as part of their <a href="http://www.microsoft.com/mscorp/twc/default.mspx">Trustworthy Computing initiative</a>. We reaped the rewards of that review starting with Windows XP Service Pack 2, which changed the code for the WindowsIdentity class from running in User mode to running in Kernel mode. That means unless it&#8217;s a rootkit, there is no way that any malware can hijack this call. So as long as your users are running Windows XP SP2 or later, this code is secure.</p>
<p>I know all of the above sounds pretty complicated for such a seemingly simple issue, but if you spend any time trying to get the alternative hacks working, you&#8217;ll quickly realise that this is the most elegant solution. You might even want to create a Code Snippet for this, to save yourself some time in the future.</p>
<p>So here&#8217;s the final implementation of this solution. Fast, reliable, and secure.</p>
<pre>Public Class Form1
  Private Sub Form1_Load(ByVal sender As Object, _
                         ByVal e As System.EventArgs) _
                         Handles Me.Load
    Me.CheckBox1.Checked = True
  End Sub

  Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, _
                                       ByVal e As System.EventArgs) _
                                       Handles CheckBox1.CheckedChanged
    Dim senderUser As System.Security.Principal.WindowsIdentity
    Dim actualUser As System.Security.Principal.WindowsIdentity
    senderUser = TryCast(sender, System.Security.Principal.WindowsIdentity)
    actualUser = System.Security.Principal.WindowsIdentity.GetCurrent()

    If senderUser IsNot Nothing AndAlso _
       senderUser.User = actualUser.User Then
      'This is a real user!!
      If Me.CheckBox1.Checked Then
        MessageBox.Show("I've been checked!")
      End If
    End If
  End Sub
End Class</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.professionalvisualstudio.com/blog/2008/04/01/useful-net-coding-tip/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

