<?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>FormatException &#187; Visual Studio</title>
	<atom:link href="http://www.formatexception.com/category/visual-studio/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.formatexception.com</link>
	<description>Ramblings on developing in the Windows World</description>
	<lastBuildDate>Fri, 23 Sep 2011 15:34:26 +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>Often Unused Operators: &#124;= (or equals)</title>
		<link>http://www.formatexception.com/2009/01/often-unused-operators-or-equals/</link>
		<comments>http://www.formatexception.com/2009/01/often-unused-operators-or-equals/#comments</comments>
		<pubDate>Wed, 28 Jan 2009 15:31:34 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[often unused operators]]></category>
		<category><![CDATA[|=]]></category>

		<guid isPermaLink="false">http://www.formatexception.com/?p=174</guid>
		<description><![CDATA[I&#8217;ve debated before the usefulness of the &#124; and &#038; operators. The use of the &#124; operator depends on the context in which it is used. In the case of bools a boolean operation is performed. In the case of ints a bitwise OR is performed. For boolean operations when the &#124; operator is used [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve debated before the usefulness of the <a href="http://www.formatexception.com/2008/09/be-be/">| and &#038; operators</a>.  The use of the | operator depends on the context in which it is used. </p>
<p>In the case of bools a boolean operation is performed.  In the case of ints a bitwise OR is performed.  </p>
<p>For boolean operations when the | operator is used both sides of the operator are evaluated.  In the example below both VerifyStatus and SetDefaults are called regardless of the boolean returned by each of them.</p>
<pre name="code" class="csharp">public void TestTransaction(PaymentTransaction Transaction)
{
    if (Transaction != null &#038;&#038;
        (VerifyStatus(Transaction.State) | SetDefaults(Transaction)))
    {
        //do something with the code
    }
}
private bool VerifyStatus(ItemState State)
{
    if (State == null)
        return false;
    if (State.Status == Status.NotVerified)
        ValidateStatus(State);
    if (State.Status == Status.Valid)
        return true;
    //Status.Invalid
    return false;
}
private void ValidateStatus(ItemState State)
{
    //validate the status
}
private bool SetDefaults(PaymentTransaction Transaction)
{
    if (Transaction == null)
        return false;
    //no need to set the defaults if the state is already valid
    if (Transaction.State.Status == Status.Valid)
        return true;
    //set all the default values for the transaction
    return true;
}</pre>
<p>Ok, I know it&#8217;s not a pillar of phenomenal code.  But you get the point, both of the sides of the | are run.  Now, what if you&#8217;ve all ready evaluated the status earlier in code?  No need to do it again.  But you may need to still set the defaults.  Well, that is where the |= comes into play.</p>
<pre name="code" class="csharp">bool transStatus = VerifyStatus(Transaction.State);
//do something with the code
transStatus |= SetDefaults(Transaction);
if(transStatus)
{
    //do something with the code
}</pre>
<p>In the case of an int a bitwise operation is performed.</p>
<pre name="code" class="csharp">int a = 0x0c;
a = a | 0x06;
//a results in 0x0000000e
//1100
//0110 results in
//1110 which is e</pre>
<p>Like the logical operation example above the | and assignment can be collapsed down to:</p>
<pre name="code" class="csharp">int a = 0x0c;
a |= 0x06;</pre>
<p>I think it&#8217;s very important to understand the difference between | and || as well as &#038; and &#038;&#038;.  Recently I came across a bunch of code where the original developer used | and || interchangeably.  They are not the same.  Using | in place of || overrides the fail fast nature of boolean operations most modern languages support.  They are not interchangeable.  If Transaction was null the following code would not work</p>
<pre name="code" class="csharp">if(Transaction == null | Transaction.State == null)</pre>
<p> simply because both sides of the operation are evaluated so Transaction.State would throw a null pointer exception.  Not only that but your code will run slower.</p>
<p>Next in this series will be checked and unchecked.</p>
<p>Later,<br />
Brian</p>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.formatexception.com%2F2009%2F01%2Foften-unused-operators-or-equals%2F&amp;t=Often%20Unused%20Operators%3A%20%7C%3D%20%28or%20equals%29" title="Facebook"><img src="http://www.formatexception.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://news.ycombinator.com/submitlink?u=http%3A%2F%2Fwww.formatexception.com%2F2009%2F01%2Foften-unused-operators-or-equals%2F&amp;t=Often%20Unused%20Operators%3A%20%7C%3D%20%28or%20equals%29" title="HackerNews"><img src="http://www.formatexception.com/wp-content/plugins/sociable/images/hackernews.png" title="HackerNews" alt="HackerNews" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.formatexception.com/2009/01/often-unused-operators-or-equals/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Stop that build!</title>
		<link>http://www.formatexception.com/2008/09/stop-that-build/</link>
		<comments>http://www.formatexception.com/2008/09/stop-that-build/#comments</comments>
		<pubDate>Tue, 16 Sep 2008 22:16:10 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[VB.Net]]></category>

		<guid isPermaLink="false">http://www.formatexception.com/?p=3</guid>
		<description><![CDATA[ I&#8217;m sure we&#8217;ve all clicked build by accident, especially with a dnn project. Did you know you can stop it? Ctrl + Break ref:stevenharman.net Share and Enjoy:]]></description>
			<content:encoded><![CDATA[<p><!--StartFragment --> I&#8217;m sure we&#8217;ve all clicked build by accident, especially with a dnn project.</p>
<p>Did you know you can stop it?</p>
<p>Ctrl + Break</p>
<p>ref:<a href="http://stevenharman.net/blog/archive/2008/01/17/visual-studio-tip-kill-that-build.aspx">stevenharman.net</a></p>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.formatexception.com%2F2008%2F09%2Fstop-that-build%2F&amp;t=Stop%20that%20build%21" title="Facebook"><img src="http://www.formatexception.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://news.ycombinator.com/submitlink?u=http%3A%2F%2Fwww.formatexception.com%2F2008%2F09%2Fstop-that-build%2F&amp;t=Stop%20that%20build%21" title="HackerNews"><img src="http://www.formatexception.com/wp-content/plugins/sociable/images/hackernews.png" title="HackerNews" alt="HackerNews" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.formatexception.com/2008/09/stop-that-build/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

