<?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>{ Code: Impossible } &#187; prototype</title>
	<atom:link href="http://codeimpossible.com/tag/prototype/feed/" rel="self" type="application/rss+xml" />
	<link>http://codeimpossible.com</link>
	<description>this = HowI.Roll();</description>
	<lastBuildDate>Thu, 02 Sep 2010 04:00:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Prototype to C#: Try.These()</title>
		<link>http://codeimpossible.com/2008/10/01/prototype-to-c-trythese/</link>
		<comments>http://codeimpossible.com/2008/10/01/prototype-to-c-trythese/#comments</comments>
		<pubDate>Wed, 01 Oct 2008 05:55:25 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[prototype]]></category>
		<category><![CDATA[try.these()]]></category>
		<category><![CDATA[try/catch]]></category>

		<guid isPermaLink="false">http://pistalwhipped.wordpress.com/?p=209</guid>
		<description><![CDATA[The prototype javascript library has a class (Try) and function called these(). This function accepts an array of functions as it&#8217;s sole argument and it will execute each function in the array, in the order they are added, and return the result from the first function that executes without error. If none of the functions [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://www.prototypejs.org" target="_blank">prototype javascript library</a> has a class (Try) and function called these(). This function accepts an array of functions as it&#8217;s sole argument and it will execute each function in the array, in the order they are added, and return the result from the first function that executes without error. If none of the functions executes successfully undefined is returned.</p>
<p>For example:</p>
<pre><code class="prettyprint">
var i = Try.These(
    function() { return 9 / 0; },
    function() { return 1; }
);
</code></pre>
<p>If we ran the sample above i would equal 1 because the first function would encounter a division by zero error. However:</p>
<pre><code class="prettyprint">
var h = 0;
var i = Try.These(
    function() { return 9 / h; },
    function() { return 1 / h; }
);
</code></pre>
<p>i will equal undefined in this example.</p>
<p>With the .Net framework&#8217;s generics library we can achieve roughly the same results. We won&#8217;t be able to assign an undefined value to the result but we can play around with the default keyword. :D</p>
<pre><code class="prettyprint">
public class Try
{
    public static T These(params Func[] delegates)
    {
        for (int i = 0; i &lt;= delegates.Length - 1; i++)
        {
            try
            {
                return (T)delegates[i]();
            }
            catch
            {

            }
        }
        return default(T);
    }
}
</code></pre>
<p>So for example:</p>
<pre><code class="prettyprint">
static void Main(string[] args)
{
    int i = 0;
    int y = Try.These(
        delegate() {
            int x = 0;
            return i / x;
        },

        delegate() {
            return ++i;
    });

    Console.Write("y is " + y.ToString());
}
</code></pre>
<p>Will output</p>
<p><code>y is 0</code></p>
]]></content:encoded>
			<wfw:commentRss>http://codeimpossible.com/2008/10/01/prototype-to-c-trythese/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
