<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Rob Garrett - Blog &#187; C#</title>
	<atom:link href="http://blog.robgarrett.com/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.robgarrett.com</link>
	<description>Software and Technology Tid-bits</description>
	<lastBuildDate>Fri, 03 Feb 2012 21:52:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blog.robgarrett.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/aab8ef1df0f0164f4c766c98b644e9fb?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Rob Garrett - Blog &#187; C#</title>
		<link>http://blog.robgarrett.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blog.robgarrett.com/osd.xml" title="Rob Garrett - Blog" />
	<atom:link rel='hub' href='http://blog.robgarrett.com/?pushpress=hub'/>
		<item>
		<title>.NET Wrapper for COM Elevation</title>
		<link>http://blog.robgarrett.com/2007/02/12/net-wrapper-for-com-elevation/</link>
		<comments>http://blog.robgarrett.com/2007/02/12/net-wrapper-for-com-elevation/#comments</comments>
		<pubDate>Mon, 12 Feb 2007 23:03:03 +0000</pubDate>
		<dc:creator>Rob Garrett</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Windows Vista]]></category>

		<guid isPermaLink="false">/cs/blogs/software/archive/2007/02/12/net-wrapper-for-com-elevation.aspx</guid>
		<description><![CDATA[Microsoft has ramped up the security in their latest operating system &#8211; Windows Vista, which means that developers now have to pay more attention to certain security constraints imposed by the operating system when developing applications.&#160;&#160; Those of you readers who have read my prior posts on User Access Control in Vista&#160;(and the further reading [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=2473&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Microsoft has ramped up the security in their latest operating system &#8211; Windows Vista, which means that developers now have to pay more attention to certain security constraints imposed by the operating system when developing applications.&nbsp;&nbsp; Those of you readers who have read my prior posts on <a href="http://robgarrett.com/cs/blogs/software/archive/2007/01/04/windows-vista-user-account-control.aspx" target="_blank">User Access Control in Vista</a>&nbsp;(and the <a href="http://robgarrett.com/cs/blogs/software/archive/2007/01/04/windows-vista-uac-further-reading.aspx" target="_blank">further reading</a> article), may be interested in this small software project I put together.&nbsp; </p>
<p><strong>The Problem in a Nutshell</strong></p>
<p>Vista requires that ALL users (even administrators) obtain an elevated security token before being permitted to execute protected API calls&nbsp;or accessing protected system resources (the registry for example).&nbsp; In prior versions of Windows, system administrators were given a privileged security token at login and permitted to perform any task without further confirmation from the interactive user.&nbsp; Windows Vista now allocates a low security token by default to all users and administrators, and an elevated token is only obtained with credentials (users) or confirmation (administrators) when required.</p>
<p>Elevation request is determined by Vista before any process is loaded &#8211; so if a particular process is running non-elevated then access to protected resources and APIs within that process is denied.&nbsp; This is an important caveat, as it affects the way in which&nbsp;software developers&nbsp;approach application design.</p>
<p><strong>How does one Elevate their Process?</strong></p>
<p>There are several articles on the Internet about developing elevated processes in Vista, so I&#8217;ll not dwell on the steps here (my previous articles cover this subject), but essentially&nbsp;elevation request&nbsp;involves decorating .NET assemblies and WIN32 executables with elevation status in the application manifest file (may be embedded or side-by-side).</p>
<p><a href="http://msdn2.microsoft.com/en-us/library/ms679687.aspx" target="_blank">The COM Elevation Moniker</a> is another method for obtaining elevation.&nbsp; Using the CEM it is possible to host a COM (Component Object Model) component in elevated state using&nbsp;the dedicated system process &#8211; DLLHost.exe.&nbsp; <a href="http://www.microsoft.com/downloads/details.aspx?familyid=c2b1e300-f358-4523-b479-f53d234cdccf&amp;displaylang=en" target="_blank">The Windows Vista SDK</a> provides details&nbsp;on adding the necessary code to your&nbsp;project to elevate an out-of-proc&nbsp;component and making it available to your non-elevated application.</p>
<p>So why use the COM Elevation Moniker?</p>
<p>The CEM permits a non-elevated process to execute protected API calls and access protected resources by calling across process boundaries into an elevated process.&nbsp; This is how Vista achieves mid-application elevation &#8211; look for the shield symbols in forms and property pages, to get an idea of what I am talking about.&nbsp; </p>
<p><strong>So what&#8217;s the big deal?</strong></p>
<p>Development of a CEM&nbsp;component typically involves some work in C++.&nbsp; Development of COM components in VB and .NET is of course possible, but when it comes down to accessing WIN32 calls to perform COM elevation most of the examples are published in C++.&nbsp; Developing a managed CEM is possible but involves some work to get it operating correctly.</p>
<p>What about cross boundary communication? &#8211; This is the klutz of the problem with authoring CEM components, how to communicate across process boundaries?&nbsp; COM development answered this problem a long time ago, and the same is true now as it was then &#8211; marshalling data and cross process communication is handled by the COM infrastructure.&nbsp; But what about managed code?&nbsp; If you&#8217;ve managed to solve the problem of invoking a .NET assembly as a COM CEM component, you still have to communicate over .NET interop, which may not be ideal.</p>
<p>Surely someone must have a way to perform elevation in the managed world?</p>
<p><strong>The .NET Wrapper for COM Elevation</strong></p>
<p>I wanted a way for managed developers to attribute their code classes in such away that embedded methods of these classes would request elevation from the Vista operating system before execution of such methods.&nbsp; So I went about developing a library to encapsulate the complexities of the COM Elevation Moniker.&nbsp; The end result is available to download from <a href="http://robgarrett.com/blogs/downloads/elevation.zip">here</a>.</p>
<p>After downloading the ZIP files and unpacking it, the library is packaged as an MSI installer file, which must be installed as an administrator to make use of the library functionality.&nbsp; The installer also installs some example code to demonstrate use of the library.</p>
<p><strong>How does the Managed Wrapper Library Work?</strong></p>
<p>Essentially, the wrapper library exists as a managed and non-managed server.&nbsp; The non-managed COM server provides the infrastructure to elevate managed code by hosting it&#8217;s own .NET AppDomain.&nbsp; The managed&nbsp;server assembly&nbsp;is loaded into the elevated AppDomain and executes code developed by the end user of my library.</p>
<p>Communication between the hosted managed server and client code is achieved using an IPC (Inter-Process Communication) channel developed against WCF (Windows Communication Foundation).&nbsp; The exact specifics of this set up is beyond the scope of this blog post, but involves the use of CodeDOM (dynamic code generation) to create a dynamic messaging system between elevated code and non-elevated client proxy.&nbsp; </p>
<p>My wrapper library is very much &#8220;beta&#8221; at present.&nbsp; I&#8217;ve published my library in the hope that I can attract a number of developers to use it and let me know their initial thoughts.&nbsp; Documentation is sparse, but I am working on publishing a technical manual. It is important to understand that end-users of my library require no knowledge of WCF development, C++, or COM expertise, which makes my library versatile and easy to use by managed developers of different skill-level.</p>
<p>For now, I&#8217;ll end this post with a thank-you &#8211; for taking an interest in my project.&nbsp; I look forward to receiving feedback (good and bad), especially any pertaining to security aspects.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/robgarrett.wordpress.com/2473/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/robgarrett.wordpress.com/2473/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/robgarrett.wordpress.com/2473/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/robgarrett.wordpress.com/2473/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/robgarrett.wordpress.com/2473/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/robgarrett.wordpress.com/2473/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/robgarrett.wordpress.com/2473/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/robgarrett.wordpress.com/2473/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/robgarrett.wordpress.com/2473/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/robgarrett.wordpress.com/2473/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/robgarrett.wordpress.com/2473/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/robgarrett.wordpress.com/2473/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/robgarrett.wordpress.com/2473/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/robgarrett.wordpress.com/2473/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/robgarrett.wordpress.com/2473/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/robgarrett.wordpress.com/2473/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=2473&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.robgarrett.com/2007/02/12/net-wrapper-for-com-elevation/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Rob Garrett</media:title>
		</media:content>
	</item>
		<item>
		<title>Pass by Reference</title>
		<link>http://blog.robgarrett.com/2006/10/25/pass-by-reference/</link>
		<comments>http://blog.robgarrett.com/2006/10/25/pass-by-reference/#comments</comments>
		<pubDate>Wed, 25 Oct 2006 09:17:49 +0000</pubDate>
		<dc:creator>Rob Garrett</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">/cs/blogs/software/archive/2006/10/25/Pass-by-Reference.aspx</guid>
		<description><![CDATA[Contrary to the beliefs of some developers, .Net reference types do not need to be passed as reference parameters (ref in C#, ByRef in VB) when a method is going to alter the contents of an object instance. Passing a object reference by reference parameter just means the method can reassign the reference to a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=2301&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Contrary to the beliefs of some developers, .Net reference types do not need to be passed as reference parameters (ref in C#, ByRef in VB) when a method is going to alter the contents of an object instance.
<p />Passing a object reference by reference parameter just means the method can reassign the reference to a new object  instance. The following reference parameter shown  below is unnecessary:
<p />class foo<br />{<br />public int i;<br />}
<p />&#8230; void fooMethod(ref foo fooInst)<br />{<br />fooInst.i = 10;<br />}
<p />.Net is smart enough to pass object instances using address &#8211; not the whole object across the stack. In C++ land, the parameter should be passed as a pointer or explicit reference (using &amp;).</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/robgarrett.wordpress.com/2301/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/robgarrett.wordpress.com/2301/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/robgarrett.wordpress.com/2301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/robgarrett.wordpress.com/2301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/robgarrett.wordpress.com/2301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/robgarrett.wordpress.com/2301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/robgarrett.wordpress.com/2301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/robgarrett.wordpress.com/2301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/robgarrett.wordpress.com/2301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/robgarrett.wordpress.com/2301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/robgarrett.wordpress.com/2301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/robgarrett.wordpress.com/2301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/robgarrett.wordpress.com/2301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/robgarrett.wordpress.com/2301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/robgarrett.wordpress.com/2301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/robgarrett.wordpress.com/2301/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=2301&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.robgarrett.com/2006/10/25/pass-by-reference/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Rob Garrett</media:title>
		</media:content>
	</item>
		<item>
		<title>C# Value Types and Ranges</title>
		<link>http://blog.robgarrett.com/2006/06/01/c-value-types-and-ranges/</link>
		<comments>http://blog.robgarrett.com/2006/06/01/c-value-types-and-ranges/#comments</comments>
		<pubDate>Fri, 02 Jun 2006 00:59:00 +0000</pubDate>
		<dc:creator>Rob Garrett</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">/cs/blogs/software/archive/2006/06/01/2005.aspx</guid>
		<description><![CDATA[Just because it&#8217;s good to know&#8230;.. Keyword Class Range bool System.Boolean true and false byte System.Byte 0 to 255 sbyte System.SByte -128 to 127 short System.Int16 -32768 to 32767 ushort System.Uint16 0 to 65535 int System.Int32 -2,147,483,648 to 2,147,483,647 uint System.UInt32 0 to 4,294,967,295 long System.Int64 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 ulong System.UInt64 0 to 18,446,744,073,709,551,615 decimal [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=2005&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Just because it&#8217;s good to know&#8230;..</p>
<table class="MsoNormalTable" style="border:1pt outset rgb(240,204,0);width:100%;" border="1" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td style="border:1pt inset rgb(240,204,0);width:10%;padding:4.5pt;" width="10%">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><b><span style="font-size:12pt;font-family:&quot;color:rgb(0,0,204);">Keyword</span></b><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);width:15%;padding:4.5pt;" width="15%">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><b><span style="font-size:12pt;font-family:&quot;color:rgb(0,102,0);">Class</span></b><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);width:75%;padding:4.5pt;" width="75%">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><b><span style="font-size:12pt;font-family:&quot;">Range</span></b><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
</tr>
<tr>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><span style="font-size:12pt;font-family:&quot;color:rgb(0,0,204);">bool</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;color:rgb(0,102,0);">System.Boolean</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;">true and false </span></p>
</td>
</tr>
<tr>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><span style="font-size:12pt;font-family:&quot;color:rgb(0,0,204);">byte</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;color:rgb(0,102,0);">System.Byte</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;">0 to 255 </span></p>
</td>
</tr>
<tr>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><span style="font-size:12pt;font-family:&quot;color:rgb(0,0,204);">sbyte</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;color:rgb(0,102,0);">System.SByte</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;">-128 to 127 </span></p>
</td>
</tr>
<tr>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><span style="font-size:12pt;font-family:&quot;color:rgb(0,0,204);">short</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;color:rgb(0,102,0);">System.Int16</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;">-32768 to 32767 </span></p>
</td>
</tr>
<tr>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><span style="font-size:12pt;font-family:&quot;color:rgb(0,0,204);">ushort</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;color:rgb(0,102,0);">System.Uint16</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;">0 to 65535</span></p>
</td>
</tr>
<tr>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><span style="font-size:12pt;font-family:&quot;color:rgb(0,0,204);">int</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;color:rgb(0,102,0);">System.Int32</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;">-2,147,483,648 to 2,147,483,647</span></p>
</td>
</tr>
<tr>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><span style="font-size:12pt;font-family:&quot;color:rgb(0,0,204);">uint</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;color:rgb(0,102,0);">System.UInt32</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;">0 to 4,294,967,295</span></p>
</td>
</tr>
<tr>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><span style="font-size:12pt;font-family:&quot;color:rgb(0,0,204);">long</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;color:rgb(0,102,0);">System.Int64</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;">-9,223,372,036,854,775,808 to<br />
    9,223,372,036,854,775,807</span></p>
</td>
</tr>
<tr>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><span style="font-size:12pt;font-family:&quot;color:rgb(0,0,204);">ulong</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;color:rgb(0,102,0);">System.UInt64</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;">0 to 18,446,744,073,709,551,615</span></p>
</td>
</tr>
<tr>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><span style="font-size:12pt;font-family:&quot;color:rgb(0,0,204);">decimal</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;color:rgb(0,102,0);">System.Decimal</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;">-79,228,162,514,264,337,593,543,950,335<br />
    to 79,228,162,514,264,337,593,543,950,335</span></p>
</td>
</tr>
<tr>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><span style="font-size:12pt;font-family:&quot;color:rgb(0,0,204);">double</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;color:rgb(0,102,0);">System.Double</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;">-1.79769313486232e308 to<br />
    1.79769313486232e308</span></p>
</td>
</tr>
<tr>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><span style="font-size:12pt;font-family:&quot;color:rgb(0,0,204);">float</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;color:rgb(0,102,0);">System.Single</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;">-3.402823e38 to 3.402823e38</span></p>
</td>
</tr>
<tr>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;text-align:center;line-height:normal;" align="center"><span style="font-size:12pt;font-family:&quot;color:rgb(0,0,204);">char</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;color:rgb(0,102,0);">System.Char</span><span style="font-size:12pt;font-family:&quot;"></span></p>
</td>
<td style="border:1pt inset rgb(240,204,0);padding:4.5pt;">
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:12pt;font-family:&quot;">0 to 65535</span></p>
</td>
</tr>
</tbody>
</table>
<p></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/robgarrett.wordpress.com/2005/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/robgarrett.wordpress.com/2005/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/robgarrett.wordpress.com/2005/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/robgarrett.wordpress.com/2005/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/robgarrett.wordpress.com/2005/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/robgarrett.wordpress.com/2005/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/robgarrett.wordpress.com/2005/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/robgarrett.wordpress.com/2005/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/robgarrett.wordpress.com/2005/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/robgarrett.wordpress.com/2005/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/robgarrett.wordpress.com/2005/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/robgarrett.wordpress.com/2005/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/robgarrett.wordpress.com/2005/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/robgarrett.wordpress.com/2005/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/robgarrett.wordpress.com/2005/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/robgarrett.wordpress.com/2005/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=2005&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.robgarrett.com/2006/06/01/c-value-types-and-ranges/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Rob Garrett</media:title>
		</media:content>
	</item>
		<item>
		<title>Access modifiers on property get/set</title>
		<link>http://blog.robgarrett.com/2006/02/25/access-modifiers-on-property-getset/</link>
		<comments>http://blog.robgarrett.com/2006/02/25/access-modifiers-on-property-getset/#comments</comments>
		<pubDate>Sun, 26 Feb 2006 01:41:00 +0000</pubDate>
		<dc:creator>Rob Garrett</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">/cs/blogs/software/archive/2006/02/25/1866.aspx</guid>
		<description><![CDATA[To some I may be stating the obvious, but today I was happy to find out that C# lets you set a different access level on the get and set for a property. The example below will help to illustrate what I&#8217;m talking about: public DateTime UpdateDate { get { object data = ViewState["UpdateDate"]; return [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1866&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>To some I may be stating the obvious, but today I was happy to find out that C# lets you set a different access level on the get and set for a property. The example below will help to illustrate what I&#8217;m talking about:</p>
<p>public DateTime UpdateDate<br />
{<br />
    get<br />
    {<br />
        object data = ViewState["UpdateDate"];<br />
        return (null == data ? DateTime.Now : (DateTime)data);<br />
    }<br />
    private set<br />
    {<br />
        ViewState["UpdateDate"] = value;<br />
    }<br />
}<br />
<br />
Why on Earth would you want to do this? Well, I found a perfect situation where I needed the above code. I wanted to expose a property that is essentially read only to any callers that use it, but writable to the encapsulating class. In the above case, I do not use a private field to store the property value, but the ViewState instead (property is part of a server control class), the private set allows the class to write a value to the ViewState for the property.&nbsp; Now, I could have written to the ViewState directly from my class and done away with the property set altogether, but this would have resulted in many statements throughout my class code.&nbsp; This way, if I want to change the logic in how my property value is persisted I can make the necessary changes in one spot.&nbsp; The alternative is to use a private set function, but a private property set is so much nicer. :)</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/robgarrett.wordpress.com/1866/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/robgarrett.wordpress.com/1866/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/robgarrett.wordpress.com/1866/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/robgarrett.wordpress.com/1866/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/robgarrett.wordpress.com/1866/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/robgarrett.wordpress.com/1866/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/robgarrett.wordpress.com/1866/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/robgarrett.wordpress.com/1866/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/robgarrett.wordpress.com/1866/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/robgarrett.wordpress.com/1866/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/robgarrett.wordpress.com/1866/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/robgarrett.wordpress.com/1866/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/robgarrett.wordpress.com/1866/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/robgarrett.wordpress.com/1866/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/robgarrett.wordpress.com/1866/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/robgarrett.wordpress.com/1866/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1866&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.robgarrett.com/2006/02/25/access-modifiers-on-property-getset/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Rob Garrett</media:title>
		</media:content>
	</item>
		<item>
		<title>From C++ to C#</title>
		<link>http://blog.robgarrett.com/2005/11/28/from-c-to-c/</link>
		<comments>http://blog.robgarrett.com/2005/11/28/from-c-to-c/#comments</comments>
		<pubDate>Tue, 29 Nov 2005 02:04:00 +0000</pubDate>
		<dc:creator>Rob Garrett</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">/cs/blogs/software/archive/2005/11/28/1726.aspx</guid>
		<description><![CDATA[I have gone back in time this week to work on a legacy ISAPI application.&#160; The ISAPI application is being developed in C++ and makes use of MSXML to read an XML configuration file.&#160; Below is some code in C++ to open the XML file and read two text values, contained in element nodes: BOOL [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1726&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I have gone back in time this week to work on a legacy <acronym title="Internet Server API">ISAPI</acronym><br />
application.&nbsp; The ISAPI application is being developed in C++ and<br />
makes use of <acronym title="Microsoft XML">MSXML</acronym> to read an XML configuration file.&nbsp; Below is<br />
some code in C++ to open the XML file and read two text values,<br />
contained in element nodes:</p>
<p>BOOL GetConfigDetails(string &amp;userName, string &amp;domain)<br />
{<br />
	BOOL result = FALSE;<br />
	if (0 != configFilename &amp;&amp; _tcslen(configFilename) &gt; 0)<br />
	{<br />
		try<br />
		{<br />
			// Create a DOM Document.<br />
			// CComPtr smart pointer will release our COM handle.<br />
			CComPtr pItfcDoc = 0;<br />
			HRESULT hr = pItfcDoc.CoCreateInstance(__uuidof(MSXML2::DOMDocument60));<br />
			if (SUCCEEDED(hr))<br />
			{<br />
				// Load the config file.<br />
				if (VARIANT_TRUE == pItfcDoc-&gt;load(configFilename))<br />
				{<br />
					// Look for the username and domain nodes.<br />
					CComPtr pItfcNode = 0;<br />
					pItfcNode = pItfcDoc-&gt;selectSingleNode(_bstr_t(&#8220;/ISAPIStub/username&#8221;));<br />
					userName = 0 != pItfcNode ? pItfcNode-&gt;Gettext() : _bstr_t(&#8220;&#8221;);<br />
					pItfcNode = pItfcDoc-&gt;selectSingleNode(_bstr_t(&#8220;/ISAPIStub/domain&#8221;));<br />
					domain = 0 != pItfcNode ? pItfcNode-&gt;Gettext() : _bstr_t(&#8220;&#8221;);<br />
					result = TRUE;<br />
				}<br />
			}<br />
		}<br />
		catch (_com_error)<br />
		{<br />
			result = FALSE;<br />
		}<br />
	}<br />
	return result;<br />
}</p>
<p>
Now, here is the code in C#:</p>
<p>public bool GetConfigDetails(out string userName, out string domain)<br />
{<br />
    bool result = false;<br />
    userName = domain = String.Empty;<br />
    if (!String.IsNullOrEmpty(configFilename))<br />
    {<br />
        try<br />
        {<br />
            XmlDocument doc = new XmlDocument();<br />
            doc.Load(configFilename);<br />
            XmlNode node = doc.SelectSingleNode(&#8220;/ISAPIStub/username&#8221;);<br />
            userName = (null != node) ? node.InnerText : String.Empty;<br />
            node = doc.SelectSingleNode(&#8220;/ISAPIStub/domain&#8221;);<br />
            domain = (null != node) ? node.InnerText : String.Empty;<br />
        }<br />
        catch<br />
        {<br />
            result = false;<br />
        }<br />
    }<br />
    return result;<br />
}</p>
<p>
Much easier huh?&nbsp; See how far we&#8217;ve come as developers.&nbsp;<br />
Whilst writing my ISAPI application I had to remember to go back and<br />
clean up my memory allocations because there is no friendly garbage<br />
collector doing it for me &#8211; doh.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/robgarrett.wordpress.com/1726/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/robgarrett.wordpress.com/1726/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/robgarrett.wordpress.com/1726/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/robgarrett.wordpress.com/1726/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/robgarrett.wordpress.com/1726/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/robgarrett.wordpress.com/1726/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/robgarrett.wordpress.com/1726/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/robgarrett.wordpress.com/1726/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/robgarrett.wordpress.com/1726/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/robgarrett.wordpress.com/1726/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/robgarrett.wordpress.com/1726/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/robgarrett.wordpress.com/1726/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/robgarrett.wordpress.com/1726/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/robgarrett.wordpress.com/1726/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/robgarrett.wordpress.com/1726/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/robgarrett.wordpress.com/1726/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1726&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.robgarrett.com/2005/11/28/from-c-to-c/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Rob Garrett</media:title>
		</media:content>
	</item>
		<item>
		<title>Multicast Delegate Events as Properties</title>
		<link>http://blog.robgarrett.com/2005/10/25/multicast-delegate-events-as-properties/</link>
		<comments>http://blog.robgarrett.com/2005/10/25/multicast-delegate-events-as-properties/#comments</comments>
		<pubDate>Wed, 26 Oct 2005 02:37:00 +0000</pubDate>
		<dc:creator>Rob Garrett</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">/cs/blogs/software/archive/2005/10/25/1682.aspx</guid>
		<description><![CDATA[The saying goes &#8220;you learn something new every day&#8221;, and today is no exception.&#160; I&#8217;m not sure how I missed this, but never less, this post shows how to gain finer control over adding and removing of event handlers to public multicast delegate events. Typically, most C# developers are used writing event code as follows: [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1682&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The saying goes &#8220;you learn something new every day&#8221;, and today is no<br />
exception.&nbsp; I&#8217;m not sure how I missed this, but never less, this<br />
post shows how to gain finer control over adding and removing of event<br />
handlers to public multicast delegate events.</p>
<p>Typically, most C# developers are used writing event code as follows:</p>
<p>using System;<br />
public delegate void MyDelegate();   // delegate declaration</p>
<p>public interface I<br />
{<br />
   event MyDelegate MyEvent;<br />
   void FireAway();<br />
}</p>
<p>public class MyClass: I<br />
{<br />
   public event MyDelegate MyEvent;</p>
<p>   public void FireAway()<br />
   {<br />
      if (MyEvent != null)<br />
         MyEvent();<br />
   }<br />
}</p>
<p>public class MainClass<br />
{<br />
   static private void f()<br />
   {<br />
      Console.WriteLine(&#8220;This is called when the event fires.&#8221;);<br />
   }</p>
<p>   static public void Main ()<br />
   {<br />
      I i = new MyClass();</p>
<p>      i.MyEvent += new MyDelegate(f);<br />
      i.FireAway();<br />
   }<br />
}</p>
<p>
The above code exposes a public multicast delegate event, which client callers subscribe to by adding handlers using the <b>+=</b><br />
operator.&nbsp; However, it is possible to define a multicast delegate<br />
event as a property, where the developer can control the adding and<br />
removal of event handlers with add and remove accessor<br />
decelerations.&nbsp; </p>
<p>The example below defines properties for each event, and the accessor<br />
decelerations on each property manages the storage of event handlers<br />
using a private hash table. This approach is usually undertaken when a<br />
class defines many events, where the developer expects most of the<br />
events to be unimplemented, thus saving some overhead in creating<br />
multicast delegate event instances that are never assigned a handler.</p>
<p>using System;<br />
using System.Collections;</p>
<p>public delegate void MyDelegate1(int i);<br />
public delegate void MyDelegate2(string s);<br />
public delegate void MyDelegate3(int i, object o);<br />
public delegate void MyDelegate4();</p>
<p>public class PropertyEventsSample<br />
{<br />
   private Hashtable eventTable = new Hashtable();</p>
<p>   public event MyDelegate1 Event1<br />
   {<br />
      add<br />
      {<br />
         eventTable["Event1"] = (MyDelegate1)eventTable["Event1"] + value;<br />
      }<br />
      remove<br />
      {<br />
         eventTable["Event1"] = (MyDelegate1)eventTable["Event1"] &#8211; value;<br />
      }<br />
   }</p>
<p>   public event MyDelegate1 Event2<br />
   {<br />
      add<br />
      {<br />
         eventTable["Event2"] = (MyDelegate1)eventTable["Event2"] + value;<br />
      }<br />
      remove<br />
      {<br />
         eventTable["Event2"] = (MyDelegate1)eventTable["Event2"] &#8211; value;<br />
      }<br />
   }</p>
<p>   public event MyDelegate2 Event3<br />
   {<br />
      add<br />
      {<br />
         eventTable["Event3"] = (MyDelegate2)eventTable["Event3"] + value;<br />
      }<br />
      remove<br />
      {<br />
         eventTable["Event3"] = (MyDelegate2)eventTable["Event3"] &#8211; value;<br />
      }<br />
   }</p>
<p>   public event MyDelegate3 Event4<br />
   {<br />
      add<br />
      {<br />
         eventTable["Event4"] = (MyDelegate3)eventTable["Event4"] + value;<br />
      }<br />
      remove<br />
      {<br />
         eventTable["Event4"] = (MyDelegate3)eventTable["Event4"] &#8211; value;<br />
      }<br />
   }</p>
<p>   public event MyDelegate3 Event5<br />
   {<br />
      add<br />
      {<br />
         eventTable["Event5"] = (MyDelegate3)eventTable["Event5"] + value;<br />
      }<br />
      remove<br />
      {<br />
         eventTable["Event5"] = (MyDelegate3)eventTable["Event5"] &#8211; value;<br />
      }<br />
   }</p>
<p>   public event MyDelegate4 Event6<br />
   {<br />
      add<br />
      {<br />
         eventTable["Event6"] = (MyDelegate4)eventTable["Event6"] + value;<br />
      }<br />
      remove<br />
      {<br />
         eventTable["Event6"] = (MyDelegate4)eventTable["Event6"] &#8211; value;<br />
      }<br />
   }<br />
}</p>
<p></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/robgarrett.wordpress.com/1682/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/robgarrett.wordpress.com/1682/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/robgarrett.wordpress.com/1682/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/robgarrett.wordpress.com/1682/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/robgarrett.wordpress.com/1682/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/robgarrett.wordpress.com/1682/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/robgarrett.wordpress.com/1682/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/robgarrett.wordpress.com/1682/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/robgarrett.wordpress.com/1682/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/robgarrett.wordpress.com/1682/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/robgarrett.wordpress.com/1682/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/robgarrett.wordpress.com/1682/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/robgarrett.wordpress.com/1682/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/robgarrett.wordpress.com/1682/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/robgarrett.wordpress.com/1682/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/robgarrett.wordpress.com/1682/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1682&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.robgarrett.com/2005/10/25/multicast-delegate-events-as-properties/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Rob Garrett</media:title>
		</media:content>
	</item>
		<item>
		<title>C# 2.0 and the &#8216;yield&#8217; keyword</title>
		<link>http://blog.robgarrett.com/2005/09/13/c-2-0-and-the-yield-keyword/</link>
		<comments>http://blog.robgarrett.com/2005/09/13/c-2-0-and-the-yield-keyword/#comments</comments>
		<pubDate>Tue, 13 Sep 2005 19:29:00 +0000</pubDate>
		<dc:creator>Rob Garrett</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">/cs/blogs/software/archive/2005/09/13/1588.aspx</guid>
		<description><![CDATA[Another nice addition to C# 2.0 is the yield keyword. Yield enables iterator blocks to provide values to an enumerated result, see the following example. // yield-example.cs using System; using System.Collections; public class List { public static IEnumerable Power(int number, int exponent) { int counter = 0; int result = 1; while(counter++ &#60; exponent) { [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1588&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Another nice addition to C# 2.0 is the <span style="font-weight:bold;">yield</span> keyword. Yield enables iterator blocks to provide values to an enumerated result, see the following example. </p>
<p>// yield-example.cs<br />
using System;<br />
using System.Collections;<br />
public class List<br />
{<br />
    public static IEnumerable Power(int number, int exponent)<br />
    {<br />
        int counter = 0;<br />
        int result = 1;<br />
        while(counter++ &lt; exponent)<br />
        {<br />
            result = result * number;<br />
            yield return result;<br />
        }<br />
    }</p>
<p>    static void Main()<br />
    {<br />
        // Display powers of 2 up to the exponent 8:<br />
        foreach(int i in Power(2, 8))<br />
            Console.Write(&#8220;{0} &#8220;, i);<br />
    }<br />
}</p>
<p>
What does this mean in English? Essentially, iterator blocks can<br />
implicitly create an array of results, which supports IEnumerable,<br />
without having to declare an array data type and populating it.</p>
<p>The following line will signal the end of the iteration.</p>
<p>yield break;<br /></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/robgarrett.wordpress.com/1588/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/robgarrett.wordpress.com/1588/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/robgarrett.wordpress.com/1588/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/robgarrett.wordpress.com/1588/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/robgarrett.wordpress.com/1588/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/robgarrett.wordpress.com/1588/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/robgarrett.wordpress.com/1588/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/robgarrett.wordpress.com/1588/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/robgarrett.wordpress.com/1588/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/robgarrett.wordpress.com/1588/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/robgarrett.wordpress.com/1588/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/robgarrett.wordpress.com/1588/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/robgarrett.wordpress.com/1588/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/robgarrett.wordpress.com/1588/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/robgarrett.wordpress.com/1588/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/robgarrett.wordpress.com/1588/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1588&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.robgarrett.com/2005/09/13/c-2-0-and-the-yield-keyword/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Rob Garrett</media:title>
		</media:content>
	</item>
		<item>
		<title>Nullable Types in C# 2.0</title>
		<link>http://blog.robgarrett.com/2005/08/30/nullable-types-in-c-2-0/</link>
		<comments>http://blog.robgarrett.com/2005/08/30/nullable-types-in-c-2-0/#comments</comments>
		<pubDate>Tue, 30 Aug 2005 19:15:00 +0000</pubDate>
		<dc:creator>Rob Garrett</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">/cs/blogs/software/archive/2005/08/30/1542.aspx</guid>
		<description><![CDATA[I was reading about Nullable Types in the C# 2.0 specification for the .NET Framework 2.0 today. I am surprised I didn&#8217;t find out about this feature long ago, goes to show that you learn something new every day. So what are Nullable Types? Simply put, Nullable Types are value types that a can be [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1542&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I was reading about <a href="http://msdn2.microsoft.com/library/2cf62fcy%28en-us,vs.80%29.aspx">Nullable Types</a> in the <a href="http://download.microsoft.com/download/8/1/6/81682478-4018-48fe-9e5e-f87a44af3db9/CSharp%202.0%20Specification.doc">C# 2.0 specification</a> for the<br />
.NET Framework 2.0 today. I am surprised I didn&#8217;t find out about this<br />
feature long ago, goes to show that you learn something new every day.</p>
<p>So what are Nullable Types?</p>
<p>Simply put, Nullable Types are value types that a can be assigned null. The following line of C# will give you a compiler error:</p>
<p>int y = null;</p>
<p>A System.Int32 is not a reference type (it is a value type), and can<br />
therefore not be assigned a null value, however, the next line of code<br />
will compile under the 2.0 framework:</p>
<p>int? y = null;</p>
<p>Adding a question mark after the type turns the System.Int32 value type<br />
into a Nullable Type. The line above, is in fact, shorthand for the line<br />
below:</p>
<p>System.Nullable&lt;int&gt; y = null;</p>
<p>The <b>System.Nullable&lt;T&gt;</b> generic class is used to create value type variables that contain an undefined state. The <b>HasValue</b> property of this class will return true if the contained variable has an assigned value, or false if it null.&nbsp; The <b>Value</b> property will return the value of the property, or throw a <b>System.InvalidOperationException</b> if the contained variable is unassigned.</p>
<p>Why use Nullable Types?</p>
<p>How many times have you defined an integer in your code, and assigned it to -1 in the constructor (or definition) as meaning <i>undefined</i>?&nbsp;<br />
What happens of your code needs to change to allow negative numbers,<br />
but you still need to determine if a value has been assigned to your<br />
variable?&nbsp; With Nullable Types, you can assign null to your<br />
variable and test to see if the variable has been assigned at runtime.</p>
<p>I am presently writing a piece of code that uses reflection to get a<br />
list of properties in a class, and then output an XML tag for each<br />
property if the property value is not null.&nbsp; Using Nullable<br />
Types, I do not have to worry about value types outputting if their<br />
value has not been assigned.</p>
<p>Casting from Nullable Type to value type.&nbsp; The following lines of code will fail compilation</p>
<p>int? y = null;<br />
int x = y;</p>
<p>The following will compile, but throw an exception if y is null:</p>
<p>int? y = null;<br />
int x = (int)y;</p>
<p>The following will also compile, but throw an exception if y is null:</p>
<p>int? y = null;<br />
int x = y.Value;</p>
<p>What is needed in the above examples, is an operator that will assign a<br />
default value if the nullable variable type is unassigned.&nbsp; It<br />
just so happens that one exists. The following line will assign -1 to x<br />
if y is null:</p>
<p>int? y = null;<br />
int x = y ?? -1;</p>
<p>Nullable types work fine with most of the basic operators, <b>bool&amp;</b> is an interesting one.&nbsp; The table below defines how logical and and logical or work with two boolean nullable types:</p>
<p>bool? x, y;</p>
<table border="1">
<tbody>
<tr>
<th>
<p>x</p>
</th>
<th>
<p>y</p>
</th>
<th>
<p>x&amp;y</p>
</th>
<th>
<p>x|y</p>
</th>
</tr>
<tr>
<td>
<p>true</p>
</td>
<td>
<p>true</p>
</td>
<td>
<p>true</p>
</td>
<td>
<p>true</p>
</td>
</tr>
<tr>
<td>
<p>true</p>
</td>
<td>
<p>false</p>
</td>
<td>
<p>false</p>
</td>
<td>
<p>true</p>
</td>
</tr>
<tr>
<td>
<p>true</p>
</td>
<td>
<p>null</p>
</td>
<td>
<p>null</p>
</td>
<td>
<p>true</p>
</td>
</tr>
<tr>
<td>
<p>false</p>
</td>
<td>
<p>true</p>
</td>
<td>
<p>false</p>
</td>
<td>
<p>true</p>
</td>
</tr>
<tr>
<td>
<p>false</p>
</td>
<td>
<p>false</p>
</td>
<td>
<p>false</p>
</td>
<td>
<p>false</p>
</td>
</tr>
<tr>
<td>
<p>false</p>
</td>
<td>
<p>null</p>
</td>
<td>
<p>false</p>
</td>
<td>
<p>null</p>
</td>
</tr>
<tr>
<td>
<p>null</p>
</td>
<td>
<p>true</p>
</td>
<td>
<p>null</p>
</td>
<td>
<p>true</p>
</td>
</tr>
<tr>
<td>
<p>null</p>
</td>
<td>
<p>false</p>
</td>
<td>
<p>false</p>
</td>
<td>
<p>null</p>
</td>
</tr>
<tr>
<td>
<p>null</p>
</td>
<td>
<p>null</p>
</td>
<td>
<p>null</p>
</td>
<td>
<p>null</p>
</td>
</tr>
</tbody>
</table>
<p>
So what are you waiting for? It&#8217;s time to start using the .NET<br />
Framework 2.0, with it&#8217;s array of nice new features, including Nullable<br />
Types.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/robgarrett.wordpress.com/1542/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/robgarrett.wordpress.com/1542/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/robgarrett.wordpress.com/1542/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/robgarrett.wordpress.com/1542/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/robgarrett.wordpress.com/1542/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/robgarrett.wordpress.com/1542/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/robgarrett.wordpress.com/1542/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/robgarrett.wordpress.com/1542/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/robgarrett.wordpress.com/1542/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/robgarrett.wordpress.com/1542/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/robgarrett.wordpress.com/1542/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/robgarrett.wordpress.com/1542/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/robgarrett.wordpress.com/1542/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/robgarrett.wordpress.com/1542/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/robgarrett.wordpress.com/1542/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/robgarrett.wordpress.com/1542/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1542&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.robgarrett.com/2005/08/30/nullable-types-in-c-2-0/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Rob Garrett</media:title>
		</media:content>
	</item>
		<item>
		<title>Firing multicast delegate events</title>
		<link>http://blog.robgarrett.com/2005/08/17/firing-multicast-delegate-events/</link>
		<comments>http://blog.robgarrett.com/2005/08/17/firing-multicast-delegate-events/#comments</comments>
		<pubDate>Thu, 18 Aug 2005 01:31:00 +0000</pubDate>
		<dc:creator>Rob Garrett</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">/cs/blogs/software/archive/2005/08/17/1521.aspx</guid>
		<description><![CDATA[I am currently writing an ASP.NET server control, which exposes a number of custom events.&#160; I was looking through my code archives for a helper class, which will safely call handler methods of a multicast delegate, when an event is triggered.&#160; I found the class I was looking for, and decided to publish it here [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1521&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I am currently writing an ASP.NET server control, which exposes a<br />
number of custom events.&nbsp; I was looking through my code archives<br />
for a helper class, which will safely call handler methods of a<br />
multicast delegate, when an event is triggered.&nbsp; I found the class<br />
I was looking for, and decided to publish it here on my blog for future<br />
reference.</p>
<p>A quick note on multicast delegates &#8211; Multicast delegates are delegate<br />
instances that are derived from <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemmulticastdelegateclasstopic.asp">System.MulticastDelegate</a><br />
and can have<br />
more than one instance in their invocation list &#8211; essentially they are<br />
delegates<br />
that allow assignment of multiple handler methods. When dealing with<br />
multicast delegates, a question arises about how to deal with<br />
exceptions thrown in handler methods. When the framework executes a<br />
multicast delegate, all handler methods in the invocation list are<br />
executed sequentially.&nbsp; If one of the invoked handler methods<br />
should throw an exception, the remaining handlers in the<br />
list, yet to be executed, are not invoked. </p>
<p>So, let&#8217;s say that we have a custom event on a control (events are<br />
multicast delegates). The containing code of the control subscribes to<br />
the control&#8217;s event with two different event handlers.&nbsp; The event<br />
fires, causing the first handler to be invoked, which then happens to<br />
throw an exception.&nbsp; The second event handler will never get<br />
called, which could cause serious malfunction or resource leakage in<br />
the application if the handler had an important part to play. The<br />
helper class, posted below, alleviates this problem by invoking the<br />
handler methods in a delegate invocation list dynamically, whilst<br />
trapping exceptions.</p>
<p>The helper class exposes methods to execute a multicast delegate, both<br />
synchronously and asynchronously.&nbsp; The synchronous method returns<br />
the list of thrown exceptions, whilst calling all handlers in the<br />
invocation list.&nbsp; The asynchronous method ignores any exceptions<br />
and calls each handler in the invocation list on a separate thread.</p>
<p>public class EventHelper<br />
{<br />
    ///<br />
    /// Asynchronous delegate.<br />
    ///<br />
    private delegate void AsyncFire(Delegate del, object[] args);</p>
<p>    ///<br />
    /// Static instance of asynchronous delegate.<br />
    ///<br />
    private static AsyncFire _asyncFire = new AsyncFire(InvokeDelegate);</p>
<p>    ///<br />
    /// Call back for asynchronous delegate call.<br />
    ///<br />
    private static AsyncCallback _asyncCallback = new AsyncCallback(InvokeDone);</p>
<p>    ///<br />
    /// Fire synchronously.<br />
    ///<br />
    /// If an event handler throws an exception ignore it and move onto the next handler.<br />
    /// Multicast delegate.<br />
    /// Args.<br />
    public static Exception[] Fire(Delegate del, params object[] args)<br />
    {<br />
        List exceptions = new List();<br />
        // If delegate has no handlers then do nothing.<br />
        if (null != del)<br />
        {<br />
            // Iterate the handlers.<br />
            Delegate[] delegates = del.GetInvocationList();<br />
            foreach (Delegate sink in delegates)<br />
            {<br />
                try<br />
                {<br />
                    // Invoke.<br />
                    sink.DynamicInvoke(args);<br />
                }<br />
                catch (Exception ex)<br />
                {<br />
                    exceptions.Add(ex);<br />
                }<br />
            }<br />
        }<br />
        return exceptions.ToArray();<br />
    }</p>
<p>    ///<br />
    /// Fire asynchronously.<br />
    ///<br />
    /// Each handler is fired in an thread from the thread pool.<br />
    /// Multicast delegate.<br />
    /// Args.<br />
    public static void FireAsync(Delegate del, params object[] args)<br />
    {<br />
        // If delegate has no handlers then do nothing.<br />
        if (null != del)<br />
        {<br />
            // Iterate the handlers.<br />
            // Cannot call multicast delegate asyncronously, must process<br />
            // each handler separate.<br />
            Delegate[] delegates = del.GetInvocationList();<br />
            foreach (Delegate sink in delegates)<br />
            {<br />
                // Invoke using a thread from the pool.<br />
                _asyncFire.BeginInvoke(sink, args, _asyncCallback, null);<br />
            }<br />
        }<br />
    }</p>
<p>    ///<br />
    /// Invoke container delegate.<br />
    ///<br />
    /// Delegate.<br />
    /// Args.<br />
    static void InvokeDelegate(Delegate del, object[] args)<br />
    {<br />
        del.DynamicInvoke(args);<br />
    }</p>
<p>    ///<br />
    /// Called when asynchronous delegate call complete.<br />
    ///<br />
    /// Asynchronous result.<br />
    private static void InvokeDone(IAsyncResult ar)<br />
    {<br />
        // Clean up.<br />
        _asyncFire.EndInvoke(ar);<br />
    }<br />
}</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/robgarrett.wordpress.com/1521/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/robgarrett.wordpress.com/1521/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/robgarrett.wordpress.com/1521/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/robgarrett.wordpress.com/1521/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/robgarrett.wordpress.com/1521/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/robgarrett.wordpress.com/1521/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/robgarrett.wordpress.com/1521/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/robgarrett.wordpress.com/1521/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/robgarrett.wordpress.com/1521/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/robgarrett.wordpress.com/1521/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/robgarrett.wordpress.com/1521/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/robgarrett.wordpress.com/1521/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/robgarrett.wordpress.com/1521/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/robgarrett.wordpress.com/1521/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/robgarrett.wordpress.com/1521/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/robgarrett.wordpress.com/1521/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1521&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.robgarrett.com/2005/08/17/firing-multicast-delegate-events/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Rob Garrett</media:title>
		</media:content>
	</item>
		<item>
		<title>Code Comments</title>
		<link>http://blog.robgarrett.com/2005/07/02/code-comments/</link>
		<comments>http://blog.robgarrett.com/2005/07/02/code-comments/#comments</comments>
		<pubDate>Sat, 02 Jul 2005 11:06:00 +0000</pubDate>
		<dc:creator>Rob Garrett</dc:creator>
				<category><![CDATA[Everything]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">/cs/blogs/software/archive/2005/07/02/1401.aspx</guid>
		<description><![CDATA[I have lost count of the number of times that I have been asked to work on a piece of software written by another developer, only to find out that the code had no comments. What typically follows is hours spent wading through lines and lines of complex instruction to determine how the software was [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1401&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I have lost count of the number of times that I have been asked to work<br />
on a piece of software written by another developer, only to find out<br />
that the code had no comments. What typically follows is hours spent<br />
wading through lines and lines of complex instruction to determine how<br />
the software was intended to work before I could make any<br />
modifications.&nbsp; I can hear the groans, yes it&#8217;s the old &#8220;code<br />
comments&#8221; debate that has been going on between software developers<br />
since the first lines of code was punched into a machine capable of<br />
storing and editing of instructions.</p>
<p>In my honest opinion I see no real good reason for the absence of code<br />
comments.&nbsp; The days of programming in a vacuum are long gone,<br />
also are the days of programming software, which is never to be<br />
maintained once it reaches production.&nbsp; There is a strong<br />
probability that any code that you develop today will be reviewed<br />
and/or maintained by someone else tomorrow. Yet, there are still plenty<br />
of developers who believe that this simplistic approach to software<br />
documentation is a hindrance to their productivity and refuse to<br />
comment.</p>
<p>Below are the excuses that I have heard from other software developers when asked why their code has no comments:</p>
<p><span style="font-weight:bold;">1. &#8220;I had a deadline to meet and did not have time to add comments.&#8221;</span><br />
Adding code comments at the time of writing adds very little to the<br />
overall development time of a project.&nbsp; I would argue that it<br />
actually saves time because it saves the developer having to remember<br />
what they wrote a few days ago when coming back to a previously written<br />
module. Comments will also save hours of work down the line when the<br />
same piece of software is maintained or enhanced. The time required to<br />
add comments should be considering in the original implementation<br />
estimate.</p>
<p><span style="font-weight:bold;">2. &#8220;I&#8217;ll add the comments later after our software is released to production.&#8221;</span><br />
Comments are an inherent part of the software implementation process,<br />
and should not be considered as an after thought.&nbsp; Adding code<br />
comments as you develop software is easier than having to go back and<br />
add them later. Greater accuracy of comments is guaranteed if comments<br />
are added while the intended operation of code is fresh in the mind of<br />
the developer.&nbsp; Often is the case that developers rarely go back<br />
and add comments to production code.</p>
<p><span style="font-weight:bold;">3. &#8220;Comments slow down my compilation.&#8221;</span><br />
Nope! most preprocessors and compilers are optimized to skip comments<br />
when translating code into machine instructions or intermediate<br />
language code.&nbsp; Code comments no more slow down compilation than<br />
adding white space characters between lines of code.</p>
<p><span style="font-weight:bold;">4. &#8220;If no-one knows how my software works I&#8217;ll have job security.&#8221;</span><br />
False! If your employer has a need to terminate your agreement, writing<br />
uncommented code will not save your butt. Sure, it&#8217;ll cost more time<br />
and money having someone else follow your work, but in the long run<br />
your code will get replaced with code that is easier to maintain,<br />
saving time and money overall.</p>
<p><span style="font-weight:bold;">5. &#8220;I am just too damn lazy to add comments to my code.&#8221;</span><br />
If you cannot take enough pride in your work to write a good piece of<br />
software with comments, which others can continue developing against<br />
after your done, then you should consider another profession and stop<br />
making life difficult for other developers who need to follow you.</p>
<p><span style="font-weight:bold;">6. &#8220;I plan on stiffing my employer by creating unmaintainable code and then leaving.&#8221;</span><br />
You&#8217;re not going to hurt your employer, just the poor developer who follows you, which might be me.</p>
<p>The following are all good reasons why code comments are a good thing:</p>
<p><font color="#0000ff"><b>1. Your code can be maintained by someone else after you&#8217;ve moved on.<br />
2. You&#8217;ll be harassed less by your employer if someone else can work on your code in your absence.<br />
3. Forget what you wrote yesterday? Read your own comments, you should be able to understand them.<br />
4. Comments above classes and methods help break up code and distinguish one method or class from another.<br />
5. Any special circumstances or quirks in the code will be obvious to someone editing your code.<br />
6.&nbsp; Software change requests and bug numbers can be indicated in the code, linking the code with a bug tracking system.<br />
7. Public API code is easier to use by 3rd parties.<br />
8. If you can comment your code then you can demonstrate a greater understanding during code reviews.</b><br />
</font><br />
Finally, for those of you developing C# or Visual Basic .Net<br />
applications, using Visual Studio 2005, I have created a macro to make<br />
the task of code commenting easier:</p>
<p>1. Download the zip file, containing a VB macro file, from <a href="http://robgarrett.com/blogs/downloads/xmlcomments.zip">here</a>.<br />
2. Open the macro explorer in Visual Studio 2005.<br />
3. Create a new macros project.<br />
4. Create a new macro in the project and overwrite the VB code (all<br />
macros are written in VB) with the XmlComments.vb code downloaded in<br />
the zip file.<br />
5. Customize VS.Net by adding a keyboard short cut to the &#8220;CommentMe&#8221; macro in the new project you just created.</p>
<p>So, what does my macro code do that the regular doc comments built into VS.Net (/// in C#) do not already do?</p>
<p>1. Comments are added above classes, methods and properties, depending<br />
on the current cursor position. This makes for faster development, e.g.<br />
when adding comment, just execute the macro when inside the curly<br />
braces. No need for moving the cursor above the method, property or<br />
class to trigger the built-in doc comments.</p>
<p>2. My macro detects parameter types and adds <i>cref </i>tags to the parameter comments.</p>
<p>3. Any exceptions used in a method or property are detected and<br />
automatically commented in the comments above the method or property<br />
using the <i>exception</i> tag.</p>
<p>4. Existing comments are not overwritten by my macro, instead my macro adds missing comment tags to existing comments.</p>
<p>5. When overriding methods and properties from a base class, my macros<br />
code will use the same comment from the base class version.</p>
<p>6. VS.Net adds empty tags when using the built-in doc comments, my macro has a stab at suggesting a default comment.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/robgarrett.wordpress.com/1401/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/robgarrett.wordpress.com/1401/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/robgarrett.wordpress.com/1401/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/robgarrett.wordpress.com/1401/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/robgarrett.wordpress.com/1401/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/robgarrett.wordpress.com/1401/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/robgarrett.wordpress.com/1401/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/robgarrett.wordpress.com/1401/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/robgarrett.wordpress.com/1401/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/robgarrett.wordpress.com/1401/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/robgarrett.wordpress.com/1401/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/robgarrett.wordpress.com/1401/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/robgarrett.wordpress.com/1401/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/robgarrett.wordpress.com/1401/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/robgarrett.wordpress.com/1401/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/robgarrett.wordpress.com/1401/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.robgarrett.com&amp;blog=7688126&amp;post=1401&amp;subd=robgarrett&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.robgarrett.com/2005/07/02/code-comments/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Rob Garrett</media:title>
		</media:content>
	</item>
	</channel>
</rss>
