<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: looking for a better switch/case statement?</title>
	<atom:link href="http://jonfuller.codingtomusic.com/2008/09/04/looking-for-a-better-switchcase-statement/feed/" rel="self" type="application/rss+xml" />
	<link>http://jonfuller.codingtomusic.com/2008/09/04/looking-for-a-better-switchcase-statement/</link>
	<description></description>
	<lastBuildDate>Fri, 04 Feb 2011 19:10:56 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: Paul</title>
		<link>http://jonfuller.codingtomusic.com/2008/09/04/looking-for-a-better-switchcase-statement/comment-page-1/#comment-140</link>
		<dc:creator>Paul</dc:creator>
		<pubDate>Thu, 04 Sep 2008 20:09:10 +0000</pubDate>
		<guid isPermaLink="false">http://jonfuller.codingtomusic.com/?p=25#comment-140</guid>
		<description>Good point about the Default() method bug. Below is such an example:

int age;
Switch.On(&quot;Paul&quot;)
    .Case(x =&gt; x == &quot;George&quot;, () =&gt; { age = 40; })
    .Default( () =&gt; { age = 70; })
    .Case(x =&gt; x == &quot;Paul&quot;, () =&gt; { age = 25; });

In this situation age would be set to 70 instead of 25.

I think the Default() method signature will have to be updated to return void instead of Switch. This will ensure that Default() is always at the end of the statement and is only called if none of the Cases have been met.</description>
		<content:encoded><![CDATA[<p>Good point about the Default() method bug. Below is such an example:</p>
<p>int age;<br />
Switch.On(&#8220;Paul&#8221;)<br />
    .Case(x =&gt; x == &#8220;George&#8221;, () =&gt; { age = 40; })<br />
    .Default( () =&gt; { age = 70; })<br />
    .Case(x =&gt; x == &#8220;Paul&#8221;, () =&gt; { age = 25; });</p>
<p>In this situation age would be set to 70 instead of 25.</p>
<p>I think the Default() method signature will have to be updated to return void instead of Switch. This will ensure that Default() is always at the end of the statement and is only called if none of the Cases have been met.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jon</title>
		<link>http://jonfuller.codingtomusic.com/2008/09/04/looking-for-a-better-switchcase-statement/comment-page-1/#comment-139</link>
		<dc:creator>jon</dc:creator>
		<pubDate>Thu, 04 Sep 2008 19:39:51 +0000</pubDate>
		<guid isPermaLink="false">http://jonfuller.codingtomusic.com/?p=25#comment-139</guid>
		<description>Awesome Idea!  Thanks Paul!  There is also one more change in behavior. If you specify your Default at some point other than the end of your list of Cases, it&#039;ll catch every time and it will never get to the cases below your Default.

I&#039;ll update the post with the updated implementation.</description>
		<content:encoded><![CDATA[<p>Awesome Idea!  Thanks Paul!  There is also one more change in behavior. If you specify your Default at some point other than the end of your list of Cases, it&#8217;ll catch every time and it will never get to the cases below your Default.</p>
<p>I&#8217;ll update the post with the updated implementation.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paul</title>
		<link>http://jonfuller.codingtomusic.com/2008/09/04/looking-for-a-better-switchcase-statement/comment-page-1/#comment-138</link>
		<dc:creator>Paul</dc:creator>
		<pubDate>Thu, 04 Sep 2008 19:30:17 +0000</pubDate>
		<guid isPermaLink="false">http://jonfuller.codingtomusic.com/?p=25#comment-138</guid>
		<description>I just realized a flaw in my design. If 2 Cases are met, then both actions will be fired instead of only the first case.

An additional private flag will be needed to keep track if a Case has been met, and that flag will have to be checked before running an action

public Switch Case(Predicate @case, Action action)
{
   if(!_caseMet &amp;&amp; @case(_target))
   { 
      _caseMet = true;
      action;
   }
   return this;
}</description>
		<content:encoded><![CDATA[<p>I just realized a flaw in my design. If 2 Cases are met, then both actions will be fired instead of only the first case.</p>
<p>An additional private flag will be needed to keep track if a Case has been met, and that flag will have to be checked before running an action</p>
<p>public Switch Case(Predicate @case, Action action)<br />
{<br />
   if(!_caseMet &amp;&amp; @case(_target))<br />
   {<br />
      _caseMet = true;<br />
      action;<br />
   }<br />
   return this;<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paul</title>
		<link>http://jonfuller.codingtomusic.com/2008/09/04/looking-for-a-better-switchcase-statement/comment-page-1/#comment-137</link>
		<dc:creator>Paul</dc:creator>
		<pubDate>Thu, 04 Sep 2008 19:21:46 +0000</pubDate>
		<guid isPermaLink="false">http://jonfuller.codingtomusic.com/?p=25#comment-137</guid>
		<description>What if you fire the action inside the Case() method instead of storing it? Then you wouldn&#039;t need to call Go() at the end either.

The Case method could look something like this

public Switch Case(Predicate @case, Action action)
{
    if (@case(_target))
       action;
    return this;
}

and the Default method would change to

public Switch Default(Action action)
{
   action;
   return this;
} 

and you wouldn&#039;t need the _cases and _default private fields.</description>
		<content:encoded><![CDATA[<p>What if you fire the action inside the Case() method instead of storing it? Then you wouldn&#8217;t need to call Go() at the end either.</p>
<p>The Case method could look something like this</p>
<p>public Switch Case(Predicate @case, Action action)<br />
{<br />
    if (@case(_target))<br />
       action;<br />
    return this;<br />
}</p>
<p>and the Default method would change to</p>
<p>public Switch Default(Action action)<br />
{<br />
   action;<br />
   return this;<br />
} </p>
<p>and you wouldn&#8217;t need the _cases and _default private fields.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

