<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>I, Codemonkey</title>
        <link>http://geekswithblogs.net/ICodemonkey/Default.aspx</link>
        <description>Codemonkeying Around</description>
        <language>en-US</language>
        <copyright>Chas Williams</copyright>
        <managingEditor>chas.williams@gmail.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <image>
            <title>I, Codemonkey</title>
            <url>http://geekswithblogs.net/images/RSS2Image.gif</url>
            <link>http://geekswithblogs.net/ICodemonkey/Default.aspx</link>
            <width>77</width>
            <height>60</height>
        </image>
        <item>
            <title>C# Extensions and Casting</title>
            <link>http://geekswithblogs.net/ICodemonkey/archive/2007/10/07/115902.aspx</link>
            <description>One of the things in VS 2008 that has intrigued me has been Extension Methods. I've been trying to think of ways to use these in my applications and coming up with ways to move functions associated with various processes to Extensions in our own APIs. ScottGu recently posted an interesting article on his weblog about writing an &lt;a href="http://weblogs.asp.net/scottgu/archive/2007/10/01/tip-trick-building-a-tojson-extension-method-using-net-3-5.aspx"&gt;Extension for creating JSON&lt;/a&gt;. &lt;br /&gt;
&lt;br /&gt;
One thing I dislike doing and have to do fairly often is cast Object sender to various things to pull information out of it. The most common reason to do this is that I'm making my buttons dynamically and I need to know which button was pressed so that I can have the program act accordingly. Usually I do something like this:&lt;br /&gt;
&lt;br /&gt;
Button b = (Button)sender;&lt;br /&gt;
&lt;br /&gt;
Then grab something out of it like b.Text and use that value.&lt;br /&gt;
I dislike this because the casting is just straight up, no holds barred, you were one thing and now you're this. And, if I'm wrong I throw an exception. So, I should be doing some checking, some Exception handling, and what have you. But, I really don't want to clutter up the code with all that since the Event is only tied to these buttons. Of course, a nagging in the back of my mind asks if it will always be that way (probably) and another nagging asks if I'm compromising my ethics by not doing these checks.&lt;br /&gt;
&lt;br /&gt;
Then along comes Extension methods in the VS 2008 (Orcas) Beta. I can do basically the same things using Extensions but hide some error checking behind the scenes without having to write the code over and over. I can also update my casting method in one place and it will be changed everywhere, because right now it is very basic. Here's what I have so far:&lt;br /&gt;
&lt;br /&gt;
        public static Button AsButton(this object o)&lt;br /&gt;
        {&lt;br /&gt;
            if (o is Button)&lt;br /&gt;
                return (Button)o;&lt;br /&gt;
            return null;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Then in the forms code:&lt;br /&gt;
&lt;br /&gt;
        Button btn = sender.AsButton() ?? new Button();&lt;br /&gt;
&lt;br /&gt;
This is, of course, very simplistic, but it does allow me to create a place where I can handle this casting in one place, something I am &lt;span style="font-style: italic;"&gt;very &lt;/span&gt;fond of doing. Although I'm still contemplating whether or not returning null is the right answer. Basically what I'm saying is that it will always have to be a Button, and if it isn't a button... then its a Button. I'm not sure if I like that. But, that's not the the functionality of the Extension doing that, so what I do with that returned null can vary from place to place.&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=115902"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=115902" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/ICodemonkey/aggbug/115902.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Chas Williams</dc:creator>
            <guid>http://geekswithblogs.net/ICodemonkey/archive/2007/10/07/115902.aspx</guid>
            <pubDate>Sun, 07 Oct 2007 20:24:53 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/ICodemonkey/comments/115902.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/ICodemonkey/archive/2007/10/07/115902.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/ICodemonkey/comments/commentRss/115902.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/ICodemonkey/services/trackbacks/115902.aspx</trackback:ping>
        </item>
        <item>
            <title>You made my program the SHELL???</title>
            <link>http://geekswithblogs.net/ICodemonkey/archive/2007/08/19/114772.aspx</link>
            <description>So, a while back I created what I consider a neat .NET program. It opens a remote desktop session to a Virtual Machine we have running on VMWare and logs into the machine. Then, if the remote session is closed, it shuts down the computer. It did have the downside that the remote session could, of course, be minimized and the users could access the computer. We've got some Neoware boxes now, but these were good for some training sessions we were doing throughout the company.&lt;br /&gt;
&lt;br /&gt;
In any case, I found out today that the shells of all these laptops had been replaced by my program. That means no taskbar, and it pretty much locks out the computer completely. But, I swear, I swear! that nobody had told me about this change. It took me about 20 minutes before I gave up trying to fix one of these laptops and asked, only to be assured that I had agreed to this crazy idea. It's pretty cool, I have to admit. But, now getting the computers back is going to be a pain. And, here I thought the program could just be removed from Windows Startup folder.&lt;br /&gt;
&lt;br /&gt;
Acronis, here I come!&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=114772"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=114772" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/ICodemonkey/aggbug/114772.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Chas Williams</dc:creator>
            <guid>http://geekswithblogs.net/ICodemonkey/archive/2007/08/19/114772.aspx</guid>
            <pubDate>Sun, 19 Aug 2007 19:06:39 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/ICodemonkey/comments/114772.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/ICodemonkey/archive/2007/08/19/114772.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/ICodemonkey/comments/commentRss/114772.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/ICodemonkey/services/trackbacks/114772.aspx</trackback:ping>
        </item>
        <item>
            <title>Custom Exceptions and Exception Wrapping</title>
            <link>http://geekswithblogs.net/ICodemonkey/archive/2007/07/03/113647.aspx</link>
            <description>I work almost only with code written by predecessors at my job. There's good code and bad code. Recently, I've been trying to clean up the exceptions. Just about everything is throw Exception(&amp;lt;my message here&amp;gt;) instead of customized exceptions, so you never really know what you're catching. It could be an IO exception, a null pointer, or whatever. The message is generally something that can be displayed to the user without them becoming confused, but with a mix of "good" message exceptions and "bad" ones (for example something generated by SQL), its impossible to dig through them and see what I need and what the user needs.&lt;br /&gt;
&lt;br /&gt;
So, while consolidating much of this into an error handling class that will aid us both, I've been looking into creating my own exceptions and going over some code I've never really had a chance to examine before. I'm starting with database access because that's been the biggest problem for me in this regard. Here's a snip of part of our back end that opens a database connection with some stuff cut out to make it more readable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Open the database.&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name="tDatabaseType"&amp;gt;Which database to connect to&amp;lt;/param&amp;gt;&lt;br /&gt;
        public void Open(DBType tDatabaseType)&lt;br /&gt;
        {&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                _Connection.Open();&lt;br /&gt;
                _Command = new OleDbCommand();&lt;br /&gt;
                _Command.Connection = _Connection;&lt;br /&gt;
            }&lt;br /&gt;
            catch(Exception ex)&lt;br /&gt;
            {&lt;br /&gt;
                _lastError = ex.Message.ToString();&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
I didn't even realize until now that there was no exception being thrown when it can't connect to the database. This isn't how I'd like to do things, especially considering that I want to centralize exception handling. This way, the connection never happens, but I don't know that unless I either check _lastError or until I try to use the connection and get an exception thrown at me. First, I'm going to create my own custom exception.&lt;br /&gt;
&lt;br /&gt;
using System;&lt;br /&gt;
using System.Runtime.Serialization;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Text;&lt;br /&gt;
&lt;br /&gt;
namespace DecoCore.Err&lt;br /&gt;
{&lt;br /&gt;
    [Serializable]&lt;br /&gt;
    public class DatabaseNotFoundException : Exception&lt;br /&gt;
    {&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// The database could not open a connection&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        public DatabaseNotFoundException()&lt;br /&gt;
            : base()&lt;br /&gt;
        {&lt;br /&gt;
        }&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// The database could not open a connection&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name="sMsg"&amp;gt;Message displayed to user&amp;lt;/param&amp;gt;&lt;br /&gt;
        public DatabaseNotFoundException(string sMsg)&lt;br /&gt;
            : base(sMsg)&lt;br /&gt;
        {&lt;br /&gt;
        }&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// The database could not open a connection&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name="sMsg"&amp;gt;Message displayed to user&amp;lt;/param&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name="eInnerException"&amp;gt;exception causing this exception&amp;lt;/param&amp;gt;&lt;br /&gt;
        public DatabaseNotFoundException(string sMsg,&lt;br /&gt;
            Exception eInnerException)&lt;br /&gt;
            : base(sMsg, eInnerException)&lt;br /&gt;
        {&lt;br /&gt;
        }&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// The database could not open a connection&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name="info"&amp;gt;&amp;lt;/param&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name="context"&amp;gt;&amp;lt;/param&amp;gt;&lt;br /&gt;
        protected DatabaseNotFoundException(SerializationInfo info, &lt;br /&gt;
            StreamingContext context)&lt;br /&gt;
            : base(info, context)&lt;br /&gt;
        {&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Very simple and straightforward. Now, what I'll do is go back to the first code block above, the one that opens a connection to a database supplied to it through an enumeration.&lt;br /&gt;
&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Open the database.&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name="tDatabaseType"&amp;gt;Database Type&amp;lt;/param&amp;gt;&lt;br /&gt;
        public void Open(DBType tDatabaseType)&lt;br /&gt;
        {&lt;br /&gt;
            try&lt;br /&gt;
            {&lt;br /&gt;
                _Connection.Open();&lt;br /&gt;
                _Command = new OleDbCommand();&lt;br /&gt;
                _Command.Connection = _Connection;&lt;br /&gt;
            }&lt;br /&gt;
            catch (OleDbException ex)&lt;br /&gt;
            {&lt;br /&gt;
                _lastError = ex.Message.ToString();&lt;br /&gt;
                throw new DatabaseNotFoundException("Could not connect to the database.", ex);&lt;br /&gt;
            }&lt;br /&gt;
            catch (Exception ex)&lt;br /&gt;
            {&lt;br /&gt;
                _lastError = ex.Message.ToString();&lt;br /&gt;
                throw;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Luckily for me, every call to Open is already in a try/catch even though it didn't throw an exception previously.&lt;br /&gt;
&lt;br /&gt;
Now when I get to call my error handler after this propagates up, I know there was an exception thrown and I know exactly why it was thrown. The user doesn't get strange SQL errors displayed, instead they get a simple "Could not connect to database" which they will understand, and I can now, very importantly, extract the underlying OleDbException from the DatabaseNotFoundException and get the debugging information I need for proper logging of errors.&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=113647"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=113647" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/ICodemonkey/aggbug/113647.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Chas Williams</dc:creator>
            <guid>http://geekswithblogs.net/ICodemonkey/archive/2007/07/03/113647.aspx</guid>
            <pubDate>Tue, 03 Jul 2007 14:05:26 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/ICodemonkey/comments/113647.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/ICodemonkey/archive/2007/07/03/113647.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/ICodemonkey/comments/commentRss/113647.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/ICodemonkey/services/trackbacks/113647.aspx</trackback:ping>
        </item>
        <item>
            <title>Nested String Statements</title>
            <link>http://geekswithblogs.net/ICodemonkey/archive/2007/06/25/113448.aspx</link>
            <description>Here's a strange one. Last night I had a dream about nested strings. The most obvious use of this would be in a nested SQL statement, so here's a made up on the fly example:&lt;br /&gt;
&lt;br /&gt;
select   phone_number&lt;br /&gt;
from Addressbook&lt;br /&gt;
where name = (select name from customers where city = 'Atlanta')&lt;br /&gt;
&lt;br /&gt;
But instead of writing that out as a string in my code, I did this crazy thing:&lt;br /&gt;
&lt;br /&gt;
string sQuery = "select name from customers where city = 'Atlanta'";&lt;br /&gt;
sQuery = " select   phone_number from Addressbook where name = (" + sQuery + ")";&lt;br /&gt;
&lt;br /&gt;
Mostly, I would never use that. It's far too difficult to go back and read later. However, I got to thinking and it might be interesting to use that with a dynamically created SQL statement. Say I was going to do a phone look up, but I wasn't sure what I was going to do the test against, I could use this little pattern. That way I could do a lookup versus city, zip code, PO numbers, or anything else, and it might actually be &lt;span style="font-style: italic;"&gt;easier &lt;/span&gt;to read in the future this way than with some kind of nested if statement that creates the SQL through conditional statements.&lt;br /&gt;
&lt;br /&gt;
I'll have to think on this a little more.&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=113448"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=113448" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/ICodemonkey/aggbug/113448.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>I, Codemonkey</dc:creator>
            <guid>http://geekswithblogs.net/ICodemonkey/archive/2007/06/25/113448.aspx</guid>
            <pubDate>Mon, 25 Jun 2007 13:11:27 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/ICodemonkey/comments/113448.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/ICodemonkey/archive/2007/06/25/113448.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/ICodemonkey/comments/commentRss/113448.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/ICodemonkey/services/trackbacks/113448.aspx</trackback:ping>
        </item>
    </channel>
</rss>