<?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>Jason Doucette, Toronto Tech Guy &#187; Code Samples</title>
	<atom:link href="http://jasondoucette.ca/category/code-samples/feed/" rel="self" type="application/rss+xml" />
	<link>http://jasondoucette.ca</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Thu, 02 Sep 2010 20:08:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Generating dynamic dates for MySQL test data</title>
		<link>http://jasondoucette.ca/2008/02/08/generating-dynamic-dates-for-mysql-test-data/</link>
		<comments>http://jasondoucette.ca/2008/02/08/generating-dynamic-dates-for-mysql-test-data/#comments</comments>
		<pubDate>Fri, 08 Feb 2008 15:42:12 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Code Samples]]></category>
		<category><![CDATA[Howto]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[test data]]></category>
		<category><![CDATA[test suite]]></category>

		<guid isPermaLink="false">http://jasondoucette.ca/2008/02/08/generating-dynamic-dates-for-mysql-test-data/</guid>
		<description><![CDATA[A quickie MySQL tip: I&#8217;ve been working on creating a standard testbed of data (more on that someday) for one of our apps, and the issue of dates came up.  Dates can be a problem in test data if they&#8217;re being compared to today&#8217;s date, i.e. if it matters if the date field represents something [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>A quickie MySQL tip:</p>
<p>I&#8217;ve been working on creating a standard testbed of data (more on that someday) for one of our apps, and the issue of dates came up.  Dates can be a problem in test data if they&#8217;re being compared to today&#8217;s date, i.e. if it matters if the date field represents something in the past, present or future.</p>
<p>No problem, I thought, instead of setting something up with a string literal, like</p>
<pre>date_field = "2008-03-04 12:00:00"</pre>
<p>(Assume that&#8217;s inside an insert or update command&#8230;)</p>
<p>I&#8217;d build the string based on the current date, so a field would always be, say, 5 days in the future (assuming the testbed is repopulated daily through an automated job):</p>
<pre>date_field = date(date_add(now(), interval 5 day)) + " 12:00:00"</pre>
<p>That, as it turns out, doesn&#8217;t work so well.  (Oh, another quick tip, check your data after setting up a test suite before going nuts over where your code might be failing).  A quick, easy, and &#8220;should have done this in the first place&#8221; check reveals:</p>
<pre>+-----------------------------------------------------+
| date(date_add(now(), interval 5 day)) + " 12:00:00" |
+-----------------------------------------------------+
|                                                2020 |
+-----------------------------------------------------+</pre>
<p>I ended up going with something like this:</p>
<pre>date_field = date_format(date_add(now(), interval 5 day), "%Y-%m-%d 12:00:00")</pre>
<p>Much better&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://jasondoucette.ca/2008/02/08/generating-dynamic-dates-for-mysql-test-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Converting value types in C# with Parse and TryParse</title>
		<link>http://jasondoucette.ca/2008/01/18/converting-value-types-in-c-with-parse-and-tryparse/</link>
		<comments>http://jasondoucette.ca/2008/01/18/converting-value-types-in-c-with-parse-and-tryparse/#comments</comments>
		<pubDate>Sat, 19 Jan 2008 04:13:24 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Code Samples]]></category>
		<category><![CDATA[C# .NET programming]]></category>

		<guid isPermaLink="false">http://jasondoucette.ca/2008/01/18/converting-value-types-in-c-with-parse-and-tryparse/</guid>
		<description><![CDATA[Converting values from one type to another in C# is generally pretty straightforward: most objects contain a ToString() method which handles conversion to strings (at least for simple value types), and most value types have a Parse() method to read a string representation of their type. Of course, dealing with invalid inputs can be a [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>Converting values from one type to another in C# is generally pretty straightforward: most objects contain a ToString() method which handles conversion to strings (at least for simple value types), and most value types have a Parse() method to read a string representation of their type.</p>
<p>Of course, dealing with invalid inputs can be a pain &#8211; the Parse() method will throw an FormatException if you, for example, pass &#8220;a zillion&#8221; to int.Parse(), so you need to wrap the code in a try/catch block:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<div class="csharpcode">
<pre><span class="lnum">   1:  </span><span class="kwrd">int</span> output = 0;</pre>
<pre><span class="lnum">   2:  </span><span class="kwrd">try</span></pre>
<pre><span class="lnum">   3:  </span>{</pre>
<pre><span class="lnum">   4:  </span>    output = <span class="kwrd">int</span>.Parse(<span class="str">"a zillion"</span>);</pre>
<pre><span class="lnum">   5:  </span>}</pre>
<pre><span class="lnum">   6:  </span><span class="kwrd">catch</span>(FormatException)</pre>
<pre><span class="lnum">   7:  </span>{</pre>
<pre><span class="lnum">   8:  </span>    <span class="rem">// error handling goes here</span></pre>
<pre><span class="lnum">   9:  </span>}</pre>
<pre><span class="lnum">  10:  </span>Assert.AreEqual(0, output);</pre>
</div>
<p>Version 2.0 of the .NET Framework made this a lot simpler with the addition of the TryParse() method:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<div class="csharpcode">
<pre><span class="lnum">   1:  </span><span class="kwrd">int</span> output;</pre>
<pre><span class="lnum">   2:  </span><span class="kwrd">int</span>.TryParse(<span class="str">"a zillion"</span>, <span class="kwrd">out</span> output);</pre>
<pre><span class="lnum">   3:  </span>Assert.AreEqual(0, output);</pre>
</div>
<p>Note that it wasn&#8217;t necessary to initialize output here &#8211; if the call to TryParse fails, the output variable is set to 0.  In the case of DateTime, the variable is set to DateTime.MinValue.)</p>
<p>Sadly, converting strings to Enums is still a pain, but doable &#8211; the try/catch concept still holds, but you need to watch out for ArgumentExceptions, and there are some casting and parameters to consider:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<div class="csharpcode">
<pre><span class="lnum">   1:  </span><span class="kwrd">public</span> <span class="kwrd">enum</span> Stuff</pre>
<pre><span class="lnum">   2:  </span>{</pre>
<pre><span class="lnum">   3:  </span>    Something,</pre>
<pre><span class="lnum">   4:  </span>    SomethingElse</pre>
<pre><span class="lnum">   5:  </span>}</pre>
<pre><span class="lnum">   6:  </span>&nbsp;</pre>
<pre><span class="lnum">   7:  </span>[Test]</pre>
<pre><span class="lnum">   8:  </span><span class="kwrd">public</span> <span class="kwrd">void</span> EnumParsingTest()</pre>
<pre><span class="lnum">   9:  </span>{</pre>
<pre><span class="lnum">  10:  </span>    Stuff output = Stuff.SomethingElse;</pre>
<pre><span class="lnum">  11:  </span>    <span class="kwrd">try</span></pre>
<pre><span class="lnum">  12:  </span>    {</pre>
<pre><span class="lnum">  13:  </span>        output = (Stuff) (Enum.Parse(<span class="kwrd">typeof</span> (Stuff), <span class="str">"Something"</span>));</pre>
<pre><span class="lnum">  14:  </span>    }</pre>
<pre><span class="lnum">  15:  </span>    <span class="kwrd">catch</span>(ArgumentException)</pre>
<pre><span class="lnum">  16:  </span>    {</pre>
<pre><span class="lnum">  17:  </span>        <span class="rem">// error handling</span></pre>
<pre><span class="lnum">  18:  </span>    }</pre>
<pre><span class="lnum">  19:  </span>    Assert.AreEqual(Stuff.Something, output);</pre>
<pre><span class="lnum">  20:  </span>}</pre>
</div>
]]></content:encoded>
			<wfw:commentRss>http://jasondoucette.ca/2008/01/18/converting-value-types-in-c-with-parse-and-tryparse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C#, List.Find() and Predicates</title>
		<link>http://jasondoucette.ca/2007/10/30/c-listfind-and-predicates/</link>
		<comments>http://jasondoucette.ca/2007/10/30/c-listfind-and-predicates/#comments</comments>
		<pubDate>Tue, 30 Oct 2007 23:12:49 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Code Samples]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[predicates]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://jasondoucette.ca/2007/10/30/c-listfind-and-predicates/</guid>
		<description><![CDATA[Because I use this just infrequently enough to have to look it up every time&#8230; I&#8217;m a huge (ab)user of the .NET generic collection classes, but I hate cluttering code with foreach() loops every time that I need to find an item in a collection. Enter the Find() method, which takes a predicate and does [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>Because I use this <em>just</em> infrequently enough to have to look it up every time&#8230;</p>
<p>I&#8217;m a huge (ab)user of the .NET generic collection classes, but I hate cluttering code with foreach() loops every time that I need to find an item in a collection.  Enter the Find() method, which takes a predicate and does the work for you so you can keep focusing on the stuff that&#8217;s actually interesting:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">List&lt;SomeObject&gt; myObjects = <span class="kwrd">new</span> List&lt;SomeObject&gt;();
<span class="rem">/* .. load objects up somehow .. */</span>
SomeObject desiredObject =
    myObjects.Find(<span class="kwrd">delegate</span>(SomeObject o) { <span class="kwrd">return</span> o.Id == desiredId; });</pre>
]]></content:encoded>
			<wfw:commentRss>http://jasondoucette.ca/2007/10/30/c-listfind-and-predicates/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
