<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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" version="2.0">

<channel>
	<title>Weblog of Nirandas</title>
	
	<link>http://nirandas.com/blog</link>
	<description>“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” - Martin Fowler</description>
	<pubDate>Wed, 29 Apr 2009 03:38:49 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.1</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/ADeveloperFromIndia" type="application/rss+xml" /><item>
		<title>Using Smarty with codeigniter</title>
		<link>http://nirandas.com/blog/index.php/using-smarty-with-codeigniter/</link>
		<comments>http://nirandas.com/blog/index.php/using-smarty-with-codeigniter/#comments</comments>
		<pubDate>Sat, 14 Mar 2009 07:39:59 +0000</pubDate>
		<dc:creator>Niran</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[php]]></category>

		<category><![CDATA[codeigniter]]></category>

		<category><![CDATA[smarty]]></category>

		<guid isPermaLink="false">http://nirandas.com/blog/?p=111</guid>
		<description><![CDATA[If you like to use smarty for designing your views while developing codeigniter applications, just read on. Smarty is a powerful templating engine for php. Its template language is simple yet powerful. By default, codeigniter doesn&#8217;t use any templating system. Instead it relies on plain old php for designing views. Integrating smarty into codeigniter is [...]]]></description>
			<content:encoded><![CDATA[<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">If you like to use smarty for designing your views while developing codeigniter applications, just read on. Smarty is a powerful templating engine for php. Its template language is simple yet powerful. By default, codeigniter doesn&#8217;t use any templating system. Instead it relies on plain old php for designing views. Integrating smarty into codeigniter is rather simple. First, download the latest smarty version and copy the libs folder into /system/application/libraries/.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: 10pt; line-height: 115%; font-family: &quot;Courier New&quot;;">Now create a new file “View.php” inside the /system/application/libraries folder. Note: the case of the filename is important it must be “View.php”. Now put the following code inside the View.php file.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: 10pt; line-height: 115%; font-family: &quot;Courier New&quot;;"> </span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><a name="OLE_LINK1"></a><a name="OLE_LINK2"><span style="mso-bookmark: OLE_LINK1;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">&lt;? php </span></span></a><span style="mso-bookmark: OLE_LINK2;"><span style="mso-bookmark: OLE_LINK1;"></span></span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="mso-bookmark: OLE_LINK2;"><span style="mso-bookmark: OLE_LINK1;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">require_once(dirname(__FILE__).&#8217;/libs/Smarty.class.php&#8217;);</span></span></span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="mso-bookmark: OLE_LINK2;"><span style="mso-bookmark: OLE_LINK1;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">class View extends Smarty{</span></span></span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="mso-bookmark: OLE_LINK2;"><span style="mso-bookmark: OLE_LINK1;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">public $layout=&#8217;default&#8217;;</span></span></span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="mso-bookmark: OLE_LINK2;"><span style="mso-bookmark: OLE_LINK1;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">function __construct(){</span></span></span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="mso-bookmark: OLE_LINK2;"><span style="mso-bookmark: OLE_LINK1;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">parent::__construct();</span></span></span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="mso-bookmark: OLE_LINK2;"><span style="mso-bookmark: OLE_LINK1;">$this-&gt;compile_check = true;</span></span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="mso-bookmark: OLE_LINK2;"><span style="mso-bookmark: OLE_LINK1;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">$this-&gt;debugging = false;</span></span></span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="mso-bookmark: OLE_LINK2;"><span style="mso-bookmark: OLE_LINK1;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">$this-&gt;template_dir = APPPATH.&#8217;views&#8217;;</span></span></span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="mso-bookmark: OLE_LINK2;"><span style="mso-bookmark: OLE_LINK1;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">$this-&gt;compile_dir = APPPATH.&#8217;temp&#8217;;</span></span></span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="mso-bookmark: OLE_LINK2;"><span style="mso-bookmark: OLE_LINK1;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">}</span></span></span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="mso-bookmark: OLE_LINK2;"><span style="mso-bookmark: OLE_LINK1;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">function show($name){</span></span></span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="mso-bookmark: OLE_LINK2;"><span style="mso-bookmark: OLE_LINK1;"><a name="OLE_LINK4"></a><a name="OLE_LINK3"><span style="mso-bookmark: OLE_LINK4;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">$content=$this-&gt;fetch($name.&#8217;.tpl&#8217;);</span></span></a></span></span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="mso-bookmark: OLE_LINK2;"><span style="mso-bookmark: OLE_LINK1;"><span style="mso-bookmark: OLE_LINK3;"><span style="mso-bookmark: OLE_LINK4;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">$this-&gt;assign(&#8217;content&#8217;,$content);</span></span></span></span></span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="mso-bookmark: OLE_LINK2;"><span style="mso-bookmark: OLE_LINK1;"><span style="mso-bookmark: OLE_LINK3;"><span style="mso-bookmark: OLE_LINK4;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">$this-&gt;display(&#8217;layout/&#8217;.$this-&gt;layout.&#8217;.tpl&#8217;);</span></span></span></span></span></p>
<p> </p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="mso-bookmark: OLE_LINK2;"><span style="mso-bookmark: OLE_LINK1;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">}</span></span></span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="mso-bookmark: OLE_LINK2;"><span style="mso-bookmark: OLE_LINK1;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">}</span></span></span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="mso-bookmark: OLE_LINK2;"><span style="mso-bookmark: OLE_LINK1;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">?&gt;</span></span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="mso-bookmark: OLE_LINK2;"><span style="mso-bookmark: OLE_LINK1;"><span style="font-size: small; font-family: Calibri;"> </span></span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="mso-bookmark: OLE_LINK2;"><span style="mso-bookmark: OLE_LINK1;"><span style="font-size: small; font-family: Calibri;">Point of interest:</span></span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">We have a member variable on our view class which will hold the name of the layout template. By default it is set to ‘default’.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">In the constructor, we are telling smarty where to look for our view templates and where to save the compiled version of our templates. Smarty will look for views in /system/application/views/ and it will save the compiled templates inside the /system/application/temp/ folder. Please do set the required permissions on the /system/application/temp/ folder enabling Smarty to write to it.</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">We will be calling the show() function of our view class for rendering the views. The $name parameter should be the path to the view without the “.tpl” extension. For example:</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">$this-&gt;view-&gt;show(‘site/login’);</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">Inside the show () function, we are fetching the output of the view specified using the following line</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">$content=$this-&gt;fetch($name.&#8217;.tpl&#8217;);</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">Then, we are assigning the output of the view to a smarty variable named ‘content’</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">$this-&gt;assign(&#8217;content&#8217;,$content);</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">Finally, we are displaying the layout template using the below line</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">$this-&gt;display(&#8217;layout/&#8217;.$this-&gt;layout.&#8217;.tpl&#8217;);</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">Now our layout template should use the assigned variable ‘content’ for outputting the contents of the view captured earlier. For example</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">&lt;html&gt;</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">&lt;head&gt;…&lt;/head&gt;</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">&lt;body&gt;</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">…</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">{$content}</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">…</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">&lt;/body&gt;</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">&lt;/html&gt;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">Neat! Instead of views including the headers and footers they need, now we have a common, site wide layout template much like ASP.NET’s master pages. The layout template must be placed inside a folder named “layout” inside the /system/application/views/ folder.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">Using the view class</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">For using this new view class, you must load the View library using </span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">$this-&gt;load-&gt;library(‘view’); </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">Or you can auto load the view library so that you do not have to load it in each controller action. Once the view class is loaded either through auto loading or using code, use the similar code as shown below for displaying views.</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">$this-&gt;view-&gt;assign(&#8217;name’,’nirandas’);</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">$this-&gt;view-&gt;show(&#8217;hello’);</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;;"> </span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">All methods of the Smarty object are available to us through $this-&gt;view variable. For assigning variables we can use </span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">$this-&gt;view-&gt;assign(‘var_name’,’var_value’); </span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">And for displaying our pages we can use </span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: x-small; color: #92d050; font-family: Courier New;">$this-&gt;view-&gt;show(‘view_name’); </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;"> </span></p>
<img src="http://feeds.feedburner.com/~r/ADeveloperFromIndia/~4/Fz2QhRkd7FM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://nirandas.com/blog/index.php/using-smarty-with-codeigniter/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Beginning Reflection.Emit, a basic example</title>
		<link>http://nirandas.com/blog/index.php/beginning-reflectionemit-a-basic-example/</link>
		<comments>http://nirandas.com/blog/index.php/beginning-reflectionemit-a-basic-example/#comments</comments>
		<pubDate>Fri, 16 Jan 2009 13:49:38 +0000</pubDate>
		<dc:creator>Niran</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[c#]]></category>

		<category><![CDATA[Emit]]></category>

		<category><![CDATA[Reflection]]></category>

		<guid isPermaLink="false">http://nirandas.com/blog/?p=106</guid>
		<description><![CDATA[The System.Reflection.Emit namespace provides us the tools required to generate .NET code on the fly. Especially the DynamicMethod class makes the job of creating small code snippet easier. In this post I’ll show a simple example of dynamically creating a method which takes a string as its argument and returns a string “hi “prepended to [...]]]></description>
			<content:encoded><![CDATA[<p>The System.Reflection.Emit namespace provides us the tools required to generate .NET code on the fly. Especially the DynamicMethod class makes the job of creating small code snippet easier. In this post I’ll show a simple example of dynamically creating a method which takes a string as its argument and returns a string “hi “prepended to it. First, we will create the DynamicMethod object specifying the argument list and the return type of the dynamic method. Next we will emit the MSIL required to do the task, yes, we can only create dynamic methods using MSIL. Then we will create a delegate from our dynamic method. Then we can call the delegate like we would do with any delegate.</p>
<p>Okay, let us declare the delegate first</p>
<p>//Delegate matching the method to be generated<br />
delegate string SayHi(string name);</p>
<p>Now, let us look at the actual code:</p>
<p>//create the DynamicMethod object<br />
DynamicMethod method=new DynamicMethod(&#8221;method&#8221;,//Method name<br />
typeof(string),//The return type<br />
new Type[]{typeof(string)},//array of types specifying the type of arguments and their order<br />
typeof(Program).Module //The module to which the method will be connected<br />
);<br />
//Create the IL generater<br />
ILGenerator il=method.GetILGenerator();<br />
//Load the const string &#8220;hi &#8221; on to the evaluation stack<br />
il.Emit(OpCodes.Ldstr,&#8221;Hi &#8220;);<br />
//Load the argument, the name<br />
il.Emit (OpCodes.Ldarg_0 );<br />
//call the string.Concat() with 2 string parameters<br />
il.EmitCall(OpCodes.Call,<br />
typeof(string).GetMethod(&#8221;Concat&#8221;,new Type[]{typeof(string),typeof(string)}) ,//Find and pass the overload of Concat which accepts 2 strings<br />
null );<br />
//return, effectivly returning the result of string.Concat()<br />
il.Emit(OpCodes.Ret);<br />
// Complete the IL generation and create a delegate<br />
SayHi hi=(SayHi)method.CreateDelegate(typeof(SayHi));</p>
<p>//execute our generated method with &#8220;nirandas&#8221; as the parameter<br />
MessageBox.Show(hi(&#8221;Nirandas&#8221;));</p>
<p>For more information about dynamic method creation, visit <a href="http://msdn.microsoft.com/en-us/library/exczf7b9.aspx">this MSDN page</a>.<br />
Happy coding!</p>
<img src="http://feeds.feedburner.com/~r/ADeveloperFromIndia/~4/Rvl4fJzcj4I" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://nirandas.com/blog/index.php/beginning-reflectionemit-a-basic-example/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Determining if a Type implements an interface</title>
		<link>http://nirandas.com/blog/index.php/determining-if-a-type-implements-an-interface/</link>
		<comments>http://nirandas.com/blog/index.php/determining-if-a-type-implements-an-interface/#comments</comments>
		<pubDate>Tue, 13 Jan 2009 05:08:57 +0000</pubDate>
		<dc:creator>Niran</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[c#]]></category>

		<category><![CDATA[DotNET]]></category>

		<guid isPermaLink="false">http://nirandas.com/blog/?p=104</guid>
		<description><![CDATA[If we have an object of a type, we can easily check if that object implements a particular type by using is operator.
 
if(o is IExample)
Console.WriteLine(&#8220;Yes&#8221;); 
 
Here the If statement will evaluate to true if the object o implements the interface IExample or the object is a derived object of IExample.
 
However, we cannot use is operator [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">If we have an object of a type, we can easily check if that object implements a particular type by using is operator.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;"> </span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small;"><span style="font-family: Courier New;"><span style="color: blue;">if</span>(o <span style="color: blue;">is</span> <span style="color: teal;">IExample</span>)</span></span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small;"><span style="font-family: Courier New;"><span style="color: teal;">Console</span>.WriteLine(<span style="color: maroon;">&#8220;Yes&#8221;</span>); </span></span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small;"><span style="font-family: Calibri;">Here the If statement will evaluate to true if the object o implements the interface </span><span class="CodeChar"><span style="mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;"><span style="font-family: Courier New;">IExample</span></span></span><span style="font-family: Calibri;"> or the object is a derived object of </span><span class="CodeChar"><span style="mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;"><span style="font-family: Courier New;">IExample</span></span></span><span style="font-family: Calibri;">.</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">However, we cannot use is operator if all we got is a type and we want to find whether that type implements a particular interface.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;"> </span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">Type.IsAssignableFrom()</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small;"><span style="font-family: Calibri;">The </span><span class="CodeChar"><span style="mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;"><span style="font-family: Courier New;">System.Type</span></span></span><span style="font-family: Calibri;"> class has an </span><span class="CodeChar"><span style="mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;"><span style="font-family: Courier New;">IsAssignableFrom()</span></span></span><span style="font-family: Calibri;"> method which does exactly what we want. The </span><span class="CodeChar"><span style="mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;"><span style="font-family: Courier New;">IsAssignableFrom()</span></span></span><span style="font-family: Calibri;"> method takes a type as its argument and returns true if the object of the provided type can be assigned to a variable of the current type. See the following example:</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;"> </span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small;"><span style="font-family: Courier New;">interface <span style="color: teal;">IExample</span></span></span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">{</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small;"><span style="font-family: Courier New;"><span style="mso-spacerun: yes;"> </span>void Do();</span></span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">}</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">class <span style="color: teal;">Example</span> : <span style="color: teal;">IExample</span> </span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">{</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;"> </span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small;"><span style="font-family: Courier New;"><span style="mso-spacerun: yes;">    </span>#region IExample Members</span></span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;"> </span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small;"><span style="font-family: Courier New;"><span style="mso-spacerun: yes;">    </span>public void Do()</span></span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small;"><span style="font-family: Courier New;"><span style="mso-spacerun: yes;">    </span>{</span></span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small;"><span style="font-family: Courier New;"><span style="mso-spacerun: yes;">        </span>throw new <span style="color: teal;">Exception</span>(<span style="color: maroon;">&#8220;The method or operation is not implemented.&#8221;</span>);</span></span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small;"><span style="font-family: Courier New;"><span style="mso-spacerun: yes;">    </span>}</span></span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;"> </span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small;"><span style="font-family: Courier New;"><span style="mso-spacerun: yes;">    </span>#endregion</span></span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">}</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small;"><span style="font-family: Calibri;">Here we are declaring an interface </span><span class="CodeChar"><span style="mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;"><span style="font-family: Courier New;">IExample</span></span></span><span style="font-family: Calibri;"> and a class </span><span class="CodeChar"><span style="mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;"><span style="font-family: Courier New;">Example</span></span></span><span style="font-family: Calibri;"> which implements the </span><span class="CodeChar"><span style="mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;"><span style="font-family: Courier New;">IExample</span></span></span><span style="font-family: Calibri;"> interface. Now let us see how we can check that the type </span><span class="CodeChar"><span style="mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;"><span style="font-family: Courier New;">Example</span></span></span><span style="font-family: Calibri;"> implements </span><span class="CodeChar"><span style="mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;"><span style="font-family: Courier New;">IExample </span></span></span><span style="font-family: Calibri;">interface.</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;"> </span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small;"><span style="font-family: Courier New;"><span style="color: blue;">if</span>(<span style="color: blue;">typeof</span>(<span style="color: teal;">IExample</span>).IsAssignableFrom(<span style="color: blue;">typeof</span>(<span style="color: teal;">Example</span>)))</span></span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small;"><span style="font-family: Courier New;"><span style="color: teal;">Console</span>.WriteLine(<span style="color: maroon;">&#8220;Yes&#8221;</span>); </span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small;"><span style="font-family: Calibri;">This will output “yes” to the console as the interface </span><span class="CodeChar"><span style="mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;"><span style="font-family: Courier New;">IExample</span></span></span><span style="font-family: Calibri;"> is implemented by class </span><span class="CodeChar"><span style="mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;"><span style="font-family: Courier New;">Example</span></span></span><span style="font-family: Calibri;"> class. For more information on </span><span class="CodeChar"><span style="mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;"><span style="font-family: Courier New;">Type.IsAssignableFrom()</span></span></span><span style="font-family: Calibri;"> method, visit </span></span><a href="http://msdn.microsoft.com/en-us/library/system.type.isassignablefrom.aspx"><span style="font-size: small; font-family: Calibri;">this MSDN page</span></a><span style="font-size: small; font-family: Calibri;">.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">Happy coding!</span></p>
<img src="http://feeds.feedburner.com/~r/ADeveloperFromIndia/~4/P88AbVrHixw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://nirandas.com/blog/index.php/determining-if-a-type-implements-an-interface/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Basics of PHP</title>
		<link>http://nirandas.com/blog/index.php/basics-of-php/</link>
		<comments>http://nirandas.com/blog/index.php/basics-of-php/#comments</comments>
		<pubDate>Mon, 12 Jan 2009 16:56:07 +0000</pubDate>
		<dc:creator>Niran</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[php]]></category>

		<category><![CDATA[Learning PHP]]></category>

		<guid isPermaLink="false">http://nirandas.com/blog/?p=99</guid>
		<description><![CDATA[What is PHP?
PHP is a high level scripting language particularly used in developing websites. PHP is very powerful and easy to learn. PHP is platform independent that is, PHP can run on either Linux or Windows and many other OS. Unlike C or C++, PHP is an interpreted language that is, the PHP code we [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">What is PHP?</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">PHP is a high level scripting language particularly used in developing websites. PHP is very powerful and easy to learn. PHP is platform independent that is, PHP can run on either Linux or Windows and many other OS. Unlike C or C++, PHP is an interpreted language that is, the PHP code we write doesn’t get converted into machine code. Instead the PHP reads and executes the source code when we run the script. For more information about PHP and its history, visit </span><a href="http://en.wikipedia.org/wik"><span style="font-size: small; font-family: Calibri;">http://en.wikipedia.org/wik</span></a><span style="font-size: small; font-family: Calibri;">i/PHP</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">PHP and web server</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">For creating websites, PHP must be installed and configured on the server hosting the website. Although any modern web server can be configured to run PHP and serve PHP websites, the most widely used combination is Linux, apache, MySql and PHP or jointly known as LAMP. On Windows platform, we can install PHP and configure IIS for accepting requests for PHP. Or we can install WAMP, a single package containing apache, MySql and PHP for Windows and use it instead.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">Web request lifecycle</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">When we request a page through any web browser, the browser creates a specially formatted message (HTTP Request) and sends to the URL we requested. On receiving the request from the client, that is the browser, server invokes the PHP engine and provides the information about the page requested. Now the PHP engine reads our PHP file, parses it and execute the PHP code inside the file. After the execution the output of our script is send back to the client. The whole communication between web browser and the web server is governed by the HTTP protocol, a common language by which browser and server exchange information.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">PHP hello world </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">From now on, we will look at actual code examples. So I expect you have installed and configured PHP or you have access to a properly configured PHP environment.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">Create a file under the web server’s root and save the following code in it. Name the file as “hello.php”.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">Listing 1.1</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">&lt;?php</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">Echo “Hello world”;</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">?&gt;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small;"><span style="font-family: Calibri;">Let us examine the code. The first line, </span><span class="CodeChar"><span style="mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;"><span style="font-family: Courier New;">&lt;?php</span></span></span><span style="font-family: Calibri;"> indicates the start of PHP code block. The “</span><span class="CodeChar"><span style="mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;"><span style="font-family: Courier New;">?&gt;” </span></span></span><span style="font-family: Calibri;">on the third line indicates the closing of the previously opened code block. PHP only executes code found inside these code blocks, anything outside of these code block is not parsed and printed on the page as it is. For example consider the following code snippet:</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">Listing 1.2</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">&lt;html&gt;</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">&lt;head&gt;</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">&lt;title&gt;hello world example 2&lt;/title&gt;</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">&lt;/head&gt;</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">&lt;body&gt;</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">&lt;p&gt;</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">&lt;?php</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">echo &#8220;hello world!&#8221;;</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">?&gt;</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;"> </span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">&lt;/p&gt;</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">&lt;/body&gt;</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">&lt;/html&gt;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">Will output following to the browser:</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">Listing 1.2.1</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">&lt;html&gt;</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">&lt;head&gt;</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">&lt;title&gt;hello world example 2&lt;/title&gt;</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">&lt;/head&gt;</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">&lt;body&gt;</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">&lt;p&gt;</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">hello world!</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">&lt;/p&gt;</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">&lt;/body&gt;</span></p>
<p class="Code" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Courier New;">&lt;/html&gt;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small;"><span style="font-family: Calibri;">There can be 0 or more such PHP code blocks on a single page. Now the second line in the listing 1.1, </span><span class="CodeChar"><span style="mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;"><span style="font-family: Courier New;">echo “Hello world!”;</span></span></span><span style="font-family: Calibri;"> does the actual work of printing the text “Hello world!” to the webpage. The </span><span class="CodeChar"><span style="mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;"><span style="font-family: Courier New;">echo</span></span></span><span style="font-family: Calibri;"> and </span><span class="CodeChar"><span style="mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;"><span style="font-family: Courier New;">print</span></span></span><span style="font-family: Calibri;"> statements are used for writing to the output. Another thing to notice is that PHP requires that we should terminate each PHP statement using a ’;’ symbol.</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">Summary</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">PHP is a free, platform independent, scripting language. PHP is interpreted on the runtime not compiled as some traditional languages like C/C++ are. Also PHP can be setup to work with almost any web server including IIS and apache. </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small;"><span style="font-family: Calibri;">The PHP code is written inside files with </span><span class="CodeChar"><span style="mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi;"><span style="font-family: Courier New;">.php</span></span></span><span style="font-family: Calibri;"> extension. We use special markers to differentiate code from normal text by enclosing the code inside it.</span></span></p>
<img src="http://feeds.feedburner.com/~r/ADeveloperFromIndia/~4/NMP9gFumh4k" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://nirandas.com/blog/index.php/basics-of-php/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Failing tests as a reminder of unimplemented requirement</title>
		<link>http://nirandas.com/blog/index.php/failing-tests-as-a-reminder-of-unimplemented-requirement/</link>
		<comments>http://nirandas.com/blog/index.php/failing-tests-as-a-reminder-of-unimplemented-requirement/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 11:09:51 +0000</pubDate>
		<dc:creator>Niran</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://nirandas.com/blog/index.php/failing-tests-as-a-reminder-of-unimplemented-requirement/</guid>
		<description><![CDATA[I am a new comer to the art of unit testing and it has not yet become the part of my coding routine. However, even this early I have realized how useful writing unit tests can be. Specially writing unit tests gives you the understanding how the class will be used and where the errors [...]]]></description>
			<content:encoded><![CDATA[<p>I am a new comer to the art of unit testing and it has not yet become the part of my coding routine. However, even this early I have realized how useful writing unit tests can be. Specially writing unit tests gives you the understanding how the class will be used and where the errors can arise etc. For example passing the Type System.String to a method where the method expects a Type which implements a particular interface ICommand should result in an error because the System.String does not certainly implements the interface ICommand. But does the method validate the input?</p>
<p>Now writing a test for this case is very simple. Just create the object, call the method with System.String as the Type parameter. </p>
<p>[Test]<br />
[ExpectedException(typeof(ArgumentException))]<br />
public void CanNotRegisterTypesNotImplementingICommand()<br />
{<br />
CommandLocater locater=new CommandLocater();<br />
locater.RegisterCommand(1,typeof(string));<br />
}</p>
<p>Certainly I haven’t implemented the validation code for checking this, so the test is certainly failing. Until I feel like doing this validation, this test will always fail. Lot better than forgetting to write this code and getting an unrelated exception in any other section of the code is getting a failing test which will make sure that I’ll write the code necessary to implement this requirement.<br />
“Always write tests to break your code.”</p>
<img src="http://feeds.feedburner.com/~r/ADeveloperFromIndia/~4/zTWD9cZpgVo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://nirandas.com/blog/index.php/failing-tests-as-a-reminder-of-unimplemented-requirement/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Sorting list the easy way using lambda expressions</title>
		<link>http://nirandas.com/blog/index.php/sorting-list-the-easy-way-using-lambda-expressions/</link>
		<comments>http://nirandas.com/blog/index.php/sorting-list-the-easy-way-using-lambda-expressions/#comments</comments>
		<pubDate>Mon, 15 Dec 2008 06:41:37 +0000</pubDate>
		<dc:creator>Niran</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[c#]]></category>

		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://nirandas.com/blog/?p=92</guid>
		<description><![CDATA[Introduction of lambda expressions in .NET 3.5 have simplified many operations. One of them is sorting List&#38;lt;t&#38;gt; of complex types. For example assume that we have a type named Person and it has a Name property among other properties. Now if we want to sort a list of List&#38;lt;Person&#38;gt; on the Name property, we just [...]]]></description>
			<content:encoded><![CDATA[<p>Introduction of lambda expressions in .NET 3.5 have simplified many operations. One of them is sorting List&amp;lt;t&amp;gt; of complex types. For example assume that we have a type named Person and it has a Name property among other properties. Now if we want to sort a list of List&amp;lt;Person&amp;gt; on the Name property, we just have to use a single line of code.</p>
<p>list.Sort((p1,p2)=&gt;p1.Name.CompareTo(p2.Name));</p>
<p>Or to sort on the Age property</p>
<p>list.Sort((p1,p2)=&gt;p1.Age.CompareTo(p2.Age));</p>
<p>Here we are creating a delegate of Comparison&amp;lt;Person&amp;gt; using lambda expression and passing it to one of the overload of sort () method which accepts a Comparison&amp;lt;t&amp;gt;. The following code is breakdown of the above one liner into 2 lines.</p>
<p>Comparison&amp;lt;Person&amp;gt; pc=(p1,p2)=&gt;p1.Name.CompareTo(p2.Name);<br />
                                            list.Sort(pc);</p>
<p>In short, we can use lambda expression to create delegates inline and pass them where ever a delegate can be passed. The List&amp;lt;t&amp;gt; class itself has many methods where you can pass delegates to perform certain actions. For example List&amp;lt;t&amp;gt;.ForEach() method.</p>
<p>list.ForEach(o =&gt; Console.WriteLine(o.Name));</p>
<p>Here we are outputting the names of persons in the list to the console without using a foreach loop.</p>
<img src="http://feeds.feedburner.com/~r/ADeveloperFromIndia/~4/RWNxbe4vpGg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://nirandas.com/blog/index.php/sorting-list-the-easy-way-using-lambda-expressions/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Always explicitly detach explicitly attached event handlers</title>
		<link>http://nirandas.com/blog/index.php/always-explicitly-detach-explicitly-attached-event-handlers/</link>
		<comments>http://nirandas.com/blog/index.php/always-explicitly-detach-explicitly-attached-event-handlers/#comments</comments>
		<pubDate>Sat, 06 Dec 2008 16:16:12 +0000</pubDate>
		<dc:creator>Niran</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[VB.NET]]></category>

		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://nirandas.com/blog/?p=90</guid>
		<description><![CDATA[We extensively use events in our .net programs. Most of the times, it doesn’t give you debugging worries. However in some cases, it can become an issue where your object containing event handler doesn’t get garbage collected due to event raising object holding a reference to the listener object. This happens when you listen to [...]]]></description>
			<content:encoded><![CDATA[<p>We extensively use events in our .net programs. Most of the times, it doesn’t give you debugging worries. However in some cases, it can become an issue where your object containing event handler doesn’t get garbage collected due to event raising object holding a reference to the listener object. This happens when you listen to the events from a long lived object compared to the listening object.</p>
<p>If you are coding in VB.NET then using the handles clause will guard you to an extent. Just remember to set the withevents variable to nothing after done. This will remove bindings between your event handlers and the object.</p>
<p>However, if we are programming in C# or have to use AddHandler statements in VB.NET for attaching event handlers, remember to explicitly detach the handler from the event. </p>
<p>As I said earlier, most of the times this does not become an issue at all. However, while creating highly event driven classes for your application, you don’t want objects sticking around more than they need to be. Also since the handler is still attached to the event, the handler will get invoked each time the event is fired. You can think the issues arising when this happens.</p>
<p>Keep an eye on how you attach event handlers and make sure that you are detaching them in time.</p>
<img src="http://feeds.feedburner.com/~r/ADeveloperFromIndia/~4/VUc9LPRagiI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://nirandas.com/blog/index.php/always-explicitly-detach-explicitly-attached-event-handlers/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Is it OK to not have explicit return statements in functions?</title>
		<link>http://nirandas.com/blog/index.php/is-it-ok-to-not-have-explicit-return-statements-in-functionse/</link>
		<comments>http://nirandas.com/blog/index.php/is-it-ok-to-not-have-explicit-return-statements-in-functionse/#comments</comments>
		<pubDate>Wed, 12 Nov 2008 04:41:07 +0000</pubDate>
		<dc:creator>Niran</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[VB.NET]]></category>

		<guid isPermaLink="false">http://nirandas.com/blog/?p=86</guid>
		<description><![CDATA[Simple answer: no. There are basically two types of methods in .NET world. First those do not return any values (void methods in C# or subroutines in VB). Second those methods which do return some values (non void methods in C# and functions in VB)
So, failing to have a return statement where the calling code [...]]]></description>
			<content:encoded><![CDATA[<p>Simple answer: no. There are basically two types of methods in .NET world. First those do not return any values (void methods in C# or subroutines in VB). Second those methods which do return some values (non void methods in C# and functions in VB)</p>
<p>So, failing to have a return statement where the calling code expects one, should be reason enough to cause a compilation error. Not so in VB.NET 2005, at least not with the default configuration.</p>
<p>Couple of days ago I spend few minutes understanding this same issue. Yes, not explicitly returning any value from a function is a huge programming error. But it becomes even worse when you don’t get to know it until runtime.</p>
<p>The value returned to the caller when there isn’t any explicit return statement in the code is the default value for the return type of the function. Or in other words, the result will be as if you have returned nothing. For example, if you have following function in your code:</p>
<p>Private Function test() As Integer</p>
<p>End Function</p>
<p>Then somewhere else have this statement:</p>
<p>MessageBox.Show(test.ToString())</p>
<p>The program will compile normally and produce a message box with 0 (the default value of integer) in it. If we change the return type of the test method to string, the function will be returning nothing (return nothing) the default value for any object.</p>
<p>Does anyone know any way around this issue, any changes to configuration? So that VB.NET compiler becomes stricter? Yes, it isn’t such a problem. But still, the compiler should alert us to such silly mistakes which we may make time to time.</p>
<img src="http://feeds.feedburner.com/~r/ADeveloperFromIndia/~4/HtSHfJ_UXnE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://nirandas.com/blog/index.php/is-it-ok-to-not-have-explicit-return-statements-in-functionse/feed/</wfw:commentRss>
		</item>
		<item>
		<title>hide email and other sensitive information from spam harvesters</title>
		<link>http://nirandas.com/blog/index.php/hide-email-and-other-sensitive-information-from-spam-harvesters/</link>
		<comments>http://nirandas.com/blog/index.php/hide-email-and-other-sensitive-information-from-spam-harvesters/#comments</comments>
		<pubDate>Thu, 16 Oct 2008 16:28:10 +0000</pubDate>
		<dc:creator>Niran</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[asp.net]]></category>

		<category><![CDATA[javascript]]></category>

		<category><![CDATA[php]]></category>

		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://nirandas.com/blog/?p=82</guid>
		<description><![CDATA[
Often we have to display email addresses and other contact information on our pages. For example providing a mailto link etc. However, along with the intended users, some other persons/bots are also interested in getting those email IDs for sending spams. For this, spammers use special bots for harvesting email/contact info from the web. In [...]]]></description>
			<content:encoded><![CDATA[<p>
Often we have to display email addresses and other contact information on our pages. For example providing a mailto link etc. However, along with the intended users, some other persons/bots are also interested in getting those email IDs for sending spams. For this, spammers use special bots for harvesting email/contact info from the web. In this post, I will show a method using javascript and any server side language by which we can easily protect such important information.
</p>
<p><span id="more-82"></span></p>
<h3>example</h3>
<p>
The following c# code sample demonstrates the use of this code. The server side:
</p>
<blockquote><pre>
&lt;script type="text/javascript"&gt;
document.write(JsDecrypt(&lt;%=JsEncrypt("example@example.com")%&gt;));
&lt;/script&gt;
</pre>
</blockquote>
<p>It is how the code will look like on the client side</p>
<blockquote><pre>
document.write(JsDecrypt({value:"%u00c2%u0097%17%3aw%u00ea%u00cdu%0f1K%u00a2G%u008a%0d%u00e3%u00fb%02%02",key:93 }));
</pre>
</blockquote>
<p>
When this script gets executed on the client side, the email id &#8220;example@example.com&#8221; will be printed to the document. Naturally, for it to work, the JsDecrypt() function should also be included in the page.
</p>
<h3>How it works?</h3>
<p>
It is simple, the server side code encrypts the information and encodes it in JSON, javascript object notation, and sends it to the client. There our java script function JsDecrypt() decrypts the message and returns the original message. A special thing is that the key used is generated randomly while encrypting the message and is included in the JSON returned. The client side script uses that key for the decryption process. Hence, the same message would generate different outputs depending upon the randomly generated key.</p>
<p>Naturally the encryption method used is not the strongests however, it is more than enough for preventing the email spam bots. Also the code as it is, will only work with ascii characters (not tested with unicode).
</p>
<h3>code</h3>
<p>The code for this javascript decrypter has two parts. 1. The server side code for encrypting the data. The server side code can be in any language even in php. The only thing to remember is that the decrypter and encrypter version should be the same. 2. The client side javascript for decryption. The client code follows.
</p>
<blockquote><pre>
&lt;script type="text/javascript" language="javascript"&gt;
//version:1.0
//the version of the encryption method used should also be 1.0
function JsDecrypt(value)
{
var key=value.key;
value=unescape(value.value);
var output='';
for(i=0;i&lt;value.length;i++)
{
output= output+ String.fromCharCode(((value.charCodeAt(i)+256)-key)%256);
key=(key+value.charCodeAt(i))%256;
}
return output;
}
&lt;/script&gt;
</pre>
</blockquote>
<p>
The JsDecrypt() function takes an object with two properties. 1. The encrypted data. 2. The key used for encryption. Then the function decrypts the data using the key and returns the result. Note</p>
<blockquote><p>
The encryption function discussed below returns the encryption result as required by the JsDecrypt() function. You only need to pass it directly to the JsDecrypt() function for decrypting.
</p></blockquote>
<h3>The server side code</h3>
<p>
The server side code can be in any language as long as the generated result is as per the format required by the JsDecrypt() function. I have included code for asp.net (c#) and php. asp.net code follows.
</p>
<h4>asp.net version</h3>
<blockquote><pre>
//version:1.0
//the javascript decrypter version should also be 1.0
public string JsEncrypt(string data)
{
int Key=new Random().Next(0,256);
int StartingKey=Key;
string output=string.Empty;
for(int i=0;i&lt;data.Length;i++)
{
 output+=char.ConvertFromUtf32((char.ConvertToUtf32(data,i)+Key)%256);
Key =(Key +char.ConvertToUtf32(output,i))%256;
}
return string.Format("{{value:\"{0}\",key:{1} }}",System.Web.HttpUtility.UrlEncodeUnicode ( output),StartingKey);
}
</pre>
</blockquote>
<h4>php version</h4>
<blockquote><pre>
//version:1.0
//the javascript decrypter version should also be 1.0
function JsEncrypt($string)
{
$const=rand(0,255);
$key=$const;
$output='';
for($i=0;$i&lt;strlen($string);$i++)
{
$output.=chr((ord($string[$i])+$const)%256);
$const=(($const+ord($output[$i]))%256);
}
return '{value:"'.rawurlencode($output).'",key:'.$key.'}';
}
</pre>
</blockquote>
<img src="http://feeds.feedburner.com/~r/ADeveloperFromIndia/~4/4bVh_HHkui8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://nirandas.com/blog/index.php/hide-email-and-other-sensitive-information-from-spam-harvesters/feed/</wfw:commentRss>
		</item>
		<item>
		<title>creating GUIDs in .net</title>
		<link>http://nirandas.com/blog/index.php/creating-guids-in-net/</link>
		<comments>http://nirandas.com/blog/index.php/creating-guids-in-net/#comments</comments>
		<pubDate>Mon, 13 Oct 2008 12:58:09 +0000</pubDate>
		<dc:creator>Niran</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[VB.NET]]></category>

		<category><![CDATA[c#]]></category>

		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://nirandas.com/blog/?p=77</guid>
		<description><![CDATA[After an interval of more than 2 weeks, I am back online blogging. Today I will share a quick tip on how to create a guid in C# and vb.net. For a description about GUIDs, visit this Wikipedia page.
A guid in the .net world is represented by the System.Guid class. The Guid class has many [...]]]></description>
			<content:encoded><![CDATA[<p>After an interval of more than 2 weeks, I am back online blogging. Today I will share a quick tip on how to create a guid in C# and vb.net. For a description about GUIDs, visit this <a href="http://en.wikipedia.org/wiki/Globally_Unique_Identifier">Wikipedia page</a>.</p>
<p>A guid in the .net world is represented by the System.Guid class. The Guid class has many constructors which takes 0 or more arguments and constructs a guid from it. However, if we want to create a random guid, using the parameter less constructor would not do. A guid created using the parameter less constructor contains all zeros. So how to create a unique guid?</p>
<h3>Guid.NewGuid() method</h3>
<p>The Guid class has a static method named NewGuid() which creates and returns a new random guid. Let&#8217;s see a example.</p>
<dl>
<dt>C#</dt>
<dd>
<blockquote>
<pre>Guid g=Guid.NewGuid();
                             MessageBox.Show(g.ToString());</pre>
</blockquote>
</dd>
<dt>VB.NET</dt>
<dd>
<blockquote>
<pre>Dim g As Guid =Guid.NewGuid()
                             MessageBox.Show(g.ToString())</pre>
</blockquote>
</dd>
</dl>
<p>Here we are simply creating a new guid and using ToString() method for displaying it. You can also get byte representation of the guid using ToByteArray() method.</p>
<p>Enjoy!</p>
<img src="http://feeds.feedburner.com/~r/ADeveloperFromIndia/~4/_b75vrmPQGc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://nirandas.com/blog/index.php/creating-guids-in-net/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
