<?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>Ellis Web &#187; c#</title>
	<atom:link href="http://ellisweb.net/category/code/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://ellisweb.net</link>
	<description>Thoughts, Articles &#38; Links on Programming &#38; Technology by Yaakov Ellis</description>
	<lastBuildDate>Wed, 21 Apr 2010 07:01:00 +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>Connecting to Excel and Access Files using .Net on a 64-bit Server</title>
		<link>http://ellisweb.net/2010/01/connecting-to-excel-and-access-files-using-net-on-a-64-bit-server/</link>
		<comments>http://ellisweb.net/2010/01/connecting-to-excel-and-access-files-using-net-on-a-64-bit-server/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 13:30:42 +0000</pubDate>
		<dc:creator>Yaakov Ellis</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[64-bit]]></category>
		<category><![CDATA[access]]></category>
		<category><![CDATA[ace]]></category>
		<category><![CDATA[excel]]></category>
		<category><![CDATA[jet]]></category>
		<category><![CDATA[odbc]]></category>
		<category><![CDATA[oledb]]></category>

		<guid isPermaLink="false">http://ellisweb.net/?p=436</guid>
		<description><![CDATA[If you are trying to query a MS Excel (.xls, .xlsx, .xlsb) or MS Access (.mdb, .accdb) file on a 64-bit server and are getting one of the following error messages: The &#8216;Microsoft.Jet.OLEDB.4.0&#8242; provider is not registered on the local machine. The &#8216;Microsoft.ACE.OLEDB.12.0&#8242; provider is not registered on the local machine. Then it is necessary [...]]]></description>
			<content:encoded><![CDATA[<p>If you are trying to query a MS Excel (.xls, .xlsx, .xlsb) or MS Access (.mdb, .accdb) file on a 64-bit server and are getting one of the following error messages:</p>
<ul>
<li>The &#8216;Microsoft.Jet.OLEDB.4.0&#8242; provider is not registered on the local machine.</li>
<li>The &#8216;Microsoft.ACE.OLEDB.12.0&#8242; provider is not registered on the local machine.</li>
</ul>
<p>Then it is necessary to install <a href="http://www.microsoft.com/downloads/details.aspx?familyid=C06B8369-60DD-4B64-A44B-84B371EDE16D&amp;displaylang=en">2010 Office System Driver Beta: Data Connectivity Components</a> on the server (the reason for this is the old Jet4.0 drover does not exist for 64 bit, and the ACE driver needed to read the newer formats is not installed by default). If you are using a version of Windows with UAC enabled, be sure to do this as Administrator.</p>
<p>After installing the components, use the following connection string formats (from the page linked-to above):</p>
<ul>
<li>Using <strong>OLEDB</strong>, set the Provider argument of the ConnectionString property to “Microsoft.ACE.OLEDB.14.0”. Example: &#8220;Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Path_To_File&#8221;</li>
<li>Using <strong>OLEDB </strong>and connecting to a Microsoft Office Excel file, add “Excel 14.0” to the Extended Properties of the OLEDB connection string defined in the previous bullet point.</li>
<li>Using <strong>ODBC</strong>:
<ul>
<li>Connecting to Microsoft Office Access (.mdb or .accdb): set the Connection String to “Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=path to mdb/accdb file”</li>
<li>Connecting to Microsoft Office Excel (.xls, .xlsx, .xlsb): set the Connection String to “Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=path to xls/xlsx/xlsm/xlsb file”</li>
</ul>
</li>
</ul>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fellisweb.net%2F2010%2F01%2Fconnecting-to-excel-and-access-files-using-net-on-a-64-bit-server%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fellisweb.net%2F2010%2F01%2Fconnecting-to-excel-and-access-files-using-net-on-a-64-bit-server%2F&amp;source=yaakov&amp;style=compact&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://ellisweb.net/2010/01/connecting-to-excel-and-access-files-using-net-on-a-64-bit-server/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Casting vs. Converting in .Net</title>
		<link>http://ellisweb.net/2008/12/casting-vs-converting-in-net/</link>
		<comments>http://ellisweb.net/2008/12/casting-vs-converting-in-net/#comments</comments>
		<pubDate>Thu, 11 Dec 2008 20:40:55 +0000</pubDate>
		<dc:creator>Yaakov Ellis</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[cast]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[convert]]></category>
		<category><![CDATA[types]]></category>

		<guid isPermaLink="false">http://ellisweb.net/?p=406</guid>
		<description><![CDATA[I recently saw a piece of code in a project that was throwing an exception that I did not understand: string boolString = (string)DataBinder.Eval(DataItem, "IsNew"); One would expect that boolString would be assigned the value &#8220;True&#8221; or &#8220;False&#8221; depending on the value of the IsNew property of the DataItem object. However, this through an Exception [...]]]></description>
			<content:encoded><![CDATA[<p>I recently saw a piece of code in a project that was throwing an exception that I did not understand:</p>
<pre name="code" class="c#">
string boolString = (string)DataBinder.Eval(DataItem, "IsNew");
</pre>
<p>One would expect that boolString would be assigned the value &#8220;True&#8221; or &#8220;False&#8221; depending on the value of the IsNew property of the DataItem object. However, this through an Exception with the message: &#8220;Cannot explicitly convert bool to string&#8221;. What is going on here?</p>
<p>The syntax of <em>(<strong>type</strong>)variable</em> attempts to <a href="http://msdn.microsoft.com/en-us/library/ms173105(VS.80).aspx">explicitly cast</a> the variable into the given type. Casting does not attempt to interpret the data in the variable &#8211; it just tries to fit the object referred to by the variable into the new data type. This will work whenever the two types are somehow compatible (for example, an int can be cast into a float with no exceptions) though sometimes it may result in data loss (a float cast into an int). However, in cases where there is no connection between the two classes, casting will result in an Exception (like the one that I received above).</p>
<p>(There is another way to cast an object &#8211; using the <em>as</em> keyword. This will return a null if the cast fails, and is <a href="http://www.codeproject.com/KB/cs/csharpcasts.aspx">much faster</a> than the explicit casting referred to above).</p>
<p>However, when converting an object, the conversion function has &#8220;knowledge&#8221; of the data contained in both the source and final object types, and will create the equivalent of the variable in the new data type. In the case above, using the System.Convert.ToString() method in place of the attempted cast to (string) would have worked fine. This is because the ToString method &#8220;knows&#8221; that a bool cannot be case into a string &#8211; but it also knows what the equivalent string to each value of a bool will be, and is able to process the operation accordingly. In this case, reflector uncovers the following code (in the Boolean class):</p>
<pre name="code" class="c#">
public string ToString() {
    if (!this) {
        return "False";
    }
    return "True";
}
</pre>
<p>This is obviously a very simple type conversion. However, there are many more complex conversion utilities built into the .Net framework (accessible through the <a href="http://msdn.microsoft.com/en-us/library)/system.convert.aspx">System.Convert</a> class &#8211; see the DateTime.ToString conversion for an example of this. Also see the IConvertible interface). While conversions can be more expensive to run, since they are strongly typed, they are checked at compile time, and when used properly are more reliable to use when producing stable code. (See the MSDN article on <a href="http://msdn.microsoft.com/en-us/library/ms173105.aspx">Casting and Type Conversions</a> for more info).
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fellisweb.net%2F2008%2F12%2Fcasting-vs-converting-in-net%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fellisweb.net%2F2008%2F12%2Fcasting-vs-converting-in-net%2F&amp;source=yaakov&amp;style=compact&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://ellisweb.net/2008/12/casting-vs-converting-in-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Detecting Application Idle State in Windows Forms</title>
		<link>http://ellisweb.net/2008/02/detecting-application-idle-state-in-windows-forms/</link>
		<comments>http://ellisweb.net/2008/02/detecting-application-idle-state-in-windows-forms/#comments</comments>
		<pubDate>Thu, 07 Feb 2008 11:44:21 +0000</pubDate>
		<dc:creator>Yaakov Ellis</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[windows forms]]></category>
		<category><![CDATA[application idle]]></category>
		<category><![CDATA[idle]]></category>

		<guid isPermaLink="false">http://ellisweb.net/2008/02/detecting-application-idle-state-in-windows-forms/</guid>
		<description><![CDATA[On a recent project, I had the need to detect whether or not the application is idle, and if so, for how long has the idle state persisted. Idle in my case is defined as no mouse movement or keyboard activity when any of the forms of the application are in focus. If a different [...]]]></description>
			<content:encoded><![CDATA[<p>On a recent project, I had the need to detect whether or not the application is idle, and if so, for how long has the idle state persisted. Idle in my case is defined as no mouse movement or keyboard activity when any of the forms of the application are in focus. If a different program is in focus, I define this as being an application-idle state (for my app), regardless of whether or not there is input activity from keyboard or mouse.</p>
<p>On researching the subject, I found several approaches. The <a href="http://www.thescripts.com/forum/thread256888.html">main approach</a> I have seen is to use the static <a href="http://msdn2.microsoft.com/en-us/library/system.windows.forms.application.idle.aspx">Application.Idle</a> event. This event fires whenever &#8220;application finishes processing and is about to enter the idle state&#8221; &#8211; In other words, whenever the application&#8217;s message queue is empty. The problem with this approach is that this event fires a lot, so much that it becomes impractical for tracking idle state the way that I need it (it doesn&#8217;t help that any Timer operating to track how long the idle state persists would set off Application.Idle, further complicating the situation).</p>
<p>The other approach that I have seen is to set up some Windows hooks to detect mouse and keyboard activity. I have zero experience operating with the Windows API, so thankfully, I found a post by Johan Danforth that gives some working code for doing exactly what I needed: <a href="http://weblogs.asp.net/jdanforth/archive/2006/06/21/454219.aspx">Detecting Idle Time with Mouse and Keyboard Hooks</a>. I integrated the code with my application and tested it out and it worked great.</p>
<p>There was one problem however: this code detects idle time for all applications. In other words, if your application is open but not in focus and you use your mouse or keyboard, the code changes your application status from Idle to Active. For my purposes (see definition if <em>idle</em> above) this is not good enough. So I inspected different properties of the System,Windows.Forms.Form class to see what could tell me whether or not a given form is active. The first candidates were Focused, TopLevel, TopMost and Visible, but none of these did the job. The property that ended up telling me exactly what I needed to know is <a href="http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.containsfocus.aspx">ContainsFocus</a>. This is a property of the Control class (from which Form inherits) and it &#8220;Gets a value indicating whether the control, or one of its child controls, currently has the input focus&#8221;. (Focused is not good enough, since it only returns true when the form itself has focus, but returns false when a child control contained within the form has focus).</p>
<p>I also needed to detect whether any of the secondary forms of my application had focus (since I could have more than one window open at a time, only one of which could have focus). Here is the code that I used:</p>
<pre name="code" class="c#">private bool DoesApplicationHaveFocus() {
  bool hasFocus = false;
  if (ContainsFocus) {
    hasFocus = true;
  } else {
    FormCollection forms = Application.OpenForms;
    foreach (Form f in forms) {
      if (f != null &#038;&#038; f.ContainsFocus) {
        hasFocus = true;
        break;
      }
    }
  }
  return hasFocus;
}</pre>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fellisweb.net%2F2008%2F02%2Fdetecting-application-idle-state-in-windows-forms%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fellisweb.net%2F2008%2F02%2Fdetecting-application-idle-state-in-windows-forms%2F&amp;source=yaakov&amp;style=compact&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://ellisweb.net/2008/02/detecting-application-idle-state-in-windows-forms/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Custom DateTime Format Strings for .Net</title>
		<link>http://ellisweb.net/2007/11/custom-datetime-format-strings-for-net/</link>
		<comments>http://ellisweb.net/2007/11/custom-datetime-format-strings-for-net/#comments</comments>
		<pubDate>Thu, 15 Nov 2007 13:16:25 +0000</pubDate>
		<dc:creator>Yaakov Ellis</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[formatting]]></category>

		<guid isPermaLink="false">http://ellisweb.net/2007/11/custom-datetime-format-strings-for-net/</guid>
		<description><![CDATA[I have had to look up on DateTime formatting on MSDN so many times, I have lost track. So I am putting the links and some of the key information here for my own future reference. Standard DateTime Format Strings d/D short/long date f long date, short time F long date, long time g short [...]]]></description>
			<content:encoded><![CDATA[<p>I have had to look up on DateTime formatting on MSDN so many times, I have lost track. So I am putting the links and some of the key information here for my own future reference.</p>
<p><span id="more-211"></span></p>
<ul>
<li><a href="http://msdn2.microsoft.com/en-us/library/az4se3k1.aspx">Standard DateTime Format Strings</a><br />
<table border="0" cellpadding="2" cellspacing="0" width="374">
<tr>
<td valign="top" width="134">d/D</td>
<td valign="top" width="238">short/long date</td>
</tr>
<tr>
<td valign="top" width="144">f</td>
<td valign="top" width="247">long date, short time</td>
</tr>
<tr>
<td valign="top" width="147">F</td>
<td valign="top" width="249">long date, long time</td>
</tr>
<tr>
<td valign="top" width="148">g</td>
<td valign="top" width="249">short date, short time</td>
</tr>
<tr>
<td valign="top" width="149">G</td>
<td valign="top" width="249">short date, long time</td>
</tr>
<tr>
<td valign="top" width="149">t/T</td>
<td valign="top" width="249">short/long time</td>
</tr>
</table>
</li>
<li><a href="http://msdn2.microsoft.com/en-us/library/az4se3k1.aspx">Custom DateTime Format Strings</a><br />
<table border="0" cellpadding="2" cellspacing="0" width="374">
<tr>
<td valign="top" width="134">d</td>
<td valign="top" width="238">day 1-31</td>
</tr>
<tr>
<td valign="top" width="144">dd</td>
<td valign="top" width="247">day 01-31</td>
</tr>
<tr>
<td valign="top" width="147">ddd</td>
<td valign="top" width="249">abbreviated day name</td>
</tr>
<tr>
<td valign="top" width="148">dddd</td>
<td valign="top" width="249">full day name</td>
</tr>
<tr>
<td valign="top" width="149">h</td>
<td valign="top" width="249">hour 1-12</td>
</tr>
<tr>
<td valign="top" width="149">hh</td>
<td valign="top" width="249">hour 01-12</td>
</tr>
<tr>
<td valign="top" width="149">H</td>
<td valign="top" width="249">hour 0-23</td>
</tr>
<tr>
<td valign="top" width="149">HH</td>
<td valign="top" width="249">hour 00-23</td>
</tr>
<tr>
<td valign="top" width="149">m</td>
<td valign="top" width="249">minute 0-59</td>
</tr>
<tr>
<td valign="top" width="149">mm</td>
<td valign="top" width="249">minute 00-59</td>
</tr>
<tr>
<td valign="top" width="149">M</td>
<td valign="top" width="249">month 1-12</td>
</tr>
<tr>
<td valign="top" width="149">MM</td>
<td valign="top" width="249">month 01-12</td>
</tr>
<tr>
<td valign="top" width="149">MMM</td>
<td valign="top" width="249">abbreviated month name</td>
</tr>
<tr>
<td valign="top" width="149">MMMM</td>
<td valign="top" width="249">full month name</td>
</tr>
<tr>
<td valign="top" width="149">s/ss</td>
<td valign="top" width="249">seconds (0/00)</td>
</tr>
<tr>
<td valign="top" width="149">t</td>
<td valign="top" width="249">am/pm</td>
</tr>
<tr>
<td valign="top" width="149">y/yy</td>
<td valign="top" width="249">year</td>
</tr>
</table>
</li>
<li>MS Reporting Services <a href="http://thavash.spaces.live.com/blog/cns!CF6232111374DFD2!155.entry?wa=wsignin1.0">Date Formatting</a></li>
<li><a href="http://blog.stevex.net/index.php/parsing-dates-and-times-in-net/">Blog post from SteveX</a> on using <a href="http://msdn2.microsoft.com/en-us/library/w2sa9yss.aspx">DateTime.ParseExact</a></li>
</ul>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fellisweb.net%2F2007%2F11%2Fcustom-datetime-format-strings-for-net%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fellisweb.net%2F2007%2F11%2Fcustom-datetime-format-strings-for-net%2F&amp;source=yaakov&amp;style=compact&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://ellisweb.net/2007/11/custom-datetime-format-strings-for-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Repeater Failure and Disappearance on Row 28</title>
		<link>http://ellisweb.net/2007/08/repeater-failure-and-disappearance-on-row-28/</link>
		<comments>http://ellisweb.net/2007/08/repeater-failure-and-disappearance-on-row-28/#comments</comments>
		<pubDate>Thu, 02 Aug 2007 07:47:46 +0000</pubDate>
		<dc:creator>Yaakov Ellis</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[repeater]]></category>

		<guid isPermaLink="false">http://ellisweb.net/2007/08/repeater-failure-and-disappearance-on-row-28/</guid>
		<description><![CDATA[I was working on an ASP.net application (1.1) the other day, changing the UI display of a page. This page basically consisted of a Repeater being populated with data from the DB, and binding javascript actions and styling info to the different rows to be output (each overall it was producing a Table, and each [...]]]></description>
			<content:encoded><![CDATA[<p>I was working on an ASP.net application (1.1) the other day, changing the UI display of a page. This page basically consisted of a Repeater being populated with data from the DB, and binding javascript actions and styling info to the different rows to be output (each overall it was producing a Table, and each item was a TableRow). Although the look-and-feel was going to be changing, as well as the manner in which the data was to be retrieved, the actual data set up and binding was to remain the same as it had been. Piece of cake, right?</p>
<p>The programming was straight-forward enough. When I first tested the feature on a few records, it worked fine. However, when I tested it on more records, it didn&#8217;t work. And when I say that it didn&#8217;t work, I am not saying that I got back some ugly .Net exception. I mean that the HTML output by the server was completely devoid of the Repeater element. It was as if it just did not exist. Somehow, when I tried the load the page with a large number of records,  some weired thing happened in IIS that just completely abandoned the rendering of the Repeater element without even providing an error message. Completely weird.</p>
<p>After a couple of hours of debugging, I was able to determine that the error was not related to any specific data item. Rather, it always happened when outputting the 28th DataItem in the Repeater. 27 worked fine. 28 killed it. Racking my brain (and Google), I couldn&#8217;t find any reason why this would be happening. This exact same repeater worked fine on other pages, on the live site. There was no reason I could think of why this type of failure would happen, and why it would happen here.</p>
<p>My coworker SeanS suggested that perhaps this was related to my dev environment, perhaps some faulty memory or something like that. There was nothing else to go on and this <em>did</em> explain what was going on (and I knew that the feature worked for &lt;28 rows), so I proceeded with development. And lo and behold, when I uploaded my changes to the staging site yesterday, it worked with any number of rows without any hitch-up.</p>
<p>Moral of the story: weird things happen on web servers and on dev machines. Not every error that you get is related to code (especially if the error is reported by the absence of output rather than an exception). If something like this is happening to you (and it is not reproducible on other platforms), it may very well be environment-related. Test this out before wasting more time and energy trying to sort it out through the code.
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fellisweb.net%2F2007%2F08%2Frepeater-failure-and-disappearance-on-row-28%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fellisweb.net%2F2007%2F08%2Frepeater-failure-and-disappearance-on-row-28%2F&amp;source=yaakov&amp;style=compact&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://ellisweb.net/2007/08/repeater-failure-and-disappearance-on-row-28/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Enumerating a Dictionary Object</title>
		<link>http://ellisweb.net/2007/06/enumerating-a-dictionary-object/</link>
		<comments>http://ellisweb.net/2007/06/enumerating-a-dictionary-object/#comments</comments>
		<pubDate>Sun, 03 Jun 2007 16:11:25 +0000</pubDate>
		<dc:creator>Yaakov Ellis</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[collections]]></category>
		<category><![CDATA[dictionary]]></category>
		<category><![CDATA[generics]]></category>
		<category><![CDATA[list]]></category>

		<guid isPermaLink="false">http://ellisweb.net/2007/06/enumerating-a-dictionary-object/</guid>
		<description><![CDATA[In C# using .Net 2.0+, there is now built-in support for Generics collections using the System.Collections.Generic namespace. I use the List type most often, as a strongly-typed substitute for the ArrayList. Since it is strongly-typed, you now have design-time type-checking for enumerations like this (no casting necessary): List&#60;int&#62; Foos = new List&#60;int&#62;(); foo.Add(new foo(1)); foo.Add(new [...]]]></description>
			<content:encoded><![CDATA[<p>In C# using .Net 2.0+, there is now built-in support for <a href="http://msdn2.microsoft.com/en-us/library/512aeb7t(VS.80).aspx">Generics</a> collections using the <a href="http://msdn2.microsoft.com/en-us/library/system.collections.generic(VS.80).aspx">System.Collections.Generic</a> namespace. I use the <a href="http://msdn2.microsoft.com/en-us/library/6sh2ey19(VS.80).aspx">List</a> type most often, as a <a href="http://en.wikipedia.org/wiki/Strong_typing">strongly-typed</a> substitute for the ArrayList. Since it is strongly-typed, you now have design-time type-checking for enumerations like this (no casting necessary):</p>
<pre class="c#" name="code">List&lt;int&gt; Foos = new List&lt;int&gt;();
foo.Add(new foo(1));
foo.Add(new foo(2));
foreach (foo f in Foos) {
  Console.PrintLine(f.ToString());
}</pre>
<p>The second-most useful Generic (in my opinion) is <a href="http://msdn2.microsoft.com/en-us/library/xfhwa508(VS.80).aspx">Dictionary</a>. This is a strongly-typed hashtable, where you can determine the types of both X (the Key) and Y (the Value). Like other Generic collections, the Dictionary object type is also Enumerable (meaning that you can use statements like foreach to automatically parse its contents in order). However, since there are two different types of value stored in a Generic Dictionary object, how do you call an strongly-typed enumeration statement like foreach? This is done by referencing each pair of values as a <a href="http://msdn2.microsoft.com/en-us/library/5tbh8a42(VS.80).aspx">KeyValuePair</a> object (which is another type of Generic supported by System.Collections.Generic&#8221;) and can be performed as follows (<a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=490254&amp;SiteID=1">ref</a>)</p>
<pre class="c#" name="code">Dictionary&lt;string, int&gt; PhoneNumbers = new Dictionary&lt;string, int&gt;();
PhoneNumbers.Add("Jones, Jim", "800-555-2312");
PhoneNumbers.Add("Smith, Beth", "324-555-6742");
foreach(KeyValuePair&lt;string, int&gt; personNumber in PhoneNumbers) {
  Console.PrintLine(personNumber.Key + "\t" + personNumber.Value);
}</pre>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fellisweb.net%2F2007%2F06%2Fenumerating-a-dictionary-object%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fellisweb.net%2F2007%2F06%2Fenumerating-a-dictionary-object%2F&amp;source=yaakov&amp;style=compact&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://ellisweb.net/2007/06/enumerating-a-dictionary-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Declaring Custom Events in User Controls</title>
		<link>http://ellisweb.net/2007/04/declaring-custom-events-in-user-controls/</link>
		<comments>http://ellisweb.net/2007/04/declaring-custom-events-in-user-controls/#comments</comments>
		<pubDate>Sun, 15 Apr 2007 08:31:30 +0000</pubDate>
		<dc:creator>Yaakov Ellis</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[user controls]]></category>

		<guid isPermaLink="false">http://ellisweb.net/2007/04/declaring-custom-events-in-user-controls/</guid>
		<description><![CDATA[I am constantly needing to search my code to find the exact syntax for how to do this, so for future reference (for myself and anyone else who finds this useful), to declare and call an event from a custom user control or inherited control: public class CustomEventArgs : EventArgs { public CustomEventArgs(int propValue) { [...]]]></description>
			<content:encoded><![CDATA[<p>I am constantly needing to search my code to find the exact syntax for how to do this, so for future reference (for myself and anyone else who finds this useful), to declare and call an event from a custom user control or inherited control:</p>
<pre class="c#" name="code">public class CustomEventArgs : EventArgs {
  public CustomEventArgs(int propValue) {
    _propValue = propValue;
  }

  private int _propValue;
  public int PropValue {
    get { return _propValue; }
  }
}

public delegate void CustomEventHandler(object sender, CustomEventArgs e);
public event CustomEventHandler CustomEvent;

public void Function() {
  // Do Stuff
  if (CustomEvent != null) {
    CustomEventArgs e = new CustomEventArgs(x);
    CustomEvent(this, e);
  }
}</pre>
<p>If you don&#8217;t need a CustomEventArgs class, just use the built-in EventArgs instead.
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fellisweb.net%2F2007%2F04%2Fdeclaring-custom-events-in-user-controls%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fellisweb.net%2F2007%2F04%2Fdeclaring-custom-events-in-user-controls%2F&amp;source=yaakov&amp;style=compact&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://ellisweb.net/2007/04/declaring-custom-events-in-user-controls/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Capitalizing a string using C#</title>
		<link>http://ellisweb.net/2007/04/capitalizing-a-string-using-csharp/</link>
		<comments>http://ellisweb.net/2007/04/capitalizing-a-string-using-csharp/#comments</comments>
		<pubDate>Thu, 12 Apr 2007 17:13:19 +0000</pubDate>
		<dc:creator>Yaakov Ellis</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://ellisweb.net/2007/04/capitalizing-a-string-using-c/</guid>
		<description><![CDATA[I was in need of a quick way to capitalize all of the words in a string (and though to check with Google before writing my own implementation). Here is what I found: This one is built-in to .Net, but can be slow at times (and some people do not like the way that it [...]]]></description>
			<content:encoded><![CDATA[<p>I was in need of a quick way to capitalize all of the words in a string (and though to check with Google before writing my own implementation). Here is what I found:</p>
<ol>
<li>This one is <a href="http://msdn2.microsoft.com/en-us/library/system.globalization.textinfo.totitlecase.aspx">built-in</a> to .Net, but can be slow at times (and <a href="http://blogs.msdn.com/michkap/archive/2005/03/04/384927.aspx">some people</a> do not like the way that it handles capitalizations of Unicode strings)</li>
</ol>
<pre name="code" class="c#|nocontrols">System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(value);</pre>
<ol start="2">
<li>Based on Regular Expressions (2 examples <a href="http://www.windowsdevcenter.com/pub/a/oreilly/windows/news/csharp_0101.html">found here</a>)</li>
<li>Some other <a href="http://channel9.msdn.com/ShowPost.aspx?PostID=287545">home-baked implementations</a>, mostly scanning through the string for whitespace, and capitalizing new words as they come up.</li>
</ol>
<p>Am I missing anything?
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fellisweb.net%2F2007%2F04%2Fcapitalizing-a-string-using-csharp%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fellisweb.net%2F2007%2F04%2Fcapitalizing-a-string-using-csharp%2F&amp;source=yaakov&amp;style=compact&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://ellisweb.net/2007/04/capitalizing-a-string-using-csharp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Annoying Coding Practices</title>
		<link>http://ellisweb.net/2007/04/annoying-coding-practices/</link>
		<comments>http://ellisweb.net/2007/04/annoying-coding-practices/#comments</comments>
		<pubDate>Tue, 10 Apr 2007 11:03:27 +0000</pubDate>
		<dc:creator>Yaakov Ellis</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[advice]]></category>

		<guid isPermaLink="false">http://ellisweb.net/2007/04/annoying-coding-practices/</guid>
		<description><![CDATA[I am right now in the middle of transferring a piece of functionality from one project (ASP.net 1.1 web application) to a second project (Windows Forms desktop application). Although it is a significant feature, most of the work should be straight copy-paste. It is a large chunk of code written in C#, referencing a business [...]]]></description>
			<content:encoded><![CDATA[<p>I am right now in the middle of transferring a piece of functionality from one project (ASP.net 1.1 web application) to a second project (Windows Forms desktop application). Although it is a significant feature, most of the work should be straight copy-paste. It is a large chunk of code written in C#, referencing a business object library shared between the two projects, a bunch of external dlls, etc.</p>
<p>Really, all that I <em>should</em> have to do is to customize the function for my projects codebase, migrate some of the ASP.net aspect to a WinForms environment, and hook it up to my UI.  Yet, as I am going through this code, trying to make sense of what is going on, I find myself having to delete/modify/rewrite much more of the code than I should be doing. Although some of this is to be expected, much of it has to do with poor programming practices that if maintained lead to future difficulties with maintenance, poor readability, logic errors and bugs and are detrimental to debugging and testing. True, I have not found so many examples that are worthy of <a href="http://worsethanfailure.com/Default.aspx">public exhibition</a>, but I have received an education in how poor coding practices can effect a section of code long after the original programmer has completed his (or her) work.</p>
<p>A sampling of some of the things that are currently causing me grief:<br />
<span id="more-179"></span><br />
<strong>Not enclosing the body of an <em>if</em> statement (or other control-body) in brackets</strong></p>
<blockquote><pre>DoWork();
foreach (string s in List&lt;string&gt;)
   if (s == "a")
      DoA();
   else if (s == "b")
      DoB();
DoWork();</pre>
</blockquote>
<p>True, this code compiles and works. And thanks to Visual Studio&#8217;s automatic indentation system, you can sort of tell what items belong in what structure by looking at the indentation. But it is confusing. And if someone adds one more line of code somewhere in there without thinking about it, you are going to have big problems on your hands. (What should be done: use open- and close-brackets very liberally)</p>
<p><strong>Leaving superfluous and redundant code in production</strong></p>
<p>I found two different functions, each with <em>hundreds </em>of lines of code, each intended to perform the same function. One of them is used, one of them is not. Yet, neither of them is commented out, and there is not indication given that one of them is not used and is not necessary. This doesn&#8217;t break anything, but is a headache for those who have to deal with the code. (What should be done: if there is a whole section of code that is redundant, either comment it out or delete it)</p>
<p><strong>Mixing different return types in the same functions</strong></p>
<blockquote><pre>private object GetText(string s) {
   if (s == "a") {
      string t = a.ToLower()s
      return t;
   } else {
      StringCollection x = new StringCollection();
      x.Add(s);
      return x;
   }
}

private void Process() {
   // Do Work
   if (a) {
      txtName.Text = (string)GetText(x);
   } else {
      txtName.Text = ((StringCollection)GetText(x))[0];
   }
}</pre>
</blockquote>
<p>The GetText function above can return either a string or a StringCollection, depending on the value of the parameter. Although this works if the programmer knows exactly what is going on and makes no mistakes, it is very error-prone and is a nightmare for maintenance. (What should be done: find some way to use one return type in a function. If this is not possible, create two separate functions. If this is not possible, use reflection to test the value of a returned <em>object</em> before using it in the guise of its assumed type)</p>
<p><strong>Using <em>strings</em> instead of <em>enums</em></strong></p>
<blockquote><pre>
private void DoStuff(string size) {
   switch (size) {
      case "small":
        // work
        break;
      case "large":
        // work
        break;
   }
}</pre>
</blockquote>
<p>Like the previous examples, this code compiles and works. One would presumably either call this function with <em>DoStuff(&#8220;small&#8221;)</em> or <em>DoStuff(&#8220;large&#8221;)</em>. But what if you use a different parameter value. <em>DoStuff(&#8220;Small&#8221;)</em> compiles but fails at runtime, as does <em>DoStuff(&#8220;medium&#8221;)</em>. However, if instead of using a string parameter, you create an <a href="http://msdn2.microsoft.com/en-us/library/sbbt4032.aspx">enum</a> called Sizes with values &#8220;small&#8221; and &#8220;large&#8221;, you guarantee that only valid values can be passed to the function.</p>
<p><strong>Misspelling the product name in the assembly</strong></p>
<p>This doesn&#8217;t really affect anything. It is just sloppy. If your program is called <em>Writing Workshop</em>, please do not create a namespace in your code called &#8220;ritingworksop&#8221;. It is just annoying and reflects badly on your understanding of the application that you are creating.
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fellisweb.net%2F2007%2F04%2Fannoying-coding-practices%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fellisweb.net%2F2007%2F04%2Fannoying-coding-practices%2F&amp;source=yaakov&amp;style=compact&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://ellisweb.net/2007/04/annoying-coding-practices/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WYSIWYG Editing of HTML in a Windows Forms Control</title>
		<link>http://ellisweb.net/2007/03/wysiwyg-editing-of-html-in-a-windows-forms-control/</link>
		<comments>http://ellisweb.net/2007/03/wysiwyg-editing-of-html-in-a-windows-forms-control/#comments</comments>
		<pubDate>Mon, 19 Mar 2007 12:52:57 +0000</pubDate>
		<dc:creator>Yaakov Ellis</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[windows forms]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[rtf]]></category>
		<category><![CDATA[tools]]></category>
		<category><![CDATA[visual studio]]></category>

		<guid isPermaLink="false">http://ellisweb.net/2007/03/wysiwyg-editing-of-html-in-a-windows-forms-control/</guid>
		<description><![CDATA[In a WinForms project that I am working on right now, I have text stored in HTML format in the database. I need a way for novice users to be able to edit this text using a WYSIWYG interface. The most obvious choice for this in the Windows.Forms control library that is part of the [...]]]></description>
			<content:encoded><![CDATA[<p>In a WinForms project that I am working on right now, I have text stored in HTML format in the database. I need a way for novice users to be able to edit this text using a <a href="http://en.wikipedia.org/wiki/WYSIWYG">WYSIWYG</a> interface.</p>
<p>The most obvious choice for this in the Windows.Forms control library that is part of the .Net 2.0/3.0 framework is the <a href="http://msdn2.microsoft.com/en-us/library/3tdc88y7(VS.80).aspx">RichTextBox</a> control. This control gives you a textbox that has functionality similar to the WordPad application found in the Windows OS. It can display formatted pretty well. Aside from the fact that you have to handle all formatting of text in code, RichTextBox it has one major shortcomings: It can only input/output plain text or RTF encoded text. If I wanted to use a RichTextBox control to edit HTML, I would need some way to translate from the source HTML (stored in the DB) to RTF (to be displayed in the RichTextBox) and then back to HTML (to be stored again in the DB). Although I found some components from <a href="http://www.sautinsoft.com/components.php">Sautin Software</a> that seem to do the job, they cost money that I would rather not spend, and add a layer of complexity to my code library that I would rather avoid.</p>
<p>I found a couple of controls that extend the RichTextBox in order to allow input of HTML: <a href="http://www.codeproject.com/cs/miscctrl/htmlrichtextbox.asp">An extended RichTextBox to save and load &#8220;HTML lite&#8221; files</a> by <a href="http://www.codeproject.com/script/Articles/list_articles.asp?userid=3359">Oscar Londoño</a> (Code Project &#8211; For .Net 1.1/VS 2003) and                                                                                                                                              									<a id="_ctl3_samplesBody__ctl4_myCategoryList__ctl6_Linkman2" title="Linkman1" name="Linkman1" href="http://www.windowsforms.net/TrackViews.aspx?ID=186&amp;Type=Samples&amp;Redir=%2fSamples%2fdownload.aspx%3fPageId%3d1%26ItemId%3d186%26tabindex%3d4" target="_self">RichTextBox that Displays XHTML</a> by Eric Voreis. Both of these controls extend the RichTextBox control in the Windows.Forms library so that it can translate on the fly between RTF and HTML code (so you can input HTML code, see it displayed in the RTB and export HTML code). However, both are old and only support a small subset of HTML tags (although this can be extended in the source, it required knowledge of how RTF works). Also, since they use a RichTextBox control for editing and display, the display is similar but not identical to what you would get using a WebBrowser.</p>
<p>The next method that I found for providing this type of error is <a href="http://windowsforms.net/articles/htmleditor.aspx">HTML Editor</a> by Carl Nolan (Microsoft Consulting Services). This control (written in 2003 for .Net 1.1) modifies a WebBrowser control to allow inline editing and display of HTML. It is professionally written, provides support for many HTML tags and does just about everything that I need (and is free). The one problem is that it is pre-.Net 2.0. It uses a WebBrowser control from before this control was included as part of the Windows.Forms library in .Net 2.0 and thus has to reference Win32 calls and perform other types of acrobatics in order to get the control to function properly. This matters to me because it decreases portability of the control (using Mono, for instance), and makes it harder for me to customize and edit.</p>
<p><a title="WebBrowser-based HTML Editor in action" href="http://ellisweb.net/wp-content/uploads/2007/03/html.jpg"><img src="http://ellisweb.net/wp-content/uploads/2007/03/html.jpg" alt="WebBrowser-based HTML Editor in action" width="374" height="211" /></a></p>
<p>The control that I am using in the end employs the same general idea used in HTML Editor, but takes advantage of new features (like the native WebBrowser class) provided by the .Net 2.0/3.0 framework and and Visual Studio 2005. <a href="http://www.codeproject.com/KB/edit/editor_in_windows_forms.aspx">A Windows Forms based text editor with HTML output</a> by <a href="http://www.codeproject.com/Members/kevin-delafield">Kevin DeLafield</a> uses a <a href="http://msdn2.microsoft.com/en-us/library/system.windows.forms.webbrowser.aspx">WebBrowser</a> control with modified functionality to allow WYSIWYG display and editing of HTML text. It provides all of the functionality that I am looking for and allows for easy modifications and customizations in Visual Studio 2005. I recommend that anyone in need of similar functionality take a look at this control.</p>
<p><img src="file:///C:/DOCUME%7E1/Yaakov/LOCALS%7E1/Temp/moz-screenshot.jpg" alt="" />
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fellisweb.net%2F2007%2F03%2Fwysiwyg-editing-of-html-in-a-windows-forms-control%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fellisweb.net%2F2007%2F03%2Fwysiwyg-editing-of-html-in-a-windows-forms-control%2F&amp;source=yaakov&amp;style=compact&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://ellisweb.net/2007/03/wysiwyg-editing-of-html-in-a-windows-forms-control/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
