<?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>psychoric &#187; Coder&#8217;s Code</title>
	<atom:link href="http://www.psychoric.com/category/coders/feed" rel="self" type="application/rss+xml" />
	<link>http://www.psychoric.com</link>
	<description>still trying to figure out what this blog is about</description>
	<lastBuildDate>Fri, 12 Mar 2010 01:19:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Which group(s) am I in?</title>
		<link>http://www.psychoric.com/2008/12/04/which-groups-am-i-in</link>
		<comments>http://www.psychoric.com/2008/12/04/which-groups-am-i-in#comments</comments>
		<pubDate>Thu, 04 Dec 2008 02:58:24 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Coder's Code]]></category>

		<guid isPermaLink="false">http://www.psychoric.com/?p=58</guid>
		<description><![CDATA[If you are working in an Active Directory environment and you do utilize it for authentication in your project, this snippet might come handy. It lists the group(s) that current authenticated user belongs to, so you can expand on this to do whatever you need to.

using System.Security.Principal; //do this to save time in typing
&#160;
foreach &#40;IdentityReference [...]]]></description>
			<content:encoded><![CDATA[<p>If you are working in an Active Directory environment and you do utilize it for authentication in your project, this snippet might come handy. It lists the group(s) that current authenticated user belongs to, so you can expand on this to do whatever you need to.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Security.Principal</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//do this to save time in typing</span>
&nbsp;
<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>IdentityReference <span style="color: #0600FF;">ref</span> <span style="color: #0600FF;">in</span> WindowsIdentity.<span style="color: #0000FF;">GetCurrent</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Groups</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">//get current user group(s)</span>
	Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">ref</span>.<span style="color: #0000FF;">Translate</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>NTAccount<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</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: #008080; font-style: italic;">//translate it to meaningful value</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.psychoric.com/2008/12/04/which-groups-am-i-in/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# Generic Comparer</title>
		<link>http://www.psychoric.com/2008/04/17/c-generic-comparer</link>
		<comments>http://www.psychoric.com/2008/04/17/c-generic-comparer#comments</comments>
		<pubDate>Thu, 17 Apr 2008 02:46:53 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Coder's Code]]></category>

		<guid isPermaLink="false">http://www.psychoric.com/2008/04/17/c-generic-comparer/</guid>
		<description><![CDATA[This morning I ran into a situation where i need to sort an array of ServiceController before binding into a combobox. So I looked around and I found Array.Sort (or List.Sort). Tried with the parameter-less call, but .NET doesn't like it. One of the required overloaded methods requires a IComparer. Hmm, looks familiar and found [...]]]></description>
			<content:encoded><![CDATA[<p>This morning I ran into a situation where i need to sort an array of ServiceController before binding into a combobox. So I looked around and I found Array.Sort (or List<T>.Sort). Tried with the parameter-less call, but .NET doesn't like it. One of the required overloaded methods requires a IComparer. Hmm, looks familiar and found some straightforward examples online. Immediately I have a simple class that solves my problem.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #FF0000;">class</span> ServiceControllerSort <span style="color: #008000;">:</span> IComparer<span style="color: #008000;">&lt;</span>ServiceController<span style="color: #008000;">&gt;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Compare<span style="color: #000000;">&#40;</span>ServiceController x, ServiceController y<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Compare</span><span style="color: #000000;">&#40;</span>x.<span style="color: #0000FF;">DisplayName</span>, y.<span style="color: #0000FF;">DisplayName</span>, <span style="color: #0600FF;">true</span><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>Now what? What if next time I want to sort on my ExtendedServiceController class (as if there is necessity to do one)? I will have more and more IComparer implementations. I decided to explore further and this <a href="http://book.itzero.com/read/microsoft/0602/Addison.Wesley.Data.Binding.with.Windows.Forms.2.0.Programming.Smart.Client.Data.Applications.with.dot.NET.Jan.2006_html/032126892X/ch09lev1sec13.html" target="_blank">page</a> says it all. Took the code, clean it up and woots, I have a generic comparison class that is usable for almost all types.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Custom sorter based on property</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;typeparam name=&quot;T&quot;&gt;Target Type&lt;/typeparam&gt;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ExtSortComparer<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;:</span> IComparer<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> ListSortDirection _sortDirection <span style="color: #008000;">=</span> ListSortDirection.<span style="color: #0000FF;">Ascending</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">private</span> PropertyDescriptor _targetPropDescriptor<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
	<span style="color: #008080; font-style: italic;">/// Constructor</span>
	<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
	<span style="color: #0600FF;">public</span> ExtSortComparer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
	<span style="color: #008080; font-style: italic;">/// Constructor</span>
	<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
	<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;PropertyName&quot;&gt;Name of property to sort on. Case sensitive.&lt;/param&gt;</span>
	<span style="color: #0600FF;">public</span> ExtSortComparer<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> PropertyName<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		_targetPropDescriptor <span style="color: #008000;">=</span> TypeDescriptor.<span style="color: #0000FF;">GetProperties</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>T<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span>PropertyName<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
	<span style="color: #008080; font-style: italic;">/// Constructor</span>
	<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
	<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;PropertyName&quot;&gt;Name of property to sort on. Case sensitive.&lt;/param&gt;</span>
	<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;SortDirection&quot;&gt;Sort direction&lt;/param&gt;</span>
	<span style="color: #0600FF;">public</span> ExtSortComparer<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> PropertyName, ListSortDirection SortDirection<span style="color: #000000;">&#41;</span> <span style="color: #008000;">:</span> <span style="color: #0600FF;">this</span><span style="color: #000000;">&#40;</span>PropertyName<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		_sortDirection <span style="color: #008000;">=</span> SortDirection<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
	<span style="color: #008080; font-style: italic;">/// Constructor</span>
	<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
	<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;TargetProperty&quot;&gt;Descriptor of property to sort on&lt;/param&gt;</span>
	<span style="color: #0600FF;">public</span> ExtSortComparer<span style="color: #000000;">&#40;</span>PropertyDescriptor TargetProperty<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		_targetPropDescriptor <span style="color: #008000;">=</span> TargetProperty<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
	<span style="color: #008080; font-style: italic;">/// Constructor</span>
	<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
	<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;TargetProperty&quot;&gt;Descriptor of property to sort on&lt;/param&gt;</span>
	<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;SortDirection&quot;&gt;Sort direction&lt;/param&gt;</span>
	<span style="color: #0600FF;">public</span> ExtSortComparer<span style="color: #000000;">&#40;</span>PropertyDescriptor TargetProperty, ListSortDirection SortDirection<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		_targetPropDescriptor <span style="color: #008000;">=</span> TargetProperty<span style="color: #008000;">;</span>
		_sortDirection <span style="color: #008000;">=</span> SortDirection<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
	<span style="color: #008080; font-style: italic;">/// Compare two given object</span>
	<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
	<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;x&quot;&gt;Object A&lt;/param&gt;</span>
	<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;y&quot;&gt;Object B&lt;/param&gt;</span>
	<span style="color: #008080; font-style: italic;">/// &lt;returns&gt;Result of comparison&lt;/returns&gt;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Compare<span style="color: #000000;">&#40;</span>T x, T y<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008080; font-style: italic;">//ensure there is proper property description</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_targetPropDescriptor <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #FF0000;">int</span> intRetValue <span style="color: #008000;">=</span> _CompareValues<span style="color: #000000;">&#40;</span>_targetPropDescriptor.<span style="color: #0000FF;">GetValue</span><span style="color: #000000;">&#40;</span>x<span style="color: #000000;">&#41;</span>, 
											_targetPropDescriptor.<span style="color: #0000FF;">GetValue</span><span style="color: #000000;">&#40;</span>y<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
			<span style="color: #008080; font-style: italic;">//reverse the sorting if descending</span>
			<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_sortDirection <span style="color: #008000;">==</span> ListSortDirection.<span style="color: #0000FF;">Descending</span><span style="color: #000000;">&#41;</span>
				<span style="color: #0600FF;">return</span> intRetValue <span style="color: #008000;">*</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
&nbsp;
			<span style="color: #0600FF;">return</span> intRetValue<span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//return the result</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0600FF;">return</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//return neutral if insufficient information to sort</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
	<span style="color: #008080; font-style: italic;">/// Compare values of target property</span>
	<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
	<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;ValueX&quot;&gt;Value of property (Object A)&lt;/param&gt;</span>
	<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;ValueY&quot;&gt;Value of property (Object B)&lt;/param&gt;</span>
	<span style="color: #008080; font-style: italic;">/// &lt;returns&gt;Result of comparison&lt;/returns&gt;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">int</span> _CompareValues<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> ValueX, <span style="color: #FF0000;">object</span> ValueY<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>ValueX <span style="color: #008000;">is</span> IComparable<span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">//if X can be compared</span>
			<span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>IComparable<span style="color: #000000;">&#41;</span>ValueX<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">CompareTo</span><span style="color: #000000;">&#40;</span>ValueY<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>ValueY <span style="color: #008000;">is</span> IComparable<span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">//else if Y can be compared</span>
			<span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>IComparable<span style="color: #000000;">&#41;</span>ValueY<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">CompareTo</span><span style="color: #000000;">&#40;</span>ValueX<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">//when both can't be compared, resort to string value.</span>
		<span style="color: #0600FF;">return</span> ValueX.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">CompareTo</span><span style="color: #000000;">&#40;</span>ValueY.<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>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The comments should explain what is happening internally. Another good addition to my common library! If you have any thoughts, shoot!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.psychoric.com/2008/04/17/c-generic-comparer/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# Generic Constraints</title>
		<link>http://www.psychoric.com/2008/03/28/c-generic-constraints</link>
		<comments>http://www.psychoric.com/2008/03/28/c-generic-constraints#comments</comments>
		<pubDate>Fri, 28 Mar 2008 02:14:33 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Coder's Code]]></category>

		<guid isPermaLink="false">http://www.psychoric.com/2008/03/28/c-generic-constraints/</guid>
		<description><![CDATA[Generic is a new wonder in .NET with the flexiblity it introduces to coders. But when you wish to explore further, you might face the limitation of only able to work with Object capabilities. This is because generic doesn't know your type details, and since all types are Object, so you only have Object to [...]]]></description>
			<content:encoded><![CDATA[<p>Generic is a new wonder in .NET with the flexiblity it introduces to coders. But when you wish to explore further, you might face the limitation of only able to work with Object capabilities. This is because generic doesn't know your type details, and since all types are Object, so you only have Object to play with. To overcome this, we have Where keyword in C# (VB.net has As, i think). Apart from be able to constraint the types that generic accept, it also allows you to use the constraints capabilities.</p>
<p>Generics support few types of constraints:</p>
<ul>
<li>Interface: Allow only types that implement specific interfaces to use your generic.</li>
<li>Class/Base class: Allow only types that match or inherit from a specific base class to use your generic.</li>
<li>Constructor: Require types that use your generic to implement a parameterless constructor.</li>
</ul>
<p>Say you have a custom Interface as below</p>
<pre class="mycode">
<code>public interface IMyCustomOne
{
	string WhatsMyName();
}</code></pre>
<p>and a generic method</p>
<pre class="mycode">
<code>public bool CheckNameEqualJohn&lt;T&gt;()
{
	return (T.WhatsMyName == "John");
}</code></pre>
<p>Compiler will throw you at the instant you try to build.</p>
<p>To solve this, we add Where at the end</p>
<pre class="mycode">
<code>public bool CheckNameEqualJohn&lt;T&gt;() where T : IMyCustomOne
{
	return (T.WhatsMyName == "John");
}</code></pre>
<p>Woots, compiler is now happy! The obstacle has been removed and off you go to maximize your creativity. If you need 2 constraints, how to go about it?</p>
<pre class="mycode">
<code>public class MyClass&lt;T,U&gt;
	where T : IMyCustomT
	where U : IMyCustomU
{
	//....
}</code></pre>
<p>Some additional jargons about this constraint that will make you look cooler in front of your peers</p>
<p><strong>Unbound Type Parameter</strong><br />
Basically this refers to the most basic way of us using generic</p>
<pre class="mycode">
<code>public class MyClass&lt;T&gt;
{
	//....
}</code></pre>
<p><strong>Naked Type Constraint</strong><br />
When a generic type parameter is used as constraint</p>
<pre class="mycode">
<code>public class MyClass&lt;T&gt;
{
	public void MethodOne&lt;U&gt;(U param) where U : T
	{
		//....
	}
}</code></pre>
<p>Happy Generic'ing!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.psychoric.com/2008/03/28/c-generic-constraints/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Better comparison with enumeration</title>
		<link>http://www.psychoric.com/2008/03/11/better-comparison-with-enumeration</link>
		<comments>http://www.psychoric.com/2008/03/11/better-comparison-with-enumeration#comments</comments>
		<pubDate>Tue, 11 Mar 2008 08:45:25 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Coder's Code]]></category>

		<guid isPermaLink="false">http://www.psychoric.com/2008/03/11/better-comparison-with-enumeration/</guid>
		<description><![CDATA[Suppose you have an enum list and a variable which value comes from external source. What do you usually do to compare against the enum list?

If (MyValue == "3") { //3 means ok, do some stuff }

What if one day you decide that you don't like 3 (for whatever reason) and change it to 30? [...]]]></description>
			<content:encoded><![CDATA[<p>Suppose you have an enum list and a variable which value comes from external source. What do you usually do to compare against the enum list?</p>
<pre class="mycode">
<code>If (MyValue == "3") { //3 means ok, do some stuff }</code>
</pre>
<p>What if one day you decide that you don't like 3 (for whatever reason) and change it to 30? You will have to find and replace the repeated value all over your project. Yes, you have the FindAndReplace function with most IDE, but holy cow, you only realize that the last couple of 3 meant something else!</p>
<p>So a safer way is to cast the value into appropriate enum for comparison or any other operation.</p>
<pre class="mycode">
<code>'VB.NET
dim myEnumValue as TheEnum = CType([Enum].Parse(GetType(TheEnum), _
				tempValue.ToString()), TheEnum)
If myEnumValue = TheEnum.NumberThree Then
	'..
End If

//C#
TheEnum myEnumValue = (TheEnum)Enum.Parse(typeof(TheEnum), tempValue);
if (myEnumValue == TheEnum.NumberThree) { //.. }</code>
</pre>
<p>"Hey, that looks useful. I want it in my SuperCoolReusable library" you might say. With the power of Generic, we can put it into a common call.</p>
<pre class="mycode">
<code>'VB.NET
Public Shared Function ParseEnum(Of T)(ByVal Value As Object) As T
	Return CType([Enum].Parse(GetType(T), Value.ToString()), T)
End Function

//C#
public static ReturnType ConvertStringToEnum&lt;ReturnType&gt;(string Value)
{
	return (ReturnType)Enum.Parse(typeof(ReturnType), Value);
}</code>
</pre>
<p>Another simple yet useful tip! <img src='http://www.psychoric.com/wp/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.psychoric.com/2008/03/11/better-comparison-with-enumeration/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I Learned and Applied RegularExpression Today</title>
		<link>http://www.psychoric.com/2008/01/29/i-learned-and-applied-regularexpression-today</link>
		<comments>http://www.psychoric.com/2008/01/29/i-learned-and-applied-regularexpression-today#comments</comments>
		<pubDate>Tue, 29 Jan 2008 06:56:17 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Coder's Code]]></category>

		<guid isPermaLink="false">http://www.psychoric.com/2008/01/29/i-learned-and-applied-regularexpression-today/</guid>
		<description><![CDATA[Scenario: I have a number of patterns that I need to check and replace in a string. I don't want to transverse the string and play with characters and length. What other option I have?

Answer: Ah, RegularExpression is your saviour today.
Say you have defined one pattern '{D:mmddyy}' so that you can have a dynamic date [...]]]></description>
			<content:encoded><![CDATA[<p>Scenario: I have a number of patterns that I need to check and replace in a string. I don't want to transverse the string and play with characters and length. What other option I have?</p>
<p><span id="more-17"></span></p>
<p>Answer: Ah, RegularExpression is your saviour today.</p>
<p>Say you have defined one pattern <code class="mycodetext">'{D:mmddyy}'</code> so that you can have a dynamic date part in the string value. Your string variable :</p>
<pre><code class="mycode">string myValue = "My mum say i am a good boy on {D:ddMMMyyy}";</code></pre>
<p>Firstly, you need to declare your pattern in regular expression format, and it should be:</p>
<pre class="mycode"><code>string myPattern = "(?&lt;myPat&gt;{d:[mdy]+})";</code></pre>
<p>Here we use <code class="mycodetext">(?&lt;name&gt;=pattern)</code>, which is what we called Grouping Construct to group pattern together. We easily retrieve the value with the grouping as you can see later. Once you have the pattern ready, we are ready to utilize .NET RegularExpression (RegEx)</p>
<pre><code class="mycode">MatchCollection colMatches = RegEx.Matches(myValue, myPattern, RegExOptions.IgnoreCase);</code></pre>
<p>Note that I use <code class="mycodetext">RegExOptions.IgnoreCase</code> here because I am lazy to define all cases of date-time formatting characters. And <code class="mycodetext">MatchCollection</code> is used as I expect to have multiple similar patterns in my string. If you don't, you can replace it with:</p>
<pre><code class="mycode">Match pMatch = RegEx.Match(myValue, myPattern, RegExOptions.IgnoreCase);</code></pre>
<p>If everything is ok, we should have some results.</p>
<pre><code class="mycode">foreach (Match pMatch in colMatches)
{
	Console.WriteLine("{0} is found at {1}", pMatch.Value, pMatch.Index);
}</code></pre>
<p><code class="mycodetext">pMatch.Value</code> should give you the pattern found in the string, and <code class="mycodetext">pMatch.Index</code> is the index of occurence. Once you have this information, you can do anything you can! </p>
<p>Extending from above, I use back similar technique to parse the matched pattern.</p>
<pre><code class="mycode">string PATTERN_IN_PATTERN = @"{(?&lt;key&gt;\w+)(:(?&lt;pattern&gt;\w+))?}";
Match pInnerMatch = Regex.Match(pMatch.Value,
					PATTERN_IN_PATTERN, RegexOptions.IgnoreCase);

if (pInnerMatch.Success) //if there is a match
{
	//retrieve the "key" group value
	switch (pInnerMatch.Result("${key}").ToUpper())
	{
		case "D":
			// use the "pattern" group value to format today's date
			myValue = myValue.Replace(pMatch.Value,
					DateTime.Now.ToString(pInnerMatch.Result("${pattern}")));
	}
}</code></pre>
<p>The final result:</p>
<pre><code class="mycode">My mum say i am a good boy on 29Jan2008</code></pre>
<p>Easy, right? <img src='http://www.psychoric.com/wp/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  Don't have to mess with indexes, where you need to worry about whether the count starts from 0 or 1. And it is reusable in any language! What if you have more than one patterns? Just declare a collection/array (i am sure you know how) and do the hula-loop! <img src='http://www.psychoric.com/wp/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.psychoric.com/2008/01/29/i-learned-and-applied-regularexpression-today/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I want the library FAST!</title>
		<link>http://www.psychoric.com/2008/01/28/i-want-the-library-fast</link>
		<comments>http://www.psychoric.com/2008/01/28/i-want-the-library-fast#comments</comments>
		<pubDate>Mon, 28 Jan 2008 06:52:39 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Coder's Code]]></category>

		<guid isPermaLink="false">http://www.psychoric.com/2008/01/28/i-want-the-library-fast/</guid>
		<description><![CDATA[My MSDN 2008 Library didn't install a direct link to launch help like the 2005 version. So i tweaked the 2005 shortcut and hoila, i have the shortcut for 2008 version. You like the F1, but i prefer this so i can easily answer people's query without opening the IDE  
Create a new Shortcut [...]]]></description>
			<content:encoded><![CDATA[<p>My MSDN 2008 Library didn't install a direct link to launch help like the 2005 version. So i tweaked the 2005 shortcut and hoila, i have the shortcut for 2008 version. You like the F1, but i prefer this so i can easily answer people's query without opening the IDE <img src='http://www.psychoric.com/wp/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>Create a new Shortcut and put this in the shortcut path (single line):</p>
<p><code class="mycode">"%YOUR PROGRAM FILES%\Common Files\Microsoft Shared\Help 9\dexplore.exe"<br />
/helpcol ms-help://MS.MSDNQTR.v90.en /LaunchNamedUrlTopic DefaultPage</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.psychoric.com/2008/01/28/i-want-the-library-fast/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>coalescing null yield return</title>
		<link>http://www.psychoric.com/2008/01/26/coalescing-null-yield-return</link>
		<comments>http://www.psychoric.com/2008/01/26/coalescing-null-yield-return#comments</comments>
		<pubDate>Fri, 25 Jan 2008 17:25:41 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Coder's Code]]></category>

		<guid isPermaLink="false">http://www.psychoric.com/2008/01/26/coalescing-null-yield-return/</guid>
		<description><![CDATA[Ok the title means nothing at all, and they are not related too, except that they both are of C# family. As requested by Klaw, i am going to blog about it so that we all learn  
Coalescing Null Operator
You are probably thinking what's that. Maybe it is just me, but it is one [...]]]></description>
			<content:encoded><![CDATA[<p>Ok the title means nothing at all, and they are not related too, except that they both are of C# family. As requested by Klaw, i am going to blog about it so that we all learn <img src='http://www.psychoric.com/wp/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p><u><strong>Coalescing Null Operator</strong></u></p>
<p>You are probably thinking what's that. Maybe it is just me, but it is one of the cool subtle features in C# that I just discovered today. Shame on me ya. Basically you all are familiar with this:</p>
<p><span style="color: #993300"><code>string myStrVarB = (myStrVarA == null) ? "null string" : myStrVarA</code></span></p>
<p>So with coalescing null operator (??), you can shortened it to:</p>
<p><span style="color: #993300"><code>string myStrVarB = myStrVarA ?? "null string"</code></span></p>
<p>Let's see, that is saving of 17 characters. Your boss sure will praise you for that <img src='http://www.psychoric.com/wp/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  On a side note, in case you don't know, you can allow a scalar type to be null by using the nullable operator:</p>
<p><span style="color: #993300"><code>int? myInteger = null</code></span></p>
<p><u><strong>yield return</strong></u></p>
<p>Definition from MSDN: "Used in an iterator block to provide a value to the enumerator object or to signal the end of iteration". I assume you have previous knowledge with IEnumerable  (or iterator), so i am not going into detail with it. The yield statement appears only inside an iterator block and returns the value that is expected by the calling foreach statement. Cut the theory and show me the example.</p>
<pre class="mycode">
<code>public static IEnumerable Power(int number, int exponent)
{
	int counter = 0;
	int result = 1;
	while (counter++ &lt; exponent)
	{
		result = result * number;
		yield return result;
	}
}

static void Main()
{
	// Display powers of 2 up to the exponent 8:
	foreach (int i in Power(2, 8))
	{
		Console.Write("{0} ", i);
	}
}</code>
</pre>
<p>There you go, another simple bit of my learning <img src='http://www.psychoric.com/wp/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.psychoric.com/2008/01/26/coalescing-null-yield-return/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Begin Invoke..End Invoke</title>
		<link>http://www.psychoric.com/2008/01/21/begin-invokeend-invoke</link>
		<comments>http://www.psychoric.com/2008/01/21/begin-invokeend-invoke#comments</comments>
		<pubDate>Mon, 21 Jan 2008 13:58:32 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Coder's Code]]></category>

		<guid isPermaLink="false">http://www.psychoric.com/2008/01/21/begin-invokeend-invoke/</guid>
		<description><![CDATA[First post in the category   This group is mainly to share coding stuff that is out of layman term. If you are not programmer, stay away at all cost!  
Found this very easy to read yet informative post on Asynchronous Method Invocation. I admit that what i know about .NET is merely [...]]]></description>
			<content:encoded><![CDATA[<p>First post in the category <img src='http://www.psychoric.com/wp/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  This group is mainly to share coding stuff that is out of layman term. If you are not programmer, stay away at all cost! <img src='http://www.psychoric.com/wp/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p>Found this very easy to read yet informative post on Asynchronous Method Invocation. I admit that what i know about .NET is merely the tiny tip of what the iceberg can offer. Oh hell, life is long and learning is life long, ain't it? <img src='http://www.psychoric.com/wp/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><cite></cite>Here's a summary of what i learnt:</p>
<ul>
<li><span style="font-family: courier new,courier; color: #993300">MethodInvoke</span> - built in delegate in .NET that allow you to call a method that takes in no parameter</li>
<li><span style="font-family: courier new,courier; color: #993300">BeginInvoke</span> - execute the target function and return to caller instantly</li>
<li><span style="font-family: courier new,courier; color: #993300">ThreadPool</span> in .NET has a limit of 25. Avoid 'starving' the pool else be fined by performance loss</li>
<li><span style="font-family: courier new,courier; color: #993300">EndInvoke</span> - return proper values/parameters from the previous function call</li>
<li><span style="font-family: courier new,courier; color: #993300">IAsyncResult</span> - log of all datas (exceptions, return value, output parameters, etc) after execution is completed</li>
<li><span style="font-family: courier new,courier; color: #993300">AsyncCallBack</span> - used when you don't wish to wait for completion of the execution (that's the purpose of asynchronous call bah)</li>
<li><strong>Command Pattern</strong> (<span style="font-family: courier new,courier; color: #993300">ICommand</span> interface) - rather than <span style="font-family: courier new,courier; color: #993300">BeginInvoke</span>, <span style="font-family: courier new,courier; color: #993300">EndInvoke</span>, <span style="font-family: courier new,courier; color: #993300">CallBack</span> everywhere in your Main(), apply this pattern to group calls together.</li>
</ul>
<p>That's all folks. I might not have explained everything properly, so best for you to read it yourself.</p>
<p><a href="http://www.codeproject.com/KB/cs/AsyncMethodInvocation.aspx" target="_blank">Original post</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.psychoric.com/2008/01/21/begin-invokeend-invoke/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
