<?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>I&#039;m CaLendarW Blog &#187; coding snippet</title>
	<atom:link href="http://wongkalun.idv.hk/category/computer-science/coding/feed/" rel="self" type="application/rss+xml" />
	<link>http://wongkalun.idv.hk</link>
	<description>Put the technology in correct places</description>
	<lastBuildDate>Sun, 18 Apr 2010 16:19:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>yield keyword</title>
		<link>http://wongkalun.idv.hk/2009/10/29/yield-keyword/</link>
		<comments>http://wongkalun.idv.hk/2009/10/29/yield-keyword/#comments</comments>
		<pubDate>Wed, 28 Oct 2009 16:18:59 +0000</pubDate>
		<dc:creator>calendarw</dc:creator>
				<category><![CDATA[coding snippet]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://wongkalun.idv.hk/?p=627</guid>
		<description><![CDATA[近期因為睇 ASP.net MVC 既 example, 中途見到 yield keyword 既 usage, 感覺上幾好用, 以學多個 keyword 既原則黎講, 我當然會在 project 中試用, 當中既 validation 用法我覺得幾好. public partial class Dinner { public bool IsHostedBy(string userName) { return HostedBy.Equals(userName, StringComparison.InvariantCultureIgnoreCase); } public bool IsValid { get { return (GetRuleViolations().Count() == 0); } } public IEnumerable&#60;RuleViolation&#62; GetRuleViolations() { if (String.IsNullOrEmpty(Title)) yield return new [...]]]></description>
			<content:encoded><![CDATA[<p>近期因為睇 ASP.net MVC 既 example, 中途見到 <a href="http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx">yield</a> keyword 既 usage, 感覺上幾好用, 以學多個 keyword 既原則黎講, 我當然會在 project 中試用, 當中既 validation 用法我覺得幾好.</p>
<pre class="brush: csharp;">
public partial class Dinner {

    public bool IsHostedBy(string userName) {
        return HostedBy.Equals(userName, StringComparison.InvariantCultureIgnoreCase);
    }

    public bool IsValid {
        get { return (GetRuleViolations().Count() == 0); }
    }

    public IEnumerable&lt;RuleViolation&gt; GetRuleViolations() {

        if (String.IsNullOrEmpty(Title))
            yield return new RuleViolation(&quot;Title is required&quot;, &quot;Title&quot;);

        if (String.IsNullOrEmpty(Description))
            yield return new RuleViolation(&quot;Description is required&quot;, &quot;Description&quot;);

        if (String.IsNullOrEmpty(HostedBy))
            yield return new RuleViolation(&quot;HostedBy is required&quot;, &quot;HostedBy&quot;);

        if (String.IsNullOrEmpty(Address))
            yield return new RuleViolation(&quot;Address is required&quot;, &quot;Address&quot;);

        if (String.IsNullOrEmpty(Country))
            yield return new RuleViolation(&quot;Country is required&quot;, &quot;Address&quot;);

        if (String.IsNullOrEmpty(ContactPhone))
            yield return new RuleViolation(&quot;Phone# is required&quot;, &quot;ContactPhone&quot;);

        if (!PhoneValidator.IsValidNumber(ContactPhone, Country))
            yield return new RuleViolation(&quot;Phone# does not match country&quot;, &quot;ContactPhone&quot;);

        yield break;
    }
}
</pre>
<p>但當我想在 project 度用果時, 開始感覺到麻煩, 因為本身個 project 係要在 .net 2.0 既環境下運作, 而 IEnumerable<T>.Count 係未有支援, 而且無 <a href="http://msdn.microsoft.com/en-us/library/bb383977.aspx">extension methods</a> 既支援, 所以要另外寫個 class 黎 handle 呢樣野都幾麻煩, 因此我都無乜 idea 在其他方面可以實際上使用得適當&#8230;&#8230;</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-enjoy">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://wongkalun.idv.hk/2009/10/29/yield-keyword/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://wongkalun.idv.hk/2009/10/29/yield-keyword/&amp;title=yield+keyword" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://wongkalun.idv.hk/2009/10/29/yield-keyword/&amp;title=yield+keyword" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://wongkalun.idv.hk/2009/10/29/yield-keyword/&amp;title=yield+keyword&amp;desc=%E8%BF%91%E6%9C%9F%E5%9B%A0%E7%82%BA%E7%9D%87%20ASP.net%20MVC%20%E6%97%A2%20example%2C%20%E4%B8%AD%E9%80%94%E8%A6%8B%E5%88%B0%20yield%20keyword%20%E6%97%A2%20usage%2C%20%E6%84%9F%E8%A6%BA%E4%B8%8A%E5%B9%BE%E5%A5%BD%E7%94%A8%2C%20%E4%BB%A5%E5%AD%B8%E5%A4%9A%E5%80%8B%20keyword%20%E6%97%A2%E5%8E%9F%E5%89%87%E9%BB%8E%E8%AC%9B%2C%20%E6%88%91%E7%95%B6%E7%84%B6%E6%9C%83%E5%9C%A8%20project%20%E4%B8%AD%E8%A9%A6%E7%94%A8%2C%20%E7%95%B6%E4%B8%AD%E6%97%A2%20validation%20%E7%94%A8%E6%B3%95%E6%88%91%E8%A6%BA%E5%BE%97%E5%B9%BE%E5%A5%BD.%0D%0A%0D%0A%5Bcsharp%5D%0D%0Apublic%20partial%20class%20Dinner%20%7B%0D%0A%0D%0A%20%20%20%20public%20bool%20IsHostedBy%28string%20user" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://wongkalun.idv.hk/2009/10/29/yield-keyword/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://wongkalun.idv.hk/2009/10/29/yield-keyword/&amp;bm_description=yield+keyword&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://wongkalun.idv.hk/2009/10/29/yield-keyword/&amp;title=yield+keyword" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://wongkalun.idv.hk/2009/10/29/yield-keyword/&amp;title=yield+keyword" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://wongkalun.idv.hk/2009/10/29/yield-keyword/&amp;title=yield+keyword" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://wongkalun.idv.hk/2009/10/29/yield-keyword/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=yield+keyword+-+http://bit.ly/a1QEAU&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>




		<!-- Added by WP-DragToShare-eXtended Plugin -->
		<script type="text/javascript">
			dtsv.dtse_post_627_permalink = 'http://wongkalun.idv.hk/2009/10/29/yield-keyword/';
			dtsv.dtse_post_627_title = 'yield keyword';
		</script>
		<!-- End of WP-DragToShare-eXtended Plugin -->]]></content:encoded>
			<wfw:commentRss>http://wongkalun.idv.hk/2009/10/29/yield-keyword/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>List.Sort &#8211; Lambda Expression Way</title>
		<link>http://wongkalun.idv.hk/2009/08/18/list-sort-lambda-expression-way/</link>
		<comments>http://wongkalun.idv.hk/2009/08/18/list-sort-lambda-expression-way/#comments</comments>
		<pubDate>Tue, 18 Aug 2009 15:52:40 +0000</pubDate>
		<dc:creator>calendarw</dc:creator>
				<category><![CDATA[coding snippet]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[sort]]></category>

		<guid isPermaLink="false">http://wongkalun.idv.hk/?p=681</guid>
		<description><![CDATA[呢期研究既 delegation, 其中一個例子係 Lambda 在 List.Sort 既使用. List&#60;Person&#62; persons = new List&#60;Person&#62;(); // sort by name persons.Sort((a,b) =&#62; a.Name.CompareTo(b.Name)); // sort by age persons.Sort((a,b) =&#62; a.Age.CompareTo(b.Age)); bool ascending = false; // descending by age persons.Sort((a,b) =&#62; a.Age.CompareTo(b.Age) * (ascending ? 1 : -1)); 有了這樣既 syntax, 寫 sorting 易左同快捷左很多~~ Subscribe to the comments for this post? [...]]]></description>
			<content:encoded><![CDATA[<p>呢期研究既 delegation, 其中一個例子係 Lambda 在 List<T>.Sort 既使用.</p>
<pre class="brush: csharp;">
List&lt;Person&gt; persons = new List&lt;Person&gt;();

// sort by name
persons.Sort((a,b) =&gt; a.Name.CompareTo(b.Name));

// sort by age
persons.Sort((a,b) =&gt; a.Age.CompareTo(b.Age));

bool ascending = false;

// descending by age
persons.Sort((a,b) =&gt; a.Age.CompareTo(b.Age) * (ascending ? 1 : -1));
</pre>
<p>有了這樣既 syntax, 寫 sorting 易左同快捷左很多~~</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-enjoy">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://wongkalun.idv.hk/2009/08/18/list-sort-lambda-expression-way/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://wongkalun.idv.hk/2009/08/18/list-sort-lambda-expression-way/&amp;title=List%3CT%3E.Sort+-+Lambda+Expression+Way" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://wongkalun.idv.hk/2009/08/18/list-sort-lambda-expression-way/&amp;title=List%3CT%3E.Sort+-+Lambda+Expression+Way" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://wongkalun.idv.hk/2009/08/18/list-sort-lambda-expression-way/&amp;title=List%3CT%3E.Sort+-+Lambda+Expression+Way&amp;desc=%E5%91%A2%E6%9C%9F%E7%A0%94%E7%A9%B6%E6%97%A2%20delegation%2C%20%E5%85%B6%E4%B8%AD%E4%B8%80%E5%80%8B%E4%BE%8B%E5%AD%90%E4%BF%82%20Lambda%20%E5%9C%A8%20List.Sort%20%E6%97%A2%E4%BD%BF%E7%94%A8.%0D%0A%0D%0A%5Bcsharp%5D%0D%0AList%26lt%3BPerson%26gt%3B%20persons%20%3D%20new%20List%26lt%3BPerson%26gt%3B%28%29%3B%0D%0A%0D%0A%2F%2F%20sort%20by%20name%0D%0Apersons.Sort%28%28a%2Cb%29%20%3D%26gt%3B%20a.Name.CompareTo%28b.Name%29%29%3B%0D%0A%0D%0A%2F%2F%20sort%20by%20age%0D%0Apersons.Sort%28%28a%2Cb%29%20%3D%26gt%3B%20a.Age.CompareTo%28b.Age%29%29%3B%0D%0A%0D%0Abool%20a" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://wongkalun.idv.hk/2009/08/18/list-sort-lambda-expression-way/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://wongkalun.idv.hk/2009/08/18/list-sort-lambda-expression-way/&amp;bm_description=List%3CT%3E.Sort+-+Lambda+Expression+Way&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://wongkalun.idv.hk/2009/08/18/list-sort-lambda-expression-way/&amp;title=List%3CT%3E.Sort+-+Lambda+Expression+Way" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://wongkalun.idv.hk/2009/08/18/list-sort-lambda-expression-way/&amp;title=List%3CT%3E.Sort+-+Lambda+Expression+Way" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://wongkalun.idv.hk/2009/08/18/list-sort-lambda-expression-way/&amp;title=List%3CT%3E.Sort+-+Lambda+Expression+Way" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://wongkalun.idv.hk/2009/08/18/list-sort-lambda-expression-way/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=List%3CT%3E.Sort+-+Lambda+Expression+Way+-+http://bit.ly/bAZSQI&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>




		<!-- Added by WP-DragToShare-eXtended Plugin -->
		<script type="text/javascript">
			dtsv.dtse_post_681_permalink = 'http://wongkalun.idv.hk/2009/08/18/list-sort-lambda-expression-way/';
			dtsv.dtse_post_681_title = 'List.Sort – Lambda Expression Way';
		</script>
		<!-- End of WP-DragToShare-eXtended Plugin -->]]></content:encoded>
			<wfw:commentRss>http://wongkalun.idv.hk/2009/08/18/list-sort-lambda-expression-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# as operator</title>
		<link>http://wongkalun.idv.hk/2009/08/04/csharp-as-operator/</link>
		<comments>http://wongkalun.idv.hk/2009/08/04/csharp-as-operator/#comments</comments>
		<pubDate>Tue, 04 Aug 2009 15:44:16 +0000</pubDate>
		<dc:creator>calendarw</dc:creator>
				<category><![CDATA[coding snippet]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://wongkalun.idv.hk/?p=665</guid>
		<description><![CDATA[as operator 係用黎做 casting 的 static void Main() { double x = 1234.7; int a; // Cast double to int. a = (int)x; System.Console.WriteLine(a); } // Output: 1234 如果用 boxing 黎轉 type 既話, 錯 type 的話就會 throw InvalidCastException, 但如果用 as operator 就唔會 throw InvalidCastException, 而 value 就會係 null public static void Main() { object time1 [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://msdn.microsoft.com/en-us/library/cscsdfbt%28VS.71%29.aspx">as</a> operator 係用黎做 casting 的</p>
<pre class="brush: csharp;">
static void Main()
{
    double x = 1234.7;
    int a;
    // Cast double to int.
    a = (int)x;
    System.Console.WriteLine(a);
}
// Output: 1234
</pre>
<p>如果用 <a href="http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx">boxing</a> 黎轉 type 既話, 錯 type 的話就會 throw <a href="http://msdn.microsoft.com/en-us/library/system.invalidcastexception.aspx">InvalidCastException</a>, 但如果用 as operator 就唔會 throw InvalidCastException, 而 value 就會係 null</p>
<pre class="brush: csharp;">
public static void Main()
{
    object time1 = DateTime.Now;
    DateTime? t = time1 as DateTime?;    // valid casting
    int? wrongCast = time1 as int?;    // wrongCast == null
}
</pre>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-enjoy">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://wongkalun.idv.hk/2009/08/04/csharp-as-operator/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://wongkalun.idv.hk/2009/08/04/csharp-as-operator/&amp;title=C%23+as+operator" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://wongkalun.idv.hk/2009/08/04/csharp-as-operator/&amp;title=C%23+as+operator" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://wongkalun.idv.hk/2009/08/04/csharp-as-operator/&amp;title=C%23+as+operator&amp;desc=as%20operator%20%E4%BF%82%E7%94%A8%E9%BB%8E%E5%81%9A%20casting%20%E7%9A%84%0D%0A%5Bcsharp%5D%0D%0Astatic%20void%20Main%28%29%0D%0A%7B%0D%0A%20%20%20%20double%20x%20%3D%201234.7%3B%0D%0A%20%20%20%20int%20a%3B%0D%0A%20%20%20%20%2F%2F%20Cast%20double%20to%20int.%0D%0A%20%20%20%20a%20%3D%20%28int%29x%3B%0D%0A%20%20%20%20System.Console.WriteLine%28a%29%3B%0D%0A%7D%0D%0A%2F%2F%20Output%3A%201234%0D%0A%5B%2Fcsharp%5D%0D%0A%0D%0A%E5%A6%82%E6%9E%9C%E7%94%A8%20boxing%20%E9%BB%8E%E8%BD%89%20type%20%E6%97%A2%E8%A9%B1%2C%20%E9%8C%AF%20type%20%E7%9A%84%E8%A9%B1%E5%B0%B1%E6%9C%83%20throw%20InvalidCastExcep" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://wongkalun.idv.hk/2009/08/04/csharp-as-operator/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://wongkalun.idv.hk/2009/08/04/csharp-as-operator/&amp;bm_description=C%23+as+operator&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://wongkalun.idv.hk/2009/08/04/csharp-as-operator/&amp;title=C%23+as+operator" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://wongkalun.idv.hk/2009/08/04/csharp-as-operator/&amp;title=C%23+as+operator" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://wongkalun.idv.hk/2009/08/04/csharp-as-operator/&amp;title=C%23+as+operator" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://wongkalun.idv.hk/2009/08/04/csharp-as-operator/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=C%23+as+operator+-+http://bit.ly/8XJWvu&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>




		<!-- Added by WP-DragToShare-eXtended Plugin -->
		<script type="text/javascript">
			dtsv.dtse_post_665_permalink = 'http://wongkalun.idv.hk/2009/08/04/csharp-as-operator/';
			dtsv.dtse_post_665_title = 'C# as operator';
		</script>
		<!-- End of WP-DragToShare-eXtended Plugin -->]]></content:encoded>
			<wfw:commentRss>http://wongkalun.idv.hk/2009/08/04/csharp-as-operator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# ?? null coalescing operator</title>
		<link>http://wongkalun.idv.hk/2009/07/23/c-null-coalescing-operator/</link>
		<comments>http://wongkalun.idv.hk/2009/07/23/c-null-coalescing-operator/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 15:29:44 +0000</pubDate>
		<dc:creator>calendarw</dc:creator>
				<category><![CDATA[coding snippet]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://wongkalun.idv.hk/?p=654</guid>
		<description><![CDATA[null coalescing operator &#8211; ?? 係用黎決定參數是否 null 既算式, 自 C# 2.0 開始支援, 作為 null 既使用簡化. // .net framework 1.1 既寫法 if (obj.Currency != null) cbxCurrency.SelectedItem = obj.Currency; else cbxCurrency.SelectedItem = DefaultCurrency; // .net framework 2.0 既寫法 cbxCurrency.SelectedItem = obj.Currency ?? DefaultCurrency; 長度上係短左, 識既人會易睇左. 另一方面, 通常既用法會同 Nullable 一齊用, 但自己平時無乜用開 Nullable, 所以對此無乜 comment (知道有呢樣野, 但唔知咩情況用先叫做適合, 所以都未用過. // [...]]]></description>
			<content:encoded><![CDATA[<p>null coalescing operator &#8211; ?? 係用黎決定參數是否 null 既算式, 自 C# 2.0 開始支援, 作為 null 既使用簡化.</p>
<pre class="brush: csharp;">
// .net framework 1.1 既寫法
if (obj.Currency != null)
    cbxCurrency.SelectedItem = obj.Currency;
else
    cbxCurrency.SelectedItem = DefaultCurrency;
</pre>
<pre class="brush: csharp;">
// .net framework 2.0 既寫法
cbxCurrency.SelectedItem = obj.Currency ?? DefaultCurrency;
</pre>
<p>長度上係短左, 識既人會易睇左.  另一方面, 通常既用法會同 Nullable 一齊用, 但自己平時無乜用開 Nullable, 所以對此無乜 comment (知道有呢樣野, 但唔知咩情況用先叫做適合, 所以都未用過.</p>
<pre class="brush: csharp;">
// nullable int
int? i = null;
int result = i ?? 5;
// Output: result == 5
</pre>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-enjoy">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://wongkalun.idv.hk/2009/07/23/c-null-coalescing-operator/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://wongkalun.idv.hk/2009/07/23/c-null-coalescing-operator/&amp;title=C%23+%3F%3F+null+coalescing+operator" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://wongkalun.idv.hk/2009/07/23/c-null-coalescing-operator/&amp;title=C%23+%3F%3F+null+coalescing+operator" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://wongkalun.idv.hk/2009/07/23/c-null-coalescing-operator/&amp;title=C%23+%3F%3F+null+coalescing+operator&amp;desc=null%20coalescing%20operator%20-%20%3F%3F%20%E4%BF%82%E7%94%A8%E9%BB%8E%E6%B1%BA%E5%AE%9A%E5%8F%83%E6%95%B8%E6%98%AF%E5%90%A6%20null%20%E6%97%A2%E7%AE%97%E5%BC%8F%2C%20%E8%87%AA%20C%23%202.0%20%E9%96%8B%E5%A7%8B%E6%94%AF%E6%8F%B4%2C%20%E4%BD%9C%E7%82%BA%20null%20%E6%97%A2%E4%BD%BF%E7%94%A8%E7%B0%A1%E5%8C%96.%0D%0A%0D%0A%5Bcsharp%5D%0D%0A%2F%2F%20.net%20framework%201.1%20%E6%97%A2%E5%AF%AB%E6%B3%95%0D%0Aif%20%28obj.Currency%20%21%3D%20null%29%0D%0A%20%20%20%20cbxCurrency.SelectedItem%20%3D%20obj.Currency%3B%0D%0Aelse%0D%0A%20%20%20%20cbxCurrency.SelectedItem%20%3D%20DefaultCurrency%3B" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://wongkalun.idv.hk/2009/07/23/c-null-coalescing-operator/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://wongkalun.idv.hk/2009/07/23/c-null-coalescing-operator/&amp;bm_description=C%23+%3F%3F+null+coalescing+operator&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://wongkalun.idv.hk/2009/07/23/c-null-coalescing-operator/&amp;title=C%23+%3F%3F+null+coalescing+operator" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://wongkalun.idv.hk/2009/07/23/c-null-coalescing-operator/&amp;title=C%23+%3F%3F+null+coalescing+operator" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://wongkalun.idv.hk/2009/07/23/c-null-coalescing-operator/&amp;title=C%23+%3F%3F+null+coalescing+operator" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://wongkalun.idv.hk/2009/07/23/c-null-coalescing-operator/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=C%23+%3F%3F+null+coalescing+operator+-+http://bit.ly/czdKkC&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>




		<!-- Added by WP-DragToShare-eXtended Plugin -->
		<script type="text/javascript">
			dtsv.dtse_post_654_permalink = 'http://wongkalun.idv.hk/2009/07/23/c-null-coalescing-operator/';
			dtsv.dtse_post_654_title = 'C# ?? null coalescing operator';
		</script>
		<!-- End of WP-DragToShare-eXtended Plugin -->]]></content:encoded>
			<wfw:commentRss>http://wongkalun.idv.hk/2009/07/23/c-null-coalescing-operator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Timer for Performance Testing</title>
		<link>http://wongkalun.idv.hk/2009/05/13/timer-for-performance-testing/</link>
		<comments>http://wongkalun.idv.hk/2009/05/13/timer-for-performance-testing/#comments</comments>
		<pubDate>Tue, 12 May 2009 16:07:18 +0000</pubDate>
		<dc:creator>calendarw</dc:creator>
				<category><![CDATA[coding snippet]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[nunit]]></category>
		<category><![CDATA[test-driven]]></category>

		<guid isPermaLink="false">http://wongkalun.idv.hk/?p=532</guid>
		<description><![CDATA[呢排重溫緊 Pragmatic Unit Testing in C# with Nunit, 開始試緊寫有關 Performance 既 Test Case, 書中有一段有關 Performance 既 Code: [Test] public void FilterRanges() { Timer timer = new Timer(); String naughty_url = &#34;http://www.xxxxxxxxxxx.com&#34;; // First, check a bad URL against a small list URLFilter filter = new URLFilter(small_list); timer.Start(); filter.Check(naughty_url); timer.End(); Assert.IsTrue(timer.ElapsedTime &#60; 1.0); // Next, check [...]]]></description>
			<content:encoded><![CDATA[<p>呢排重溫緊 <a href="http://www.amazon.com/Pragmatic-Unit-Testing-Nunit-Programmers/dp/0974514020">Pragmatic Unit Testing in C# with Nunit</a>, 開始試緊寫有關 Performance 既 Test Case, 書中有一段有關 Performance 既 Code:</p>
<pre class="brush: csharp;">
[Test]
public void FilterRanges()
{
    Timer timer = new Timer();
    String naughty_url = &quot;http://www.xxxxxxxxxxx.com&quot;;
    // First, check a bad URL against a small list
    URLFilter filter = new URLFilter(small_list);
    timer.Start();
    filter.Check(naughty_url);
    timer.End();
    Assert.IsTrue(timer.ElapsedTime &lt; 1.0);
    // Next, check a bad URL against a big list
    filter = new URLFilter(big_list);
    timer.Start();
    filter.Check(naughty_url);
    timer.End();
    Assert.IsTrue(timer.ElapsedTime &lt; 2.0);
    // Finally, check a bad URL against a huge list
    filter = new URLFilter(huge_list);
    timer.Start();
    filter.Check(naughty_url);
    timer.End();
    Assert.IsTrue(timer.ElapsedTime &lt; 3.0);
}
</pre>
<p>段 code 係一個幾好既 example 去講點 Test Performance, 但當真係要試果陣, 就發現左樣野, 就係我搵唔到 Code 中既 Timer Class, 係我在 System 入面既幾個 namespace 中, 都搵唔到啱用既 Timer, 因為 System namespace 入面既 Timer 大部份都係用黎 Trigger Timeout Event, 而當中既 Stop method 都只係用黎停止 Event Trigger, 而沒有任何計時結果做到出黎, 所以經過一輪網上既搜尋之後, 得出以下 Timer, 主要目的係用黎計時, 仔細度高, 最啱用黎作 Performance Testing 之用!!~</p>
<pre class="brush: csharp;">
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace TestUtil
{
    public class HighResolutionTimer
    {
        private long frequency;
        private long start;
        private long stop;

        public HighResolutionTimer()
        {
            QueryPerformanceFrequency(ref frequency);
        }

        public float ElapsedTime
        {
            get
            {
                float elapsed = (((float)(stop - start)) / ((float)frequency));
                return elapsed;
            }
        }

        public void Start()
        {
            QueryPerformanceCounter(ref start);
        }

        public void Stop()
        {
            QueryPerformanceCounter(ref stop);
        }

        [System.Runtime.InteropServices.DllImport(&quot;KERNEL32.dll&quot;, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        private static extern bool QueryPerformanceCounter([In, Out] ref long performanceCount);

        [System.Runtime.InteropServices.DllImport(&quot;KERNEL32.dll&quot;, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        private static extern bool QueryPerformanceFrequency([In, Out] ref long frequency);

    }
}
</pre>
<p>HighResolutionTimer 來源 : <a href="http://www.wangchao.net.cn/bbsdetail_36077.html">在 C# 中實現高性能計時</a></p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-enjoy">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://wongkalun.idv.hk/2009/05/13/timer-for-performance-testing/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://wongkalun.idv.hk/2009/05/13/timer-for-performance-testing/&amp;title=Timer+for+Performance+Testing" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://wongkalun.idv.hk/2009/05/13/timer-for-performance-testing/&amp;title=Timer+for+Performance+Testing" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://wongkalun.idv.hk/2009/05/13/timer-for-performance-testing/&amp;title=Timer+for+Performance+Testing&amp;desc=%E5%91%A2%E6%8E%92%E9%87%8D%E6%BA%AB%E7%B7%8A%20Pragmatic%20Unit%20Testing%20in%20C%23%20with%20Nunit%2C%20%E9%96%8B%E5%A7%8B%E8%A9%A6%E7%B7%8A%E5%AF%AB%E6%9C%89%E9%97%9C%20Performance%20%E6%97%A2%20Test%20Case%2C%20%E6%9B%B8%E4%B8%AD%E6%9C%89%E4%B8%80%E6%AE%B5%E6%9C%89%E9%97%9C%20Performance%20%E6%97%A2%20Code%3A%0D%0A%5Bcsharp%5D%0D%0A%5BTest%5D%0D%0Apublic%20void%20FilterRanges%28%29%0D%0A%7B%0D%0A%20%20%20%20Timer%20timer%20%3D%20new%20Timer%28%29%3B%0D%0A%20%20%20%20String%20naughty_url%20%3D%20%26quot%3Bhttp%3A%2F%2Fwww.xxxxxxxxxxx.com%26quot%3B%3B%0D%0A%20%20" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://wongkalun.idv.hk/2009/05/13/timer-for-performance-testing/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://wongkalun.idv.hk/2009/05/13/timer-for-performance-testing/&amp;bm_description=Timer+for+Performance+Testing&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://wongkalun.idv.hk/2009/05/13/timer-for-performance-testing/&amp;title=Timer+for+Performance+Testing" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://wongkalun.idv.hk/2009/05/13/timer-for-performance-testing/&amp;title=Timer+for+Performance+Testing" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://wongkalun.idv.hk/2009/05/13/timer-for-performance-testing/&amp;title=Timer+for+Performance+Testing" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://wongkalun.idv.hk/2009/05/13/timer-for-performance-testing/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Timer+for+Performance+Testing+-+http://bit.ly/cQswFB&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>




		<!-- Added by WP-DragToShare-eXtended Plugin -->
		<script type="text/javascript">
			dtsv.dtse_post_532_permalink = 'http://wongkalun.idv.hk/2009/05/13/timer-for-performance-testing/';
			dtsv.dtse_post_532_title = 'Timer for Performance Testing';
		</script>
		<!-- End of WP-DragToShare-eXtended Plugin -->]]></content:encoded>
			<wfw:commentRss>http://wongkalun.idv.hk/2009/05/13/timer-for-performance-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Update Statement with Other Table</title>
		<link>http://wongkalun.idv.hk/2009/04/23/update-statement-with-other-table/</link>
		<comments>http://wongkalun.idv.hk/2009/04/23/update-statement-with-other-table/#comments</comments>
		<pubDate>Thu, 23 Apr 2009 15:13:54 +0000</pubDate>
		<dc:creator>calendarw</dc:creator>
				<category><![CDATA[coding snippet]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://wongkalun.idv.hk/?p=472</guid>
		<description><![CDATA[呢排為設置 Testing 場而預備緊 d SQL statement, 有部份 Data 得 detail table, 但使用時要整返個 master 比佢, 所以就要使用 update statement 黎 link 返唔同 table 既 key, 以下係一個例子: update tblModel set series_pkey = tblSeries.pkey from tblModel, tblSeries where tblSeries.full_name = 'A Series' and tblModel.full_name like 'A %'; Subscribe to the comments for this post? Share this on del.icio.us Digg [...]]]></description>
			<content:encoded><![CDATA[<p>呢排為設置 Testing 場而預備緊 d SQL statement, 有部份 Data 得 detail table, 但使用時要整返個 master 比佢, 所以就要使用 update statement 黎 link 返唔同 table 既 key, 以下係一個例子:</p>
<pre class="brush: sql;">
update tblModel
set series_pkey = tblSeries.pkey
from tblModel, tblSeries
where tblSeries.full_name = 'A Series' and tblModel.full_name like 'A %';
</pre>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-enjoy">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://wongkalun.idv.hk/2009/04/23/update-statement-with-other-table/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://wongkalun.idv.hk/2009/04/23/update-statement-with-other-table/&amp;title=Update+Statement+with+Other+Table" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://wongkalun.idv.hk/2009/04/23/update-statement-with-other-table/&amp;title=Update+Statement+with+Other+Table" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://wongkalun.idv.hk/2009/04/23/update-statement-with-other-table/&amp;title=Update+Statement+with+Other+Table&amp;desc=%E5%91%A2%E6%8E%92%E7%82%BA%E8%A8%AD%E7%BD%AE%20Testing%20%E5%A0%B4%E8%80%8C%E9%A0%90%E5%82%99%E7%B7%8A%20d%20SQL%20statement%2C%20%E6%9C%89%E9%83%A8%E4%BB%BD%20Data%20%E5%BE%97%20detail%20table%2C%20%E4%BD%86%E4%BD%BF%E7%94%A8%E6%99%82%E8%A6%81%E6%95%B4%E8%BF%94%E5%80%8B%20master%20%E6%AF%94%E4%BD%A2%2C%20%E6%89%80%E4%BB%A5%E5%B0%B1%E8%A6%81%E4%BD%BF%E7%94%A8%20update%20statement%20%E9%BB%8E%20link%20%E8%BF%94%E5%94%94%E5%90%8C%20table%20%E6%97%A2%20key%2C%20%E4%BB%A5%E4%B8%8B%E4%BF%82%E4%B8%80%E5%80%8B%E4%BE%8B%E5%AD%90%3A%0D%0A%0D%0A%5Bsql%5D%0D%0Aupdate%20tblModel%20%0D%0Aset%20series_pkey%20%3D%20tblSeries.pkey%0D%0Afrom%20tblModel%2C%20" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://wongkalun.idv.hk/2009/04/23/update-statement-with-other-table/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://wongkalun.idv.hk/2009/04/23/update-statement-with-other-table/&amp;bm_description=Update+Statement+with+Other+Table&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://wongkalun.idv.hk/2009/04/23/update-statement-with-other-table/&amp;title=Update+Statement+with+Other+Table" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://wongkalun.idv.hk/2009/04/23/update-statement-with-other-table/&amp;title=Update+Statement+with+Other+Table" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://wongkalun.idv.hk/2009/04/23/update-statement-with-other-table/&amp;title=Update+Statement+with+Other+Table" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://wongkalun.idv.hk/2009/04/23/update-statement-with-other-table/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Update+Statement+with+Other+Table+-+http://bit.ly/bzAxMp&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>




		<!-- Added by WP-DragToShare-eXtended Plugin -->
		<script type="text/javascript">
			dtsv.dtse_post_472_permalink = 'http://wongkalun.idv.hk/2009/04/23/update-statement-with-other-table/';
			dtsv.dtse_post_472_title = 'Update Statement with Other Table';
		</script>
		<!-- End of WP-DragToShare-eXtended Plugin -->]]></content:encoded>
			<wfw:commentRss>http://wongkalun.idv.hk/2009/04/23/update-statement-with-other-table/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Understanding Exception</title>
		<link>http://wongkalun.idv.hk/2009/03/29/understanding-exception/</link>
		<comments>http://wongkalun.idv.hk/2009/03/29/understanding-exception/#comments</comments>
		<pubDate>Sun, 29 Mar 2009 04:15:46 +0000</pubDate>
		<dc:creator>calendarw</dc:creator>
				<category><![CDATA[coding snippet]]></category>
		<category><![CDATA[diary]]></category>
		<category><![CDATA[Add new tag]]></category>

		<guid isPermaLink="false">http://wongkalun.idv.hk/?p=401</guid>
		<description><![CDATA[Exception &#8211; 從來都被初哥覺得係個麻煩既來源, 但其實佢只係一個提醒工具, 話比大家知那裡有問題. 好多課程, 以及書本都有教點處理 Exception, 但如何正確使用 Exception 就好似得 Pragmatic Programmer 一書內有提及. 通常我使用 Exception 既地方會有以下幾個, 作用係提醒返 d 唔正常既動作, 因為唔 throw 既話, 使用者 (包括自己) 都會唔知或者唔記得: 1. Design By Contract 主要用 Exception 黎 validate 所有 input 或 pre-condition, 令佢有個 Valid State 比佢 perform action public void Cancel() { if (item.IsLoaning) throw new InvalidOperationException(&#34;Please return the [...]]]></description>
			<content:encoded><![CDATA[<p>Exception &#8211; 從來都被初哥覺得係個麻煩既來源, 但其實佢只係一個提醒工具, 話比大家知那裡有問題.</p>
<p>好多課程, 以及書本都有教點處理 Exception, 但如何正確使用 Exception 就好似得 <a href="http://www.amazon.com/Pragmatic-Programmer-Journeyman-Master/dp/020161622X">Pragmatic Programmer</a> 一書內有提及.</p>
<p>通常我使用 Exception 既地方會有以下幾個, 作用係提醒返 d 唔正常既動作, 因為唔 throw 既話, 使用者 (包括自己) 都會唔知或者唔記得:</p>
<p>1. Design By Contract<br />
主要用 Exception 黎 validate 所有 input 或 pre-condition, 令佢有個 Valid State 比佢 perform action</p>
<pre class="brush: csharp;">
public void Cancel()
{
    if (item.IsLoaning)
        throw new InvalidOperationException(&quot;Please return the loan before cancel&quot;);
}
</pre>
<p>2. Supporting of Share Module</p>
<pre class="brush: csharp;">
public static IDbDataAdapter CreateDbDataAdapter(IDbCommand command)
{
    IDbDataAdapter adapter = null;
    if (command is SqlCommand)
    {
        adapter = new SqlDataAdapter((SqlCommand)command);
    }
    if (command is OleDbCommand)
    {
        adapter = new OleDbDataAdapter((OleDbCommand)command);
    }
    if (command is OdbcCommand)
    {
        adapter = new OdbcDataAdapter((OdbcCommand)command);
    }
    if (adapter != null)
    {
        return adapter;
    }
    throw new NotSupportedException();
}
</pre>
<p>3. Supporting on different framework</p>
<pre class="brush: csharp;">
public void NewFeature()
{
#if !USING_NEW_FRAMEWORK
    throw new NotSupportedException();
#else
    doSomething();
#endif
}
</pre>
<p>4. 又或者係未做完的工作, 不過好多時都會寫 // TODO</p>
<pre class="brush: csharp;">
public void DoSomething()
{
    throw new NotImplementedException();
}
</pre>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-enjoy">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://wongkalun.idv.hk/2009/03/29/understanding-exception/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://wongkalun.idv.hk/2009/03/29/understanding-exception/&amp;title=Understanding+Exception" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://wongkalun.idv.hk/2009/03/29/understanding-exception/&amp;title=Understanding+Exception" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://wongkalun.idv.hk/2009/03/29/understanding-exception/&amp;title=Understanding+Exception&amp;desc=Exception%20-%20%E5%BE%9E%E4%BE%86%E9%83%BD%E8%A2%AB%E5%88%9D%E5%93%A5%E8%A6%BA%E5%BE%97%E4%BF%82%E5%80%8B%E9%BA%BB%E7%85%A9%E6%97%A2%E4%BE%86%E6%BA%90%2C%20%E4%BD%86%E5%85%B6%E5%AF%A6%E4%BD%A2%E5%8F%AA%E4%BF%82%E4%B8%80%E5%80%8B%E6%8F%90%E9%86%92%E5%B7%A5%E5%85%B7%2C%20%E8%A9%B1%E6%AF%94%E5%A4%A7%E5%AE%B6%E7%9F%A5%E9%82%A3%E8%A3%A1%E6%9C%89%E5%95%8F%E9%A1%8C.%0D%0A%0D%0A%E5%A5%BD%E5%A4%9A%E8%AA%B2%E7%A8%8B%2C%20%E4%BB%A5%E5%8F%8A%E6%9B%B8%E6%9C%AC%E9%83%BD%E6%9C%89%E6%95%99%E9%BB%9E%E8%99%95%E7%90%86%20Exception%2C%20%E4%BD%86%E5%A6%82%E4%BD%95%E6%AD%A3%E7%A2%BA%E4%BD%BF%E7%94%A8%20Exception%20%E5%B0%B1%E5%A5%BD%E4%BC%BC%E5%BE%97%20Pragmatic%20Programmer%20%E4%B8%80%E6%9B%B8%E5%85%A7%E6%9C%89%E6%8F%90%E5%8F%8A.%0D%0A%0D%0A%E9%80%9A%E5%B8%B8%E6%88%91%E4%BD%BF%E7%94%A8%20Excepti" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://wongkalun.idv.hk/2009/03/29/understanding-exception/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://wongkalun.idv.hk/2009/03/29/understanding-exception/&amp;bm_description=Understanding+Exception&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://wongkalun.idv.hk/2009/03/29/understanding-exception/&amp;title=Understanding+Exception" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://wongkalun.idv.hk/2009/03/29/understanding-exception/&amp;title=Understanding+Exception" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://wongkalun.idv.hk/2009/03/29/understanding-exception/&amp;title=Understanding+Exception" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://wongkalun.idv.hk/2009/03/29/understanding-exception/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Understanding+Exception+-+http://bit.ly/agdMvt&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>




		<!-- Added by WP-DragToShare-eXtended Plugin -->
		<script type="text/javascript">
			dtsv.dtse_post_401_permalink = 'http://wongkalun.idv.hk/2009/03/29/understanding-exception/';
			dtsv.dtse_post_401_title = 'Understanding Exception';
		</script>
		<!-- End of WP-DragToShare-eXtended Plugin -->]]></content:encoded>
			<wfw:commentRss>http://wongkalun.idv.hk/2009/03/29/understanding-exception/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# 3.0 初試!!</title>
		<link>http://wongkalun.idv.hk/2009/03/13/c-30-%e5%88%9d%e8%a9%a6/</link>
		<comments>http://wongkalun.idv.hk/2009/03/13/c-30-%e5%88%9d%e8%a9%a6/#comments</comments>
		<pubDate>Fri, 13 Mar 2009 10:32:52 +0000</pubDate>
		<dc:creator>calendarw</dc:creator>
				<category><![CDATA[coding snippet]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://wongkalun.idv.hk/?p=396</guid>
		<description><![CDATA[用左 VS 2008 其實都幾個月, 但因公司目標係 .net 2.0 既關係, 技術始終都仲停留緊係 2.0 度, 近日開始習慣緊 C# 3.0 既 syntax, 有幾個新 feature 係 2.0 platform 都幾好用. Object initializers 呢個係一個新既 contractor syntax, 可以好方便咁創建同設定 Value. 但如果個 Properties 係 private set; 既話就用唔到啦!! Customer c1 = new Customer { Name=&#34;James&#34; }; Customer c2 = new Customer { Name=&#34;Tom&#34; }; Lambda expressions Lambda 好好用!! [...]]]></description>
			<content:encoded><![CDATA[<p>用左 VS 2008 其實都幾個月, 但因公司目標係 .net 2.0 既關係, 技術始終都仲停留緊係 2.0 度, 近日開始習慣緊 C# 3.0 既 syntax, 有幾個新 feature 係 2.0 platform 都幾好用.</p>
<p><strong>Object initializers</strong><br />
呢個係一個新既 contractor syntax, 可以好方便咁創建同設定 Value. 但如果個 Properties 係 private set; 既話就用唔到啦!!</p>
<pre class="brush: csharp;">
Customer c1 = new Customer { Name=&quot;James&quot; };
Customer c2 = new Customer { Name=&quot;Tom&quot; };
</pre>
<p><strong>Lambda expressions</strong><br />
Lambda 好好用!! 簡單, 快捷!!</p>
<pre class="brush: csharp;">
string name = &quot;James&quot;;
IList&lt;Customer&gt; result = list.FindAll(x =&gt; name.Equals(x.Name));
</pre>
<p><strong>Automatic properties</strong><br />
呢個可以簡化到部份 properties, 但如果有 business logic 既話就唔係太啱用</p>
<pre class="brush: csharp;">
public decimal UnitPrice
{
    get;
    private set;
}
</pre>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-enjoy">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://wongkalun.idv.hk/2009/03/13/c-30-初試/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://wongkalun.idv.hk/2009/03/13/c-30-%e5%88%9d%e8%a9%a6/&amp;title=C%23+3.0+%E5%88%9D%E8%A9%A6%21%21" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://wongkalun.idv.hk/2009/03/13/c-30-%e5%88%9d%e8%a9%a6/&amp;title=C%23+3.0+%E5%88%9D%E8%A9%A6%21%21" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://wongkalun.idv.hk/2009/03/13/c-30-%e5%88%9d%e8%a9%a6/&amp;title=C%23+3.0+%E5%88%9D%E8%A9%A6%21%21&amp;desc=%E7%94%A8%E5%B7%A6%20VS%202008%20%E5%85%B6%E5%AF%A6%E9%83%BD%E5%B9%BE%E5%80%8B%E6%9C%88%2C%20%E4%BD%86%E5%9B%A0%E5%85%AC%E5%8F%B8%E7%9B%AE%E6%A8%99%E4%BF%82%20.net%202.0%20%E6%97%A2%E9%97%9C%E4%BF%82%2C%20%E6%8A%80%E8%A1%93%E5%A7%8B%E7%B5%82%E9%83%BD%E4%BB%B2%E5%81%9C%E7%95%99%E7%B7%8A%E4%BF%82%202.0%20%E5%BA%A6%2C%20%E8%BF%91%E6%97%A5%E9%96%8B%E5%A7%8B%E7%BF%92%E6%85%A3%E7%B7%8A%20C%23%203.0%20%E6%97%A2%20syntax%2C%20%E6%9C%89%E5%B9%BE%E5%80%8B%E6%96%B0%20feature%20%E4%BF%82%202.0%20platform%20%E9%83%BD%E5%B9%BE%E5%A5%BD%E7%94%A8.%0D%0A%0D%0AObject%20initializers%0D%0A%E5%91%A2%E5%80%8B%E4%BF%82%E4%B8%80%E5%80%8B%E6%96%B0%E6%97%A2%20contractor%20syntax%2C%20%E5%8F%AF%E4%BB%A5%E5%A5%BD%E6%96%B9%E4%BE%BF%E5%92%81%E5%89%B5%E5%BB%BA%E5" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://wongkalun.idv.hk/2009/03/13/c-30-%e5%88%9d%e8%a9%a6/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://wongkalun.idv.hk/2009/03/13/c-30-%e5%88%9d%e8%a9%a6/&amp;bm_description=C%23+3.0+%E5%88%9D%E8%A9%A6%21%21&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://wongkalun.idv.hk/2009/03/13/c-30-%e5%88%9d%e8%a9%a6/&amp;title=C%23+3.0+%E5%88%9D%E8%A9%A6%21%21" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://wongkalun.idv.hk/2009/03/13/c-30-%e5%88%9d%e8%a9%a6/&amp;title=C%23+3.0+%E5%88%9D%E8%A9%A6%21%21" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://wongkalun.idv.hk/2009/03/13/c-30-%e5%88%9d%e8%a9%a6/&amp;title=C%23+3.0+%E5%88%9D%E8%A9%A6%21%21" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://wongkalun.idv.hk/2009/03/13/c-30-%e5%88%9d%e8%a9%a6/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=C%23+3.0+%E5%88%9D%E8%A9%A6%21%21+-+http://b2l.me/xx7cv&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>




		<!-- Added by WP-DragToShare-eXtended Plugin -->
		<script type="text/javascript">
			dtsv.dtse_post_396_permalink = 'http://wongkalun.idv.hk/2009/03/13/c-30-%e5%88%9d%e8%a9%a6/';
			dtsv.dtse_post_396_title = 'C# 3.0 初試!!';
		</script>
		<!-- End of WP-DragToShare-eXtended Plugin -->]]></content:encoded>
			<wfw:commentRss>http://wongkalun.idv.hk/2009/03/13/c-30-%e5%88%9d%e8%a9%a6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Working with Dock</title>
		<link>http://wongkalun.idv.hk/2009/02/04/working-with-dock/</link>
		<comments>http://wongkalun.idv.hk/2009/02/04/working-with-dock/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 10:16:09 +0000</pubDate>
		<dc:creator>calendarw</dc:creator>
				<category><![CDATA[coding snippet]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://wongkalun.idv.hk/?p=382</guid>
		<description><![CDATA[近半年開始著重地寫 Windows Application (以前以 web 同 DLL 為主), 寫左咁耐都無用過 Control.Dock 呢個屬性, 今日第一次用, 就出左好多次序上既問題, 不過在網上找到以下既解決方案, 當解決了次序既問題後, Dock 其實係幾好用!!~~ private void RemoveBtn(Button btn1) { if(this.Controls.Contains(btn1)) this.Controls.Remove(btn1); } private void SetDockProperity(Button btn1, DockStyle dockStyle) { btn1.Dock = dockStyle; } private void DockTop_Click(object sender, System.EventArgs e) { RemoveBtn(this.button1); RemoveBtn(this.button2); RemoveBtn(this.button3); SetDockProperity(this.button1, System.Windows.Forms.DockStyle.Top); SetDockProperity(this.button2, System.Windows.Forms.DockStyle.Top); SetDockProperity(this.button3, System.Windows.Forms.DockStyle.Top); this.Controls.Add(this.button3); // 注意順序, [...]]]></description>
			<content:encoded><![CDATA[<p>近半年開始著重地寫 Windows Application (以前以 web 同 DLL 為主), 寫左咁耐都無用過 Control.Dock 呢個屬性, 今日第一次用, 就出左好多次序上既問題, 不過在網上找到以下既解決方案, 當解決了次序既問題後, Dock 其實係幾好用!!~~</p>
<pre class="brush: csharp;">
private void RemoveBtn(Button btn1)
{
	if(this.Controls.Contains(btn1))
		this.Controls.Remove(btn1);
}

private void SetDockProperity(Button btn1, DockStyle dockStyle)
{
	btn1.Dock = dockStyle;
}

private void DockTop_Click(object sender, System.EventArgs e)
{
	RemoveBtn(this.button1);
	RemoveBtn(this.button2);
	RemoveBtn(this.button3);
	SetDockProperity(this.button1, System.Windows.Forms.DockStyle.Top);
	SetDockProperity(this.button2, System.Windows.Forms.DockStyle.Top);
	SetDockProperity(this.button3, System.Windows.Forms.DockStyle.Top);
	this.Controls.Add(this.button3); // 注意順序, 後 Add 的最上面
	this.Controls.Add(this.button2);
	this.Controls.Add(this.button1);
}

private void DockBottom_Click(object sender, System.EventArgs e)
{
	RemoveBtn(this.button1);
	RemoveBtn(this.button2);
	RemoveBtn(this.button3);
	SetDockProperity(this.button1, System.Windows.Forms.DockStyle.Bottom);
	SetDockProperity(this.button2, System.Windows.Forms.DockStyle.Bottom);
	SetDockProperity(this.button3, System.Windows.Forms.DockStyle.Bottom);
	this.Controls.Add(this.button3);  // 注意順序, 後 Add 的最下面
	this.Controls.Add(this.button2);
	this.Controls.Add(this.button1);
}
</pre>
<p>轉載自: <a href="http://www.blueshop.com.tw/board/show.asp?subcde=BRD200505241658412IW&#038;fumcde=FUM20050124192253INM">藍色小鋪</a></p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-enjoy">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://wongkalun.idv.hk/2009/02/04/working-with-dock/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://wongkalun.idv.hk/2009/02/04/working-with-dock/&amp;title=Working+with+Dock" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://wongkalun.idv.hk/2009/02/04/working-with-dock/&amp;title=Working+with+Dock" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://wongkalun.idv.hk/2009/02/04/working-with-dock/&amp;title=Working+with+Dock&amp;desc=%E8%BF%91%E5%8D%8A%E5%B9%B4%E9%96%8B%E5%A7%8B%E8%91%97%E9%87%8D%E5%9C%B0%E5%AF%AB%20Windows%20Application%20%28%E4%BB%A5%E5%89%8D%E4%BB%A5%20web%20%E5%90%8C%20DLL%20%E7%82%BA%E4%B8%BB%29%2C%20%E5%AF%AB%E5%B7%A6%E5%92%81%E8%80%90%E9%83%BD%E7%84%A1%E7%94%A8%E9%81%8E%20Control.Dock%20%E5%91%A2%E5%80%8B%E5%B1%AC%E6%80%A7%2C%20%E4%BB%8A%E6%97%A5%E7%AC%AC%E4%B8%80%E6%AC%A1%E7%94%A8%2C%20%E5%B0%B1%E5%87%BA%E5%B7%A6%E5%A5%BD%E5%A4%9A%E6%AC%A1%E5%BA%8F%E4%B8%8A%E6%97%A2%E5%95%8F%E9%A1%8C%2C%20%E4%B8%8D%E9%81%8E%E5%9C%A8%E7%B6%B2%E4%B8%8A%E6%89%BE%E5%88%B0%E4%BB%A5%E4%B8%8B%E6%97%A2%E8%A7%A3%E6%B1%BA%E6%96%B9%E6%A1%88%2C%20%E7%95%B6%E8%A7%A3%E6%B1%BA%E4%BA%86%E6%AC%A1%E5%BA%8F%E6%97%A2%E5%95%8F%E9%A1%8C%E5%BE%8C%2C%20Dock%20%E5%85%B6%E5%AF%A6%E4%BF%82%E5%B9%BE%E5%A5%BD%E7%94%A8%21%21%7E%7E%0D%0A%0D%0A%5Bcshar" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://wongkalun.idv.hk/2009/02/04/working-with-dock/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://wongkalun.idv.hk/2009/02/04/working-with-dock/&amp;bm_description=Working+with+Dock&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://wongkalun.idv.hk/2009/02/04/working-with-dock/&amp;title=Working+with+Dock" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://wongkalun.idv.hk/2009/02/04/working-with-dock/&amp;title=Working+with+Dock" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://wongkalun.idv.hk/2009/02/04/working-with-dock/&amp;title=Working+with+Dock" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://wongkalun.idv.hk/2009/02/04/working-with-dock/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Working+with+Dock+-+http://bit.ly/dxhNuI&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>




		<!-- Added by WP-DragToShare-eXtended Plugin -->
		<script type="text/javascript">
			dtsv.dtse_post_382_permalink = 'http://wongkalun.idv.hk/2009/02/04/working-with-dock/';
			dtsv.dtse_post_382_title = 'Working with Dock';
		</script>
		<!-- End of WP-DragToShare-eXtended Plugin -->]]></content:encoded>
			<wfw:commentRss>http://wongkalun.idv.hk/2009/02/04/working-with-dock/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Implement State of View</title>
		<link>http://wongkalun.idv.hk/2008/08/03/implement-state-of-view/</link>
		<comments>http://wongkalun.idv.hk/2008/08/03/implement-state-of-view/#comments</comments>
		<pubDate>Sun, 03 Aug 2008 10:39:56 +0000</pubDate>
		<dc:creator>calendarw</dc:creator>
				<category><![CDATA[coding snippet]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://wongkalun.idv.hk/?p=124</guid>
		<description><![CDATA[平時寫介面(GUI), 通常會寫一個 method 叫 StateChange 黎處理所有介面上既所有 visible 同 enable 既動作. 而自己會因介面既設計而定義一個 private enum 既 State, 所有 state 由一個 method 做晒. private enum State { Add, Update } private void StateChange(State eState) { if (eState == State .Add) { txtId.Visible = true; lblId.Visible = false; lblMode.Text = &#34;[Add New]&#34;; } if (eState == State .Update) { [...]]]></description>
			<content:encoded><![CDATA[<p>平時寫介面(GUI), 通常會寫一個 method 叫 StateChange 黎處理所有介面上既所有 visible 同 enable 既動作. 而自己會因介面既設計而定義一個 private enum 既 State, 所有 state 由一個 method 做晒.</p>
<pre class="brush: csharp;">
private enum State
{
    Add,
    Update
}

private void StateChange(State eState)
{
    if (eState == State .Add)
    {
        txtId.Visible = true;
        lblId.Visible = false;
        lblMode.Text = &quot;[Add New]&quot;;
    }

    if (eState == State .Update)
    {
        txtId.Visible = false;
        lblId.Visible = true;
        lblMode.Text = &quot;[Update]&quot;;
    }
}
</pre>
<p>而我自己寫過既 State 都有幾隻:<br />
同一版有個所有 Item 既 List 而 Edit box 得 Add 同 Update</p>
<pre class="brush: csharp;">private enum State
{
    Add,
    Update
}
</pre>
<p>一版得主要係用黎 Insert 既:</p>
<pre class="brush: csharp;">private enum State
{
    WaitingSave,
    WaitingConfirm    // for double confirm
}
</pre>
<p>同埋得睇同改既:</p>
<pre class="brush: csharp;">
private enum State
{
    Normal,
    Edit    // for add and update
}
</pre>
<p>仲有好多其他既 State 寫過, 不過都太過多同太專門, 所以應該係按照實際用途而加.</p>
<p>網上曾經見過有人用 State object 同 State Pattern 黎做, 對我而言好似用係 GUI 方面未必真係有咁既需要, 但對一個 domain object 而言就最好有 State object, 咁樣會對個 design 好好多.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-enjoy">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://wongkalun.idv.hk/2008/08/03/implement-state-of-view/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://wongkalun.idv.hk/2008/08/03/implement-state-of-view/&amp;title=Implement+State+of+View" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://wongkalun.idv.hk/2008/08/03/implement-state-of-view/&amp;title=Implement+State+of+View" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://wongkalun.idv.hk/2008/08/03/implement-state-of-view/&amp;title=Implement+State+of+View&amp;desc=%E5%B9%B3%E6%99%82%E5%AF%AB%E4%BB%8B%E9%9D%A2%28GUI%29%2C%20%E9%80%9A%E5%B8%B8%E6%9C%83%E5%AF%AB%E4%B8%80%E5%80%8B%20method%20%E5%8F%AB%20StateChange%20%E9%BB%8E%E8%99%95%E7%90%86%E6%89%80%E6%9C%89%E4%BB%8B%E9%9D%A2%E4%B8%8A%E6%97%A2%E6%89%80%E6%9C%89%20visible%20%E5%90%8C%20enable%20%E6%97%A2%E5%8B%95%E4%BD%9C.%20%E8%80%8C%E8%87%AA%E5%B7%B1%E6%9C%83%E5%9B%A0%E4%BB%8B%E9%9D%A2%E6%97%A2%E8%A8%AD%E8%A8%88%E8%80%8C%E5%AE%9A%E7%BE%A9%E4%B8%80%E5%80%8B%20private%20enum%20%E6%97%A2%20State%2C%20%E6%89%80%E6%9C%89%20state%20%E7%94%B1%E4%B8%80%E5%80%8B%20method%20%E5%81%9A%E6%99%92.%0D%0A%5Bcsharp%5D%0D%0Aprivate%20enum%20State%0D%0A%7B%0D%0A%20%20%20%20Add%2C%0D%0A%20%20%20%20Update%0D%0A%7D%0D%0A%0D%0Apri" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://wongkalun.idv.hk/2008/08/03/implement-state-of-view/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://wongkalun.idv.hk/2008/08/03/implement-state-of-view/&amp;bm_description=Implement+State+of+View&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://wongkalun.idv.hk/2008/08/03/implement-state-of-view/&amp;title=Implement+State+of+View" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://wongkalun.idv.hk/2008/08/03/implement-state-of-view/&amp;title=Implement+State+of+View" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://wongkalun.idv.hk/2008/08/03/implement-state-of-view/&amp;title=Implement+State+of+View" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://wongkalun.idv.hk/2008/08/03/implement-state-of-view/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Implement+State+of+View+-+http://bit.ly/bt9NVN&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>




		<!-- Added by WP-DragToShare-eXtended Plugin -->
		<script type="text/javascript">
			dtsv.dtse_post_124_permalink = 'http://wongkalun.idv.hk/2008/08/03/implement-state-of-view/';
			dtsv.dtse_post_124_title = 'Implement State of View';
		</script>
		<!-- End of WP-DragToShare-eXtended Plugin -->]]></content:encoded>
			<wfw:commentRss>http://wongkalun.idv.hk/2008/08/03/implement-state-of-view/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
