<?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>Anay Kamat's Weblog &#187; .net</title>
	<atom:link href="http://anaykamat.com/tag/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://anaykamat.com</link>
	<description>Technology, Programming, Career, Fun, Friends And Thoughts</description>
	<lastBuildDate>Wed, 25 Aug 2010 12:20:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Simple equivalent of &#8220;With&#8221; statement in C#</title>
		<link>http://anaykamat.com/2009/08/09/simple-equivalent-of-with-statement-in-c-sharp/</link>
		<comments>http://anaykamat.com/2009/08/09/simple-equivalent-of-with-statement-in-c-sharp/#comments</comments>
		<pubDate>Sun, 09 Aug 2009 16:06:29 +0000</pubDate>
		<dc:creator>Anay</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[DSL]]></category>
		<category><![CDATA[lambdas]]></category>

		<guid isPermaLink="false">http://anaykamat.com/?p=84</guid>
		<description><![CDATA[Consider the following class in C# public class Person { private string name; private int age; public string Name{ get {return name;} set { name = value; } } public int Age{ get {return age;} set { age = value; } } } If I want to set the value of Name and Age property [...]]]></description>
			<content:encoded><![CDATA[<p>Consider the following class in C#</p>
<pre class="brush: csharp;">
	public class Person
	{
		private string name;
		private int age;

		public string Name{
			get {return name;}
			set { name = value; }
		}

		public int Age{
			get {return age;}
			set { age = value; }
		}
	}
</pre>
<p>If I want to set the value of Name and Age property on the instance of Person class, I&#8217;ll need to refer to that instance for every property I need to set in the code. For example:</p>
<pre class="brush: csharp;">
var person = new Person();
person.Name = &quot;Super Man&quot;;
person.Age = 30;
</pre>
<p>It would have been great if C# had an equivalent of VB&#8217;s &#8220;With..End&#8221; statement, where we could refer to the instance of Person class only once and then refer to properties only.</p>
<p>Today, I came across this post &#8220;<a href="http://blog.bittercoder.com/PermaLink,guid,d1831805-dbf7-4b74-a6fd-2e9ed437c3d9.aspx" target="_blank">mucking about with hashes&#8230;</a>&#8220;, which shows how C# lambdas could be used as hashes. Using this concept, I implemented a simple extension method that simulates the behavior of VB&#8217;s &#8220;With..End&#8221; statement to some extent.</p>
<p>Here is the code for extension method:</p>
<pre class="brush: csharp;">
	public static class MetaExtensions
	{
		public static void Set(this object obj,params Func&lt;string,object&gt;[] hash){
				foreach(Func&lt;string,object&gt; member in hash){
					var propertyName = member.Method.GetParameters()[0].Name;
					var propertyValue = member(string.Empty);
					obj.GetType()
						.GetProperty(propertyName)
							.SetValue(obj,propertyValue,null);
				};
		}
	}
</pre>
<p>Using this extension method, we can set the value of properties on instance of Person class as follows:</p>
<pre class="brush: csharp;">
var person = new Person();
person.Set(
	Name =&gt; &quot;Super Man&quot;,
	Age =&gt; 30
);
</pre>
<p>Isn&#8217;t that cool?</p>
]]></content:encoded>
			<wfw:commentRss>http://anaykamat.com/2009/08/09/simple-equivalent-of-with-statement-in-c-sharp/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>C# 3.5 in Ubuntu</title>
		<link>http://anaykamat.com/2009/07/31/c-sharp-3-5-in-ubuntu/</link>
		<comments>http://anaykamat.com/2009/07/31/c-sharp-3-5-in-ubuntu/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 10:28:57 +0000</pubDate>
		<dc:creator>Anay</dc:creator>
				<category><![CDATA[Technologies]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mono]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://anaykamat.com/?p=59</guid>
		<description><![CDATA[Today, I managed to install Mono 2.4 in Ubuntu 9.04 without affecting Ubuntu&#8217;s default mono installation. Thanks to the instructions here (Building Mono 2.4 from source on Ubuntu 8.10), I was able to install latest mono/monodevelop in a parallel environment. Now I can work on C# 3.5 code in Linux as well. Here is the [...]]]></description>
			<content:encoded><![CDATA[<p>Today, I managed to install Mono 2.4 in Ubuntu 9.04 without affecting Ubuntu&#8217;s default mono installation. Thanks to the instructions <a href="http://www.centriment.com/2009/04/01/building-mono-24-from-source-on-ubuntu-810/" target="_blank">here (Building Mono 2.4 from source on Ubuntu 8.10)</a>, I was able to install latest mono/monodevelop in a parallel environment. Now I can work on C# 3.5 code in Linux as well.</p>
<p>Here is the screen shot of MonoDevelop instance with the code given in <a href="http://anaykamat.com/2009/04/08/y-combinator-in-csharp/" target="_blank">this post</a>.</p>
<p><a rel="attachment wp-att-60" href="http://anaykamat.com/2009/07/31/c-sharp-3-5-in-ubuntu/screenshot-functionaltest-monodevelop/"><img class="aligncenter size-medium wp-image-60" title="MonoDevelop running C# 3.5 code" src="http://anaykamat.com/wp-content/uploads/2009/07/Screenshot-FunctionalTest-MonoDevelop-300x163.png" alt="MonoDevelop running C# 3.5 code" width="300" height="163" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://anaykamat.com/2009/07/31/c-sharp-3-5-in-ubuntu/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Y-Combinator in C#</title>
		<link>http://anaykamat.com/2009/04/08/y-combinator-in-csharp/</link>
		<comments>http://anaykamat.com/2009/04/08/y-combinator-in-csharp/#comments</comments>
		<pubDate>Wed, 08 Apr 2009 14:19:26 +0000</pubDate>
		<dc:creator>Anay</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technologies]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[functional programming]]></category>

		<guid isPermaLink="false">http://anaykamat.com/?p=58</guid>
		<description><![CDATA[For last few days, I was trying to use lambda feature of C# to implement Y-Combinator. After few trial and errors, I was able to implement it in C# 3.5. I&#8217;m currently posting the code here and in my next blog, I&#8217;ll explain how I derived it. In this code, Y-Combinator function, is used to [...]]]></description>
			<content:encoded><![CDATA[<p>For last few days, I was trying to use lambda feature of C# to implement Y-Combinator. After few trial and errors, I was able to implement it in C# 3.5. I&#8217;m currently posting the code here and in my next blog, I&#8217;ll explain how I derived it.</p>
<p>In this code, Y-Combinator function, is used to implement anonymous recursive-factorial function called &#8216;factorial&#8217;.</p>
<pre class="brush: csharp;">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YCombinator
{
    class Program
    {
        delegate Func RecursiveFunction(RecursiveFunction f);

        static void Main(string[] args)
        {

            Func, Func&gt;, Func&gt; Y = (f) =&gt;
            {
                RecursiveFunction function = (h) =&gt;
                {
                    return (x) =&gt;
                    {
                        return f(h(h))(x);
                    };
                };
                return function(function);
            };

            Func factorial = Y(function=&gt;
            {
                return x =&gt;
                {
                    return x == 0 ? 1 : x * function(x - 1);
                };
            });
            Console.WriteLine(factorial(5));
            Console.ReadLine();
        }
    }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://anaykamat.com/2009/04/08/y-combinator-in-csharp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Binding HTML Form Fields To Javascript Objects</title>
		<link>http://anaykamat.com/2008/05/22/binding-html-form-fields-to-javascript-objects/</link>
		<comments>http://anaykamat.com/2008/05/22/binding-html-form-fields-to-javascript-objects/#comments</comments>
		<pubDate>Thu, 22 May 2008 15:42:58 +0000</pubDate>
		<dc:creator>Anay</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[data bindings]]></category>
		<category><![CDATA[databindings]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://anaykamat.com/?p=50</guid>
		<description><![CDATA[Consider that I have a object called &#8216;person&#8217; with a property called &#8216;Name&#8217; which returns me the name of a particular person. I want to assign this object property to a form element such that: The form element will display the value of object property (In our case, the value of person.Name) If I change [...]]]></description>
			<content:encoded><![CDATA[<p>Consider that I have a object called &#8216;person&#8217; with a property called &#8216;Name&#8217; which returns me the name of a particular person. I want to assign this object property to a form element such that:</p>
<ol>
<li>The form element will display the value of object property (In our case, the value of person.Name)</li>
<li>If I change the value in form, then it should automatically get assigned to the object property</li>
</ol>
<p>In C#, it could be done by using the following piece of code:</p>
<pre class="brush: csharp;">nameTextBox.DataBindings.Add(&quot;Text&quot;, person, &quot;Name&quot;);</pre>
<p>I really like the way DataBinding works and would really like have something like this when I&#8217;m working on web applications to bind a object property to input elements in HTML forms. So I decided to write a small javascript that would allow me to bind object properties to form elements. For example, I wanted to write something like this:</p>
<pre class="brush: xml;">&lt;input type=&quot;text&quot; object=&quot;person&quot; property=&quot;firstName&quot; /&gt;</pre>
<p>Thanks to the power of Javascript and prototype library, I was able to use data binding in html forms using code:</p>
<pre class="brush: jscript;">
function initializeFormBinding()
{
	var formElements = document.getElementsByTagName('input');
	$A(formElements).each(function(formElement)
				{
					Element.extend(formElement);
					initializeFormElement(formElement);
				});
}

function initializeFormElement(formElement)
{
	if (!(formElementHasObjectAttribute(formElement) &amp;amp;&amp;amp; formElementHasPropertyAttribute(formElement)))
		return;
	var objectName = getAttributeValue(formElement,'object');
	var propertyName = getAttributeValue(formElement,'property');
	window.eval('formElement.value = '+objectName+'.'+propertyName);
	window.eval('formElement.onchange=function(){ '+objectName+'.'+propertyName+' = formElement.value; }');
}

function formElementHasObjectAttribute(formElement)
{
	return getAttributeValue(formElement,'object')!=null;
}

function formElementHasPropertyAttribute(formElement)
{
	return getAttributeValue(formElement,'property')!=null;
}

function getAttributeValue(element, attributeName)
{
	return element.readAttribute(attributeName);
}
</pre>
<p>Lets see how to use this: First, create a javascript object. For example:</p>
<pre class="brush: jscript;">
var person = {};
person.firstName = '';
person.lastName = '';
person.age = '';
person.country = '';
</pre>
<p>After that call initializeFormBinding() function using onLoad attribute of body tag. For example:</p>
<pre class="brush: xml;">
&lt;body onLoad=&quot;javascript:initializeFormBinding();&quot;&gt;
</pre>
<p>Now, in each of your input elements, put the name of the object in &#8216;object&#8217; attribute and name of property in &#8216;property&#8217; attribute as follows:</p>
<pre class="brush: xml;">
&lt;form&gt;
First Name:
&lt;input type=&quot;text&quot; object=&quot;person&quot; property=&quot;firstName&quot; /&gt;&lt;br&gt;
Last Name:
&lt;input type=&quot;text&quot; object=&quot;person&quot; property=&quot;lastName&quot; /&gt;&lt;br&gt;
Age:
&lt;input type=&quot;text&quot; object=&quot;person&quot; property=&quot;age&quot; /&gt;&lt;br&gt;
Country:
&lt;input type=&quot;text&quot; object=&quot;person&quot; property=&quot;country&quot; /&gt;&lt;br&gt;
&lt;/form&gt;
</pre>
<p>Thats it&#8230;. If you initialize properties of person object, you would see those values in respective form elements. On the other hand, if you modify the value in a particular input element then it would get reflected in the corresponding object property. Anybody is free to use this code in their applications.</p>
<p><a title="Application example" href="http://www.anaykamat.com/fileuploads/JavascriptDataBinding.zip">Download a small example application.</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://anaykamat.com/2008/05/22/binding-html-form-fields-to-javascript-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
