<?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>Brian Grinstead &#187; snippet</title>
	<atom:link href="http://www.briangrinstead.com/blog/tag/snippet/feed" rel="self" type="application/rss+xml" />
	<link>http://www.briangrinstead.com/blog</link>
	<description></description>
	<lastBuildDate>Tue, 10 Aug 2010 18:26:31 +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>C# Tips &#8211; Type Conversion with &#8220;as&#8221; and &#8220;is&#8221;</title>
		<link>http://www.briangrinstead.com/blog/csharp-tips-type-conversion-with-as-and-is</link>
		<comments>http://www.briangrinstead.com/blog/csharp-tips-type-conversion-with-as-and-is#comments</comments>
		<pubDate>Tue, 21 Jul 2009 19:00:39 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[snippet]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.briangrinstead.com/blog/?p=305</guid>
		<description><![CDATA[I had used C# for at least a year before I found out a couple of nice shorthand ways to convert types. These are the is and as expressions, and using them can help make your code more readable. The is Expression The is expression will return true if the provided expression is non-null and [...]]]></description>
			<content:encoded><![CDATA[<p>I had used C# for at least a year before I found out a couple of nice shorthand ways to convert types.  These are the <a href="http://msdn.microsoft.com/en-us/library/scekt9xw%28VS.80%29.aspx">is</a> and <a href="http://msdn.microsoft.com/en-us/library/cscsdfbt%28VS.71%29.aspx">as</a> expressions, and using them can help make your code more readable.</p>

<h3>The is Expression</h3>

<p>The <em>is</em> expression will return true if the provided expression is non-null and can be cast to the specific type.</p>


<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;color: #0000FF;">private</span> <span style="color: #FF0000;color: #0000FF;">string</span> GetDisplayField<span style="color: #000000;color: #000000;">&#40;</span><span style="color: #FF0000;color: #0000FF;">object</span> val<span style="color: #000000;color: #000000;">&#41;</span>
<span style="color: #000000;color: #000000;">&#123;</span>
	<span style="color: #0600FF;color: #0000FF;">if</span> <span style="color: #000000;color: #000000;">&#40;</span>val <span style="color: #008000;color: #0000FF;">is</span> <span style="color: #FF0000;color: #0000FF;">string</span><span style="color: #000000;color: #000000;">&#41;</span>
	<span style="color: #000000;color: #000000;">&#123;</span>
		<span style="color: #FF0000;color: #0000FF;">string</span> text <span style="color: #008000;color: #000000;">=</span> <span style="color: #000000;color: #000000;">&#40;</span><span style="color: #FF0000;color: #0000FF;">string</span><span style="color: #000000;color: #000000;">&#41;</span>val<span style="color: #008000;color: #000000;">;</span>
		<span style="color: #0600FF;color: #0000FF;">return</span> <span style="color: #666666;color: #808080;">&quot;Value was text: &quot;</span> <span style="color: #008000;color: #000000;">+</span> text<span style="color: #008000;color: #000000;">;</span>
	<span style="color: #000000;color: #000000;">&#125;</span>
	<span style="color: #0600FF;color: #0000FF;">else</span> <span style="color: #0600FF;color: #0000FF;">if</span> <span style="color: #000000;color: #000000;">&#40;</span>val <span style="color: #008000;color: #0000FF;">is</span> DateTime<span style="color: #000000;color: #000000;">&#41;</span>
	<span style="color: #000000;color: #000000;">&#123;</span>
		DateTime time <span style="color: #008000;color: #000000;">=</span> <span style="color: #000000;color: #000000;">&#40;</span>DateTime<span style="color: #000000;color: #000000;">&#41;</span>val<span style="color: #008000;color: #000000;">;</span>
		<span style="color: #0600FF;color: #0000FF;">return</span> <span style="color: #666666;color: #808080;">&quot;Value was date: &quot;</span> <span style="color: #008000;color: #000000;">+</span> time.<span style="color: #0000FF;">ToShortDateString</span><span style="color: #000000;color: #000000;">&#40;</span><span style="color: #000000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
	<span style="color: #000000;color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;color: #0000FF;">return</span> <span style="color: #666666;color: #808080;">&quot;Could not match type&quot;</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #000000;color: #000000;">&#125;</span></pre></div></div>


<h3>The as Expression</h3>

<p>The <em>as</em> expression will try to cast the object into the given type, and returns an object of that type if the cast was successful, or return null if unsuccessful.</p>

<p>This buys a little bit more functionality for you, as it assigns the variable with an already casted value if successful.  It is functionally equivalent to: <code>expression is type ? (type)expression : (type)null</code></p>


<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;color: #0000FF;">private</span> <span style="color: #FF0000;color: #0000FF;">string</span> GetDisplayField<span style="color: #000000;color: #000000;">&#40;</span><span style="color: #FF0000;color: #0000FF;">object</span> val<span style="color: #000000;color: #000000;">&#41;</span>
<span style="color: #000000;color: #000000;">&#123;</span>
	<span style="color: #FF0000;color: #0000FF;">string</span> text <span style="color: #008000;color: #000000;">=</span> val <span style="color: #0600FF;color: #0000FF;">as</span> string<span style="color: #008000;color: #000000;">;</span>
	DateTime<span style="color: #008000;color: #000000;">?</span> time <span style="color: #008000;color: #000000;">=</span> val <span style="color: #0600FF;color: #0000FF;">as</span> DateTime<span style="color: #008000;color: #000000;">?;</span>
&nbsp;
	<span style="color: #0600FF;color: #0000FF;">if</span> <span style="color: #000000;color: #000000;">&#40;</span>text <span style="color: #008000;color: #000000;">!=</span> <span style="color: #0600FF;color: #0000FF;">null</span><span style="color: #000000;color: #000000;">&#41;</span>
	<span style="color: #000000;color: #000000;">&#123;</span>
		<span style="color: #0600FF;color: #0000FF;">return</span> <span style="color: #666666;color: #808080;">&quot;Value was text: &quot;</span> <span style="color: #008000;color: #000000;">+</span> text<span style="color: #008000;color: #000000;">;</span>
	<span style="color: #000000;color: #000000;">&#125;</span>
	<span style="color: #0600FF;color: #0000FF;">if</span> <span style="color: #000000;color: #000000;">&#40;</span>time.<span style="color: #0000FF;">HasValue</span><span style="color: #000000;color: #000000;">&#41;</span>
	<span style="color: #000000;color: #000000;">&#123;</span>
		<span style="color: #0600FF;color: #0000FF;">return</span> <span style="color: #666666;color: #808080;">&quot;Value was date: &quot;</span> <span style="color: #008000;color: #000000;">+</span> time.<span style="color: #0000FF;">Value</span>.<span style="color: #0000FF;">ToShortDateString</span><span style="color: #000000;color: #000000;">&#40;</span><span style="color: #000000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
	<span style="color: #000000;color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;color: #0000FF;">return</span> <span style="color: #666666;color: #808080;">&quot;Could not match type&quot;</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #000000;color: #000000;">&#125;</span></pre></div></div>


<h3>Testing out the functions</h3>

<p><br /></p>


<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;color: #000000;">&#40;</span>GetDisplayField<span style="color: #000000;color: #000000;">&#40;</span><span style="color: #FF0000;color: #808080;">1</span><span style="color: #000000;color: #000000;">&#41;</span><span style="color: #000000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>              <span style="color: #008080; font-style: italic;color: #008000;">// Output: &quot;Could not match type&quot;</span>
Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;color: #000000;">&#40;</span>GetDisplayField<span style="color: #000000;color: #000000;">&#40;</span><span style="color: #666666;color: #808080;">&quot;Hello&quot;</span><span style="color: #000000;color: #000000;">&#41;</span><span style="color: #000000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>        <span style="color: #008080; font-style: italic;color: #008000;">// Output: &quot;Value was text: Hello&quot;</span>
Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;color: #000000;">&#40;</span>GetDisplayField<span style="color: #000000;color: #000000;">&#40;</span>DateTime.<span style="color: #0000FF;">Now</span><span style="color: #000000;color: #000000;">&#41;</span><span style="color: #000000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>   <span style="color: #008080; font-style: italic;color: #008000;">// Output: &quot;Value was date: 7/21/2009&quot;</span></pre></div></div>


<p>They are both readable ways to perform a type conversion &#8211; but pick one or the other!  Using both of them is redundant.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.briangrinstead.com/blog/csharp-tips-type-conversion-with-as-and-is/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery outerHTML Snippet</title>
		<link>http://www.briangrinstead.com/blog/jquery-outerhtml-snippet</link>
		<comments>http://www.briangrinstead.com/blog/jquery-outerhtml-snippet#comments</comments>
		<pubDate>Tue, 16 Jun 2009 13:29:16 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://www.briangrinstead.com/?p=246</guid>
		<description><![CDATA[outerHTML is a property that is provided by Internet Explorer that returns the full HTML of an element (including start and end tags). In jQuery, the html() function returns the innerHTML of an element, which is just the HTML inside the element (not including the start and end tags). There came a time that I [...]]]></description>
			<content:encoded><![CDATA[<p><code>outerHTML</code> is a property that is provided by Internet Explorer that returns the full HTML of an element (including start and end tags).  In jQuery, the html() function returns the <code>innerHTML</code> of an element, which is just the HTML inside the element (not including the start and end tags).</p>

<p>There came a time that I wanted to get the <code>outerHTML</code> of an element, and I found that <a href="http://brandonaaron.net/blog/2007/06/17/jquery-snippets-outerhtml/">Brandon Aaron</a> shared a jQuery code snippet that does this exactly.  It does the trick for most cases, but there was one problem that I ran into.  <em>I wanted to get the outerHTML of an element inside of an iframe</em>, and I got a &#8216;Permission Denied&#8217; error in Internet Explorer.</p>

<p>The problem was that it was appending an element belonging to the iframes &#8216;contentDocument&#8217; into an element belonging to the global &#8216;document&#8217; element.<br />
Using the <a href="http://docs.jquery.com/Core/jQuery#htmlownerDocument">jQuery(html, ownerDocument)</a> overload of the jQuery core function, this error was fixed:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">fn</span>.<span style="color: #660066;">outerHTML</span> <span style="color: #339933;color: #000000;">=</span> <span style="color: #003366; font-weight: bold;color: #0000FF;">function</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
    <span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> doc <span style="color: #339933;color: #000000;">=</span> <span style="color: #000066; font-weight: bold;color: #0000FF;">this</span><span style="color: #009900;color: #000000;">&#91;</span><span style="color: #CC0000;color: #808080;">0</span><span style="color: #009900;color: #000000;">&#93;</span> <span style="color: #339933;color: #000000;">?</span> <span style="color: #000066; font-weight: bold;color: #0000FF;">this</span><span style="color: #009900;color: #000000;">&#91;</span><span style="color: #CC0000;color: #808080;">0</span><span style="color: #009900;color: #000000;">&#93;</span>.<span style="color: #660066;">ownerDocument</span> <span style="color: #339933;color: #000000;">:</span> document<span style="color: #339933;color: #000000;">;</span>
    <span style="color: #000066; font-weight: bold;color: #0000FF;">return</span> $<span style="color: #009900;color: #000000;">&#40;</span><span style="color: #3366CC;color: #808080;">'&lt;div&gt;'</span><span style="color: #339933;color: #000000;">,</span> doc<span style="color: #009900;color: #000000;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;color: #0000FF;">this</span>.<span style="color: #660066;">eq</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #CC0000;color: #808080;">0</span><span style="color: #009900;color: #000000;">&#41;</span>.<span style="color: #660066;">clone</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #009900;color: #000000;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
<span style="color: #009900;color: #000000;">&#125;</span><span style="color: #339933;color: #000000;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.briangrinstead.com/blog/jquery-outerhtml-snippet/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
