<?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 Monkey's Cubicle</title>
	<atom:link href="http://www.robfisher.us/blork/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.robfisher.us/blork</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Thu, 10 Dec 2009 16:27:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>LINQ on Form&#8217;s Controls</title>
		<link>http://www.robfisher.us/blork/?p=177</link>
		<comments>http://www.robfisher.us/blork/?p=177#comments</comments>
		<pubDate>Thu, 10 Dec 2009 16:24:47 +0000</pubDate>
		<dc:creator>robber.baron</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Visual Basic]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://www.robfisher.us/blork/?p=177</guid>
		<description><![CDATA[When loading a particular form in a project of mine I needed to shift the location of most of my controls to &#8220;hide&#8221; an unused textbox based on a boolean.  I was going to simply loop through each control and ignore the shift in location if the name values were the two controls that [...]]]></description>
			<content:encoded><![CDATA[<p>When loading a particular form in a project of mine I needed to shift the location of most of my controls to &#8220;hide&#8221; an unused textbox based on a boolean.  I was going to simply loop through each control and ignore the shift in location if the name values were the two controls that were fixed but I thought about how LINQ to Objects could help.  While this is simple and probably overkill the <em>potential</em> is awesome.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">Dim</span> res = From ctrl <span style="color: #000080;">In</span> Controls.OfType(Of <span style="color: #000080;">Control</span>)() _
             Where ctrl.Name &lt;&gt; <span style="color: #800000;">&quot;txtAddress&quot;</span> AndAlso ctrl.Name &lt;&gt; <span style="color: #800000;">&quot;lblAddress&quot;</span>
&nbsp;
<span style="color: #000080;">For</span> <span style="color: #000080;">Each</span> ctrl <span style="color: #000080;">As</span> <span style="color: #000080;">Control</span> <span style="color: #000080;">In</span> res
     ctrl.Top -= val
<span style="color: #000080;">Next</span></pre></td></tr></table></div>

<p>Admittedly this is pretty boring.  But say you wanted to change the background color of all textboxes on a form that contain a string greater than 255 and are not read only.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">Dim</span> res = From ctrl <span style="color: #000080;">In</span> Controls.OfType(Of TextBox)() _
             Where ctrl.Text.Length &gt; 255 <span style="color: #000080;">And</span> ctrl.ReadOnly = <span style="color: #000080;">False</span>
&nbsp;
<span style="color: #000080;">For</span> <span style="color: #000080;">Each</span> ctrl <span style="color: #000080;">As</span> <span style="color: #000080;">Control</span> <span style="color: #000080;">In</span> res
     ctrl.BackgroundColor = Colors.Red
<span style="color: #000080;">Next</span></pre></td></tr></table></div>

<p>Very quickly you can see how LINQ can be a convenient tool for all <em>sorts</em> of problems.  And ends up being very, very readable as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.robfisher.us/blork/?feed=rss2&amp;p=177</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Memory Stats on Mobile Device</title>
		<link>http://www.robfisher.us/blork/?p=174</link>
		<comments>http://www.robfisher.us/blork/?p=174#comments</comments>
		<pubDate>Thu, 10 Dec 2009 16:13:17 +0000</pubDate>
		<dc:creator>robber.baron</dc:creator>
				<category><![CDATA[Compact Framework]]></category>
		<category><![CDATA[Visual Basic]]></category>
		<category><![CDATA[compact framework]]></category>
		<category><![CDATA[Memory]]></category>

		<guid isPermaLink="false">http://www.robfisher.us/blork/?p=174</guid>
		<description><![CDATA[Recently I was working on a mobile project that was experiencing some serious memory leak issues.  It turns out that I wasn&#8217;t disposing for a SqlCeResultSet properly and on each query (of which there was substantial amount) I was calling New() without Dispose().
After a day of feeling like my project was falling apart before [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I was working on a mobile project that was experiencing some serious memory leak issues.  It turns out that I wasn&#8217;t disposing for a SqlCeResultSet properly and on each query (of which there was substantial amount) I was calling <code>New()</code> without <code>Dispose()</code>.</p>
<p>After a day of feeling like my project was falling apart before my eyes and getting weird and unreliable results from the .NETCF Remote Profiler I searched far a wide for <strong>some</strong> way to determine the available memory on a device.  On MSDN I found the following code (slightly modified by me) to get relevant information.</p>
<p>Basically two P/Invokes later I had the information I needed, saw that I was in fact leaking memory, quickly determined where, and restored sanity to my project.  Within thirty minutes of implementing this class the world was a happier place for me.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
</pre></td><td class="code"><pre class="vb" style="font-family:monospace;">Imports System.Text
&nbsp;
<span style="color: #000080;">Public</span> Class MEMORYSTATUSINFO
    <span style="color: #000080;">Public</span> Structure MEMORYSTATUS
        <span style="color: #000080;">Public</span> dwLength <span style="color: #000080;">As</span> UInt32
        <span style="color: #000080;">Public</span> dwMemoryLoad <span style="color: #000080;">As</span> UInt32
        <span style="color: #000080;">Public</span> dwTotalPhys <span style="color: #000080;">As</span> UInt32
        <span style="color: #000080;">Public</span> dwAvailPhys <span style="color: #000080;">As</span> UInt32
        <span style="color: #000080;">Public</span> dwTotalPageFile <span style="color: #000080;">As</span> UInt32
        <span style="color: #000080;">Public</span> dwAvailPageFile <span style="color: #000080;">As</span> UInt32
        <span style="color: #000080;">Public</span> dwTotalVirtual <span style="color: #000080;">As</span> UInt32
        <span style="color: #000080;">Public</span> dwAvailVirtual <span style="color: #000080;">As</span> UInt32
    <span style="color: #000080;">End</span> Structure
&nbsp;
    <span style="color: #000080;">Public</span> <span style="color: #000080;">Declare</span> <span style="color: #000080;">Function</span> GlobalMemoryStatus <span style="color: #000080;">Lib</span> <span style="color: #800000;">&quot;CoreDll.Dll&quot;</span> _
    (<span style="color: #000080;">ByRef</span> ms <span style="color: #000080;">As</span> MEMORYSTATUS) <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
&nbsp;
    <span style="color: #000080;">Public</span> <span style="color: #000080;">Declare</span> <span style="color: #000080;">Function</span> GetSystemMemoryDivision <span style="color: #000080;">Lib</span> <span style="color: #800000;">&quot;CoreDll.Dll&quot;</span> _
        (<span style="color: #000080;">ByRef</span> lpdwStorePages <span style="color: #000080;">As</span> UInt32, _
        <span style="color: #000080;">ByRef</span> ldpwRamPages <span style="color: #000080;">As</span> UInt32, _
        <span style="color: #000080;">ByRef</span> ldpwPageSize <span style="color: #000080;">As</span> UInt32) <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
&nbsp;
    <span style="color: #000080;">Public</span> Shared <span style="color: #000080;">Function</span> ShowMemory() <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>
        <span style="color: #000080;">Dim</span> storePages <span style="color: #000080;">As</span> UInt32
        <span style="color: #000080;">Dim</span> ramPages <span style="color: #000080;">As</span> UInt32
        <span style="color: #000080;">Dim</span> pageSize <span style="color: #000080;">As</span> UInt32
        <span style="color: #000080;">Dim</span> res <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span> = _
            GetSystemMemoryDivision(storePages, ramPages, pageSize)
&nbsp;
        <span style="color: #008000;">' Call the native GlobalMemoryStatus method
</span>        <span style="color: #008000;">' with the defined structure.
</span>        <span style="color: #000080;">Dim</span> memStatus <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> MEMORYSTATUS
        GlobalMemoryStatus(memStatus)
&nbsp;
        <span style="color: #000080;">Dim</span> load <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span> = memStatus.dwMemoryLoad / (1024 * 1024)
        <span style="color: #000080;">Dim</span> totPhys <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span> = memStatus.dwTotalPhys / (1024 * 1024)
        <span style="color: #000080;">Dim</span> availPhys <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span> = memStatus.dwAvailPhys / (1024 * 1024)
        <span style="color: #000080;">Dim</span> totalPageFile <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span> = memStatus.dwTotalPageFile
        <span style="color: #000080;">Dim</span> availPageFile <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span> = memStatus.dwAvailPageFile
        <span style="color: #000080;">Dim</span> totVirtual <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span> = memStatus.dwTotalVirtual / (1024 * 1024)
        <span style="color: #000080;">Dim</span> availVirtual <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span> = memStatus.dwAvailVirtual / (1024 * 1024)
&nbsp;
        <span style="color: #008000;">' Use a StringBuilder for the message string.
</span>        <span style="color: #000080;">Dim</span> MemoryInfo <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> StringBuilder
        MemoryInfo.<span style="color: #000080;">Append</span>(<span style="color: #800000;">&quot;Memory Load: &quot;</span> _
            &amp; load.ToString() &amp; <span style="color: #800000;">&quot;Mb&quot;</span> &amp; vbCrLf)
        MemoryInfo.<span style="color: #000080;">Append</span>(<span style="color: #800000;">&quot;Total Physical: &quot;</span> _
            &amp; totPhys &amp; <span style="color: #800000;">&quot;Mb&quot;</span> &amp; vbCrLf)
        MemoryInfo.<span style="color: #000080;">Append</span>(<span style="color: #800000;">&quot;Avail Physical: &quot;</span> _
            &amp; availPhys &amp; <span style="color: #800000;">&quot;Mb&quot;</span> &amp; vbCrLf)
        MemoryInfo.<span style="color: #000080;">Append</span>(<span style="color: #800000;">&quot;Total Page File: &quot;</span> _
            &amp; totalPageFile.ToString() &amp; vbCrLf)
        MemoryInfo.<span style="color: #000080;">Append</span>(<span style="color: #800000;">&quot;Avail Page File: &quot;</span> _
            &amp; availPageFile.ToString() &amp; vbCrLf)
        MemoryInfo.<span style="color: #000080;">Append</span>(<span style="color: #800000;">&quot;Total Virtual: &quot;</span> _
            &amp; totVirtual.ToString() &amp; <span style="color: #800000;">&quot;Mb&quot;</span> &amp; vbCrLf)
        MemoryInfo.<span style="color: #000080;">Append</span>(<span style="color: #800000;">&quot;Avail Virtual: &quot;</span> _
            &amp; availVirtual.ToString() &amp; <span style="color: #800000;">&quot;Mb&quot;</span> &amp; vbCrLf)
&nbsp;
        <span style="color: #008000;">' Show the available memory.
</span>        Return MemoryInfo.ToString()
    <span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
<span style="color: #000080;">End</span> Class</pre></td></tr></table></div>

<p>Hope this helps someone <em>else</em> find greedy memory hog code in their apps.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.robfisher.us/blork/?feed=rss2&amp;p=174</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linq on the Compact Framework</title>
		<link>http://www.robfisher.us/blork/?p=171</link>
		<comments>http://www.robfisher.us/blork/?p=171#comments</comments>
		<pubDate>Thu, 03 Dec 2009 19:10:32 +0000</pubDate>
		<dc:creator>robber.baron</dc:creator>
				<category><![CDATA[Compact Framework]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Visual Basic]]></category>

		<guid isPermaLink="false">http://www.robfisher.us/blork/?p=171</guid>
		<description><![CDATA[I am a large fan of Linq when coding.  I think it is a really clean and concise way to access large sets of data (whether in a database, xml, or a IList) quickly, efficiently, and in a clearly readable way.
I have been developing almost exclusively within the compact framework and while the compact [...]]]></description>
			<content:encoded><![CDATA[<p>I am a large fan of Linq when coding.  I think it is a really clean and concise way to access large sets of data (whether in a database, xml, or a IList<T>) quickly, efficiently, and in a clearly readable way.</p>
<p>I have been developing almost exclusively within the compact framework and while the compact framework (3.5) does not include Linq to Sql it does include Linq to XML and Linq to Objects.  The problem I had was getting it to work in the compact framework.</p>
<p>While <code>Imports System.Linq</code> always failed to a lack of reference it was because there is no System.Linq.dll.  The linq parts of the language are included within <code>System.Core</code>.  Add that reference to your project and Linq will work for you.</p>
<p>Don&#8217;t know why it took me so long to realize this.  But now I&#8217;m Linq free.</p>
<p>I also hear that Compact Framework 4.0 will support Linq to Sql.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.robfisher.us/blork/?feed=rss2&amp;p=171</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Email Errors with log4net</title>
		<link>http://www.robfisher.us/blork/?p=165</link>
		<comments>http://www.robfisher.us/blork/?p=165#comments</comments>
		<pubDate>Wed, 11 Nov 2009 17:15:54 +0000</pubDate>
		<dc:creator>robber.baron</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[log4net]]></category>
		<category><![CDATA[smtpappender]]></category>

		<guid isPermaLink="false">http://www.robfisher.us/blork/?p=165</guid>
		<description><![CDATA[On a project I have been working on I was having a terrible time getting errors from the client.  There were a plethora of database errors that I could not get to replicate on my local machine.  Simple features like adding an object to the database or editing that object to the database would error.  [...]]]></description>
			<content:encoded><![CDATA[<p>On a project I have been working on I was having a terrible time getting errors from the client.  There were a plethora of database errors that I could not get to replicate on my local machine.  Simple features like adding an object to the database or editing that object to the database would error.  What is worse is that the client would not properly transcribe the error message for my debugging purposes.</p>
<p>To alleviate this pain I used log4net to send all errors to an email address.  This way any errors that occur within the system can be sent directly to me, without user interaction, so I might investigate without the need for a middleman.  The configuration was quite simple.</p>
<p>Within app.config:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;appender</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;smtpAppender&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;log4net.Appender.SmtpAppender&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;to</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;[To address]&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;from</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;[From address]&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;subject</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Error&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;authentication</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;basic&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;smtpHost</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;[SMTP server]&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;username</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;[SMTP username]&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;password</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;[SMTP password]&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;port</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;[SMTP port]&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bufferSize</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;10&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;lossy</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;evaluator</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;log4net.Core.LevelEvaluator&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;threshold</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;ERROR&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/evaluator<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;layout</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;log4net.Layout.PatternLayout&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;conversionPattern</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/layout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/appender<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>This creates an appender that will send an email to the desired address with the proper smtp settings.  Once the appender is created you need to create a logger that uses the appender.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;logger</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;error-logger&quot;</span> <span style="color: #000066;">additivity</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;level</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;ERROR&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;appender-ref</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;smtpAppender&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> 
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/logger<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Then whenever you want to send an error via email you just log the error as an error using the <code>error-logger</code> logger.  This will send an email.</p>
<p>Personally I like giving the option to send in case the user knows what went wrong to save on spam but it isn&#8217;t a requirement.  This set up worked nicely for me.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.robfisher.us/blork/?feed=rss2&amp;p=165</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Value Objects</title>
		<link>http://www.robfisher.us/blork/?p=104</link>
		<comments>http://www.robfisher.us/blork/?p=104#comments</comments>
		<pubDate>Fri, 23 Oct 2009 19:51:40 +0000</pubDate>
		<dc:creator>robber.baron</dc:creator>
				<category><![CDATA[DDD]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.robfisher.us/blork/?p=104</guid>
		<description><![CDATA[Value Objects
A value object is essentially data without a sense of identity.  This simplifies them as objects within a system because they are immutable (once created they should never change), validated (since they never change should always be in a valid state), and equal (since they have no identity, two value objects with the same [...]]]></description>
			<content:encoded><![CDATA[<h3>Value Objects</h3>
<p>A value object is essentially data without a sense of identity.  This simplifies them as objects within a system because they are <strong>immutable</strong> (once created they should never change), <strong>validated</strong> (since they never change should always be in a valid state), and <strong>equal</strong> (since they have no identity, two value objects with the same data are equivalent).</p>
<p>The best way to see this is via example.  A good example of this is money.  If you have a value object representing a sum of money then two instances of that object worth $20.00USD are equivalent regardless of the makeup of that money.  A store doesn&#8217;t care if you provide a single twenty dollar bill, two tens, four fives, or twenty ones.  They only are about receiving twenty dollars.  Thus once that value is added to their system its identity (the specific bills you handed to the cashier) is irrelevant.  A perfect example of a value object.</p>
<h4>Immutability</h4>
<p>If an object has no sense of identity beyond the value of its members then to change its members is to change its identities.  If you live at a particular address, the summation of street address, city, state, and zip code create the identity of that address.  The moment you change a property of your address (say zip code) the address is no longer <em>your</em> address.  Its identity is altered.  Thus value objects should not expose means to edit their properties.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Address
<span style="color: #000000;">&#123;</span>
     <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> StreetAddress <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF;">private</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
     <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> City <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF;">private</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
     <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> State <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF;">private</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
     <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Zip <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF;">private</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
     <span style="color: #0600FF;">public</span> Address<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> streetAddress, <span style="color: #FF0000;">string</span> city, <span style="color: #FF0000;">string</span> state, <span style="color: #FF0000;">string</span> zip<span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
          StreetAddress <span style="color: #008000;">=</span> streetAddress<span style="color: #008000;">;</span>
          City <span style="color: #008000;">=</span> city<span style="color: #008000;">;</span>
          State <span style="color: #008000;">=</span> state<span style="color: #008000;">;</span>
          Zip <span style="color: #008000;">=</span> zip<span style="color: #008000;">;</span>
     <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Notice that the only way to give the object value is via its constructor.  This insures that its data (and thus its identity) is never changed.  It makes the object immutable.</p>
<h4>Validity</h4>
<p>If an object cannot be changed then it better always be valid.  For this reason it makes sense to check that all values provided to the constructor are valid.  Thus insuring the object is always in a valid state.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Address
<span style="color: #000000;">&#123;</span>
     <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> StreetAddress <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF;">private</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
     <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> City <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF;">private</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
     <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> State <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF;">private</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
     <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Zip <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF;">private</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
     <span style="color: #0600FF;">public</span> Address<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> streetAddress, <span style="color: #FF0000;">string</span> city, <span style="color: #FF0000;">string</span> state, <span style="color: #FF0000;">string</span> zip<span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
          <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>streetAddress<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
               <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Address requires street address&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
          <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>city<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
               <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Address requires city&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
          <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>state<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
               <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Address requires state&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
          <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>zip<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
               <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Address requires zip&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
          StreetAddress <span style="color: #008000;">=</span> streetAddress<span style="color: #008000;">;</span>
          City <span style="color: #008000;">=</span> city<span style="color: #008000;">;</span>
          State <span style="color: #008000;">=</span> state<span style="color: #008000;">;</span>
          Zip <span style="color: #008000;">=</span> zip<span style="color: #008000;">;</span>
     <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<h4>Equality</h4>
<p>Since value objects are the same if their properties are the same it makes testing their equality simple.  It becomes a simple matter of testing their properties.  For readability and convenience I tend to overload operators and the Equals method.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Address <span style="color: #008000;">:</span> IEquatable<span style="color: #008000;">&lt;</span>Address<span style="color: #008000;">&gt;</span>
<span style="color: #000000;">&#123;</span>
     <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> StreetAddress <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF;">private</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
     <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> City <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF;">private</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
     <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> State <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF;">private</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
     <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Zip <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF;">private</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
     <span style="color: #0600FF;">public</span> Address<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> streetAddress, <span style="color: #FF0000;">string</span> city, <span style="color: #FF0000;">string</span> state, <span style="color: #FF0000;">string</span> zip<span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
          <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>streetAddress<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
               <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Address requires street address&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
          <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>city<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
               <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Address requires city&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
          <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>state<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
               <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Address requires state&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
          <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>zip<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
               <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Address requires zip&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
          StreetAddress <span style="color: #008000;">=</span> streetAddress<span style="color: #008000;">;</span>
          City <span style="color: #008000;">=</span> city<span style="color: #008000;">;</span>
          State <span style="color: #008000;">=</span> state<span style="color: #008000;">;</span>
          Zip <span style="color: #008000;">=</span> zip<span style="color: #008000;">;</span>
     <span style="color: #000000;">&#125;</span>
&nbsp;
     <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">bool</span> Equals<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> obj<span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
          <span style="color: #0600FF;">return</span> Equals<span style="color: #000000;">&#40;</span>obj <span style="color: #0600FF;">as</span> Address<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
     <span style="color: #000000;">&#125;</span>
&nbsp;
     <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> Equals<span style="color: #000000;">&#40;</span>Address obj<span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
          <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>obj <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
               <span style="color: #0600FF;">return</span> false<span style="color: #008000;">;</span>
          <span style="color: #0600FF;">return</span> StreetAddress.<span style="color: #0000FF;">Equals</span><span style="color: #000000;">&#40;</span>obj.<span style="color: #0000FF;">StreetAddress</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span>
                   City.<span style="color: #0000FF;">Equals</span><span style="color: #000000;">&#40;</span>obj.<span style="color: #0000FF;">City</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span>
                   State.<span style="color: #0000FF;">Equals</span><span style="color: #000000;">&#40;</span>obj.<span style="color: #0000FF;">State</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span>
                   Zip.<span style="color: #0000FF;">Equals</span><span style="color: #000000;">&#40;</span>obj.<span style="color: #0000FF;">Zip</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
     <span style="color: #000000;">&#125;</span>
&nbsp;
     <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">bool</span> <span style="color: #0600FF;">operator</span> <span style="color: #008000;">==</span><span style="color: #000000;">&#40;</span>Address left, Address right<span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
          <span style="color: #0600FF;">return</span> Equals<span style="color: #000000;">&#40;</span>left, right<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
     <span style="color: #000000;">&#125;</span>
&nbsp;
     <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">bool</span> <span style="color: #0600FF;">operator</span> <span style="color: #008000;">!=</span><span style="color: #000000;">&#40;</span>Address left, Address right<span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
          <span style="color: #0600FF;">return</span> <span style="color: #008000;">!</span>Equals<span style="color: #000000;">&#40;</span>left, right<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
     <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This results in a rather nice value object to work within code.  Ensuring that the three important properties of a value object are met within your code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.robfisher.us/blork/?feed=rss2&amp;p=104</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>LINQ to XML: A Mild Success Story</title>
		<link>http://www.robfisher.us/blork/?p=148</link>
		<comments>http://www.robfisher.us/blork/?p=148#comments</comments>
		<pubDate>Thu, 15 Oct 2009 18:24:00 +0000</pubDate>
		<dc:creator>robber.baron</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.robfisher.us/blork/?p=148</guid>
		<description><![CDATA[In a project I am working on I&#8217;ve come to discover the need to stitch together multiple data sources.  The product itself needs to maintain a certain level of information within a database but it must also sync with a standalone application that is exporting its data.
Initially I thought this would be a nightmare [...]]]></description>
			<content:encoded><![CDATA[<p>In a project I am working on I&#8217;ve come to discover the need to stitch together multiple data sources.  The product itself needs to maintain a certain level of information within a database but it must also sync with a standalone application that is exporting its data.</p>
<p>Initially I thought this would be a nightmare to handle.  Getting the exported data as XML, looping and parsing through the XML document and creating a new class and inserting it into the proper table so my project can use it.  Thankfully I am all about LINQ these days (with the exception of LINQ-to-SQL) and LINQ to XML made my life amazingly simple.</p>
<p>Let&#8217;s take the following XML file:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;PecanProject<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Products<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Product<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Black Widget<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0.86<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Product<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Product<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Yellow Widget<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0.99<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Product<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Product<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Green Widget<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.25<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Product<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Product<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>4<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Red Widget<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.11<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Product<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Products<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/PecanProject<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Traditionally this sort of file would be easier to work with than a plain text file but it certainly isn&#8217;t elegant.  Using LINQ to XML I can very elegantly select from the XML as though it were a normal data source and then map the results into a class.</p>
<p>Here is how:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var doc <span style="color: #008000;">=</span> XElement.<span style="color: #0000FF;">Load</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;filename.xml&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
var projectList <span style="color: #008000;">=</span> from prod <span style="color: #0600FF;">in</span> doc.<span style="color: #0000FF;">Descendants</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Product&quot;</span><span style="color: #000000;">&#41;</span>
                       select <span style="color: #008000;">new</span> Product <span style="color: #000000;">&#123;</span>
                            Id <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span>prod.<span style="color: #0000FF;">Element</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Id&quot;</span><span style="color: #000000;">&#41;</span>,
                            Name <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span>prod.<span style="color: #0000FF;">Element</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Name&quot;</span><span style="color: #000000;">&#41;</span>,
                            Price <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">double</span><span style="color: #000000;">&#41;</span>prod.<span style="color: #0000FF;">Element</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Price&quot;</span><span style="color: #000000;">&#41;</span>,                       
                        <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var product <span style="color: #0600FF;">in</span> projectList<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
     <span style="color: #008080; font-style: italic;">// Do something with your new product.</span>
     PerformActionOnProduct<span style="color: #000000;">&#40;</span>product<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p><small>This of course assumes a Product class that has three public properties named Id, Name, and Price.</small></p>
<p>This was amazingly swift, easy to code, easy to read, and will be easy to maintain.  Not only that but you can actually perform calculations on the incoming code.  I, for example, am importing Customers and send the address id to the address repository to actually return the proper class, rather than just the id.</p>
<p>It is a very clean way of doing things.  I am in love.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.robfisher.us/blork/?feed=rss2&amp;p=148</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>log4net with Compact Framework</title>
		<link>http://www.robfisher.us/blork/?p=143</link>
		<comments>http://www.robfisher.us/blork/?p=143#comments</comments>
		<pubDate>Tue, 13 Oct 2009 19:29:00 +0000</pubDate>
		<dc:creator>robber.baron</dc:creator>
				<category><![CDATA[Compact Framework]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[compact framework]]></category>
		<category><![CDATA[log4net]]></category>
		<category><![CDATA[logging]]></category>

		<guid isPermaLink="false">http://www.robfisher.us/blork/?p=143</guid>
		<description><![CDATA[My favorite logging library log4net work not only on desktop and web applications but can be used on mobile devices with the compact framework.
Granted not all of the Appenders make sense within the compact framework.  Since mobile devices have neither a system log nor a console it doesn&#8217;t make much sense to have these [...]]]></description>
			<content:encoded><![CDATA[<p>My favorite logging library log4net work not only on desktop and web applications but can be used on mobile devices with the compact framework.</p>
<p>Granted not all of the Appenders make sense within the compact framework.  Since mobile devices have neither a system log nor a console it doesn&#8217;t make much sense to have these appenders.  For a complete list of supported frameworks and appenders <a title="Apache log4net: Supported Frameworks" href="http://logging.apache.org/log4net/release/framework-support.html" target="_blank">see here</a>.</p>
<p>Applications written in the compact framework don&#8217;t have app.config files.  This presents an issue when attempting to create an <code>ILog</code>.  Here is my work around.</p>
<p>First create an XML that will be copied to source on deployment.  Here is an example:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;log4net</span> <span style="color: #000066;">debug</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;appender</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;logger&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;log4net.Appender.RollingFileAppender&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;file</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;\SD Card\Assessment\DamageAssessmentLog.txt&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;appendToFile</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;maxSizeRollBackups</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;2&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;maximumFileSize</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;1048576&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rollingStyle</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Composite&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;staticLogFileName</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;layout</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;log4net.Layout.PatternLayout&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;header</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;[Header]&amp;#13;&amp;#10;&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;footer</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;[Footer]&amp;#13;&amp;#10;&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;conversionPattern</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;%newline Date: %date ,Application: %a ,Logger: %logger ,Thread: [%thread] ,Level: %level %newline Message: %message%newline Exception: %exception%newline&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/layout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/appender<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #808080; font-style: italic;">&lt;!-- Setup the root category, add the appenders and set the default level --&gt;</span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;root<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;level</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;DEBUG&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;appender-ref</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;logger&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/root<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/log4net<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>This should all be familiar so far.  Basically whatever was going to go into app.config will now be in this file.</p>
<p>Then within your application&#8217;s startup you need to explicitly point log4net to that configuration file.</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">Shared <span style="color: #000080;">Sub</span> Main()
        Try
            <span style="color: #000080;">Dim</span> logconfig = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase), <span style="color: #800000;">&quot;logging.config&quot;</span>)
&nbsp;
            log4net.Config.XmlConfigurator.Configure(<span style="color: #000080;">New</span> FileInfo(logconfig))
&nbsp;
            <span style="color: #000080;">Dim</span> logger <span style="color: #000080;">As</span> ILog = log4net.LogManager.GetLogger(<span style="color: #800000;">&quot;logger&quot;</span>)
&nbsp;
            Using map <span style="color: #000080;">As</span> formMap = <span style="color: #000080;">New</span> formMap()
                logger.Debug(<span style="color: #800000;">&quot;Staring application&quot;</span>)
                map.ShowDialog()
                logger.Debug(<span style="color: #800000;">&quot;Application closed&quot;</span>)
            <span style="color: #000080;">End</span> Using
            AssessmentConnector.Instance.CloseConnection()
        Catch ex <span style="color: #000080;">As</span> Exception
            MessageBox.Show(ex.ToString)
        Finally
            log4net.LogManager.Shutdown()
        <span style="color: #000080;">End</span> Try
    <span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span></pre></div></div>

<p>Here you can see that I get the application&#8217;s executing path and append the <code>xml</code> file&#8217;s name.  Then I call the <code>log4net.Config.XmlConfigurator</code> to initialize the logging service from the provided <code>xml</code> file.</p>
<p><strong>Note:</strong> Due to limitations of the compact framework it is important that you call <code>log4net.LogManager.Shutdown()</code> to explicitly close your log files.  Otherwise you&#8217;ll end up with a hanging process and open log files.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.robfisher.us/blork/?feed=rss2&amp;p=143</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Scope Identity with SqlCe</title>
		<link>http://www.robfisher.us/blork/?p=137</link>
		<comments>http://www.robfisher.us/blork/?p=137#comments</comments>
		<pubDate>Tue, 06 Oct 2009 19:25:29 +0000</pubDate>
		<dc:creator>robber.baron</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[compact framework]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[scope identity]]></category>

		<guid isPermaLink="false">http://www.robfisher.us/blork/?p=137</guid>
		<description><![CDATA[Normally I write my data access layer using an ORM like nHibernate as I thoroughly enjoy the speed, flexibility and power that comes from using such tools.  Recently at work, however, I have been doing a lot of Windows Mobile development and I although there are compact framework ORMs I didn&#8217;t have the experience [...]]]></description>
			<content:encoded><![CDATA[<p>Normally I write my data access layer using an ORM like nHibernate as I thoroughly enjoy the speed, flexibility and power that comes from using such tools.  Recently at work, however, I have been doing a lot of Windows Mobile development and I although there are compact framework ORMs I didn&#8217;t have the experience enough to want to use one in a very important project.</p>
<p>As a result the following code is something I had to look up and was actually an annoying thing to find.  I wanted to return the identity of a newly inserted object.  Normally nHibernate would set this for me but as I&#8217;m not using nHibernate I have to do this myself.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>connection <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SqlCeConnection<span style="color: #000000;">&#40;</span>CONNECTION_STRING<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
     connection.<span style="color: #0000FF;">Open</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
     var command <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SqlCeCommand<span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;INSERT INTO photos (path, format) VALUE (?, ?) SELECT SCOPE_IDENTITY();&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
     command.<span style="color: #0000FF;">Parameters</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">New</span> SqlCeParameter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
     command.<span style="color: #0000FF;">Parameters</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">New</span> SqlCeParameter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
     command.<span style="color: #0000FF;">Parameters</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Value</span> <span style="color: #008000;">=</span> path<span style="color: #008000;">;</span>
     command.<span style="color: #0000FF;">Parameters</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Value</span> <span style="color: #008000;">=</span> format<span style="color: #008000;">;</span>
&nbsp;
     var id <span style="color: #008000;">=</span> command.<span style="color: #0000FF;">ExecuteScalar</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
     Debug.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">String</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;New Identifier = {0}&quot;</span>, id<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This is insert the photo into the database and return the new id for the photo rather than the number of rows effected.  The key is the <code>ExecuteScalar</code> function.</p>
<p>How cool is it that log4net works for Compact Framework apps.  Has been a huge lifesaver.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.robfisher.us/blork/?feed=rss2&amp;p=137</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dictionary Iteration</title>
		<link>http://www.robfisher.us/blork/?p=131</link>
		<comments>http://www.robfisher.us/blork/?p=131#comments</comments>
		<pubDate>Fri, 02 Oct 2009 16:36:00 +0000</pubDate>
		<dc:creator>robber.baron</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[dictionary iterator .net]]></category>

		<guid isPermaLink="false">http://www.robfisher.us/blork/?p=131</guid>
		<description><![CDATA[In a project I&#8217;m working on I am storing results in a dictionary.  While I could just be storing them in a list I thought the speed and ease of use was better for a dictionary.  [In fact it turned out to be a great choice because due to collisions in the results I discovered [...]]]></description>
			<content:encoded><![CDATA[<p>In a project I&#8217;m working on I am storing results in a dictionary.  While I could just be storing them in a list I thought the speed and ease of use was better for a dictionary.  [In fact it turned out to be a great choice because due to collisions in the results I discovered that the data was actually bad and needed to be cleaned up.]</p>
<p>Once I had the results through I needed loop through them to export them to a better file format.  At first I wasn&#8217;t exactly sure how to do this.  I new a dictionary implemented the IEnumerable interface I just didn&#8217;t know how to access it.</p>
<p>Here is how:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var dictionary <span style="color: #0600FF;">as</span> <span style="color: #008000;">new</span> Dictionary<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
dictionary.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>obj1.<span style="color: #0000FF;">GetHashCode</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, obj1<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
dictionary.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>obj2.<span style="color: #0000FF;">GetHashCode</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, obj2<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
dictionary.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>obj3.<span style="color: #0000FF;">GetHashCode</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, obj3<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>KeyValuePair result <span style="color: #0600FF;">in</span> dictionary<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
     Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">String</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Key = {0} - Value = {1}&quot;</span>, result.<span style="color: #0000FF;">Key</span>, result.<span style="color: #0000FF;">Value</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.robfisher.us/blork/?feed=rss2&amp;p=131</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Visual Basic Conditionals</title>
		<link>http://www.robfisher.us/blork/?p=124</link>
		<comments>http://www.robfisher.us/blork/?p=124#comments</comments>
		<pubDate>Thu, 01 Oct 2009 18:53:36 +0000</pubDate>
		<dc:creator>robber.baron</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Visual Basic]]></category>

		<guid isPermaLink="false">http://www.robfisher.us/blork/?p=124</guid>
		<description><![CDATA[At my current job I write far more Visual Basic code than I do C# code.  Having started programming in C++ in junior year of high school and using almost exclusively C++ and Java in college, I tend to favor C based languages.  One of the more natural things about C(++/#) that I [...]]]></description>
			<content:encoded><![CDATA[<p>At my current job I write far more Visual Basic code than I do C# code.  Having started programming in C++ in junior year of high school and using almost exclusively C++ and Java in college, I tend to favor C based languages.  One of the more natural things about C(++/#) that I found lacking in Visual Basic was intelligent conditional testing.</p>
<p>For example:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">Dim</span> student <span style="color: #000080;">as</span> Student = <span style="color: #000080;">Nothing</span>
&nbsp;
<span style="color: #000080;">If</span> student IsNot <span style="color: #000080;">Nothing</span> <span style="color: #000080;">And</span> student.Name = <span style="color: #800000;">&quot;robber.baron&quot;</span> <span style="color: #000080;">Then</span>
     GiveStudentAnA(student)
EndIf</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var student <span style="color: #0600FF;">as</span> Student <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>student <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span> <span style="color: #008000;">&amp;</span>amp<span style="color: #008000;">;&amp;</span>amp<span style="color: #008000;">;</span> student.<span style="color: #0000FF;">Name</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;robber.baron&quot;</span><span style="color: #000000;">&#41;</span>
     GiveStudentAnA<span style="color: #000000;">&#40;</span>student<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>In the C# code when the conditional is a logical And (signified by the double ampersand) and the first condition fails it immediately exits the conditional.  Since both values must be true to continue if the first is false the conditional fails and there is no need to evaluate the second condition.</p>
<p>In the Visual Basic code you will get a &#8220;Object Reference not set to Instance of Object&#8221; exception because Visual Basic does not implicitly have this feature.  The test for null will fail but the second condition will <em>still be evaluated!</em> I never liked this feature and found it to be a concrete failure in Visual Basic.</p>
<p>Recently I discovered that of course Visual Basic allows you to do this.  (The CLR is the CLR is the CLR.  It is all common under the hood.)  It just needs to you explicitly request this behavior using the <strong><code>AndAlso</code></strong> or <strong><code>OrElse</code></strong> keywords.</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">Dim</span> student <span style="color: #000080;">as</span> Student = <span style="color: #000080;">Nothing</span>
&nbsp;
<span style="color: #000080;">If</span> student IsNot <span style="color: #000080;">Nothing</span> AndAlso student.Name = <span style="color: #800000;">&quot;robber.baron&quot;</span> <span style="color: #000080;">Then</span>
     GiveStudentAnA(student)
EndIf</pre></div></div>

<p>This is how you would properly translate the C# code above into Visual Basic.</p>
<p>If you wanted the inverse, you only wanted the right side condition to evaluate only if the left is true you could do the following:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">Dim</span> student <span style="color: #000080;">as</span> Student = <span style="color: #000080;">new</span> Student(<span style="color: #800000;">&quot;robber.baron&quot;</span>, <span style="color: #800000;">&quot;C&quot;</span>)
&nbsp;
<span style="color: #000080;">If</span> student.Name = <span style="color: #800000;">&quot;robber.baron&quot;</span> OrElse <span style="color: #000080;">Not</span> student.Grade = <span style="color: #800000;">&quot;A&quot;</span> <span style="color: #000080;">Then</span>
     GiveStudentAnA(student)
EndIf</pre></div></div>

<p>Finally I can combine null checks and logic checks into a single conditional correctly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.robfisher.us/blork/?feed=rss2&amp;p=124</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
