<?xml version="1.0" encoding="utf-8"?>
<feed xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xml:lang="en-us" xmlns="http://www.w3.org/2005/Atom">
  <title>VON#</title>
  <link rel="alternate" type="text/html" href="http://www.vonsharp.net/" />
  <link rel="self" href="http://www.vonsharp.net/SyndicationService.asmx/GetAtom" />
  <icon>favicon.ico</icon>
  <updated>2008-08-05T12:33:09.4629831-07:00</updated>
  <author>
    <name>Jon von Gillern</name>
  </author>
  <subtitle>Confessions of a code junkie</subtitle>
  <id>http://www.vonsharp.net/</id>
  <generator uri="http://www.dasblog.net" version="2.0.7180.0">DasBlog</generator>
  <entry>
    <title>Programming Job Interview Challenge Answer Week #14</title>
    <link rel="alternate" type="text/html" href="http://www.vonsharp.net/ProgrammingJobInterviewChallengeAnswerWeek14.aspx" />
    <id>http://www.vonsharp.net/PermaLink,guid,ae842809-9f83-4d9c-b547-9a6c3a2db694.aspx</id>
    <published>2008-08-05T12:33:09.4629831-07:00</published>
    <updated>2008-08-05T12:33:09.4629831-07:00</updated>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">Here is the question: <a href="http://www.dev102.com/2008/08/05/a-programming-job-interview-challenge-14-2d-geometry/">Programming
Job Interview Challenge Question Week #14</a><br /><br />
This week was a fun and interesting question. Here is the answer<br /><br />
Step 1. As you're adding points to the polygon, determine a bounding rectangle by
keeping track of the min x/y and the max x/y.<br /><br />
Step 2. Draw a line from the point you're interested in, to any point outside the
bounding rectangle (because you know the outside point can't be within the polygon).<br /><br />
Step 3. Count the number of intersections between your newly created line and the
polygon, if the number is even (0 included) the point in question is not within the
polygon, if the number of intersections is odd, then the point is within the polygon.<br /><br />
Examples:<br /><br />
Outside = Even Intersections<p></p><img src="http://www.vonsharp.net/content/binary/question14%20outside.jpg" border="0" /><br /><br />
Inside = Odd Intersections<br /><img src="http://www.vonsharp.net/content/binary/question14%20inside%20polygon.jpg" border="0" /></div>
    </content>
  </entry>
  <entry>
    <title>Programming Job Interview Challenge #13 Answer</title>
    <link rel="alternate" type="text/html" href="http://www.vonsharp.net/ProgrammingJobInterviewChallenge13Answer.aspx" />
    <id>http://www.vonsharp.net/PermaLink,guid,0dc5e23a-c3ec-49c0-9a3a-2a4ec9ce83b1.aspx</id>
    <published>2008-07-22T06:00:08.4719885-07:00</published>
    <updated>2008-07-22T06:00:08.4719885-07:00</updated>
    <category term="Stuff" label="Stuff" scheme="http://www.vonsharp.net/CategoryView,category,Stuff.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <a href="http://www.dev102.com/2008/07/21/a-programming-job-interview-challenge-13-brackets/">Programming
Job Interview Challenge Question Week #13</a>
        <br />
        <br />
Highlight the text below for the answer.<br /><font color="#a9a9a9"><br /></font><font color="#ffffff">This is pretty simple, use a stack to track all open
brackets and on all closed pop the stack to see if you have the correct matching bracket.<br /><br /></font><font color="#ffffff" face="Courier New" size="2">private bool AreBracketsClosedProperly(string
input)<br />
{<br />
    Stack&lt;char&gt; openBrackets = new Stack&lt;char&gt;();<br />
    foreach (char bracket in input)<br />
    {<br />
        switch (bracket)<br />
        {<br />
            case '(':<br />
            case '[':<br />
            case '&lt;':<br />
            case '{':<br />
               
openBrackets.Push(bracket);<br />
               
break;<br /><br />
            case ')':<br />
               
if (openBrackets.Pop() != ')')<br />
                   
return false;<br />
            case ']':<br />
               
if (openBrackets.Pop() != ']')<br />
                   
return false;<br />
            case '&gt;':<br />
               
if (openBrackets.Pop() != '&gt;')<br />
                   
return false;<br />
            case '}':<br />
               
if (openBrackets.Pop() != '}')<br />
                   
return false;<br />
               
break;<br />
        }<br />
    }<br /><br />
    if (openBrackets.Count != 0)<br />
        return false;<br />
    else<br />
        return true;<br />
}</font><br /><p></p></div>
    </content>
  </entry>
  <entry>
    <title>Programming Job Interview Challenge #10 Answer</title>
    <link rel="alternate" type="text/html" href="http://www.vonsharp.net/ProgrammingJobInterviewChallenge10Answer.aspx" />
    <id>http://www.vonsharp.net/PermaLink,guid,5c5bb1a9-91c3-4c04-b73c-815c14a34e8a.aspx</id>
    <published>2008-07-01T09:00:57.931-07:00</published>
    <updated>2008-07-01T09:01:16.4791419-07:00</updated>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">If you select the text below, you can find
the answer for this week's <a href="http://www.dev102.com/net/a-programming-job-interview-challenge-10-the-missing-number/">Job
Interview Challenge #10 from dev102.com</a>.<br /><br /><font color="#ffffff">This is a fairly easy problem, you can find the missing number
by taking the difference of the sum of the numbers you're given and what the total
should be for 1 to n. If you actually do the sum of 1 to n via a for loop, the time
complexity is O(2n), which is really just O(n), but if you want to get picky, you
can make it actually O(1n) by only looping through the list you're handed and instead
using the formula n(n+1)/2 to get the total of the numbers from 1 to n. 
<br /><br /></font><font color="#ffffff" face="Courier New" size="2">public static void FindMissingNumbers()<br />
{<br />
    //The O(n) that I discussed above is for 
<br />
    //the FindMissingNumber method only<br /><br />
    int n = 100;<br />
    List&lt;int&gt; numbers = CreateRandomList(n);<br />
    Console.WriteLine("Found:     Left Out Number
is: " + FindMissingNumber(numbers));<br /><br />
    n = 1000;<br />
    numbers = CreateRandomList(n);<br />
    Console.WriteLine("Found:     Left Out Number
is: " + FindMissingNumber(numbers));<br /><br />
    n = 10000;<br />
    numbers = CreateRandomList(n);<br />
    Console.WriteLine("Found:     Left Out Number
is: " + FindMissingNumber(numbers));<br /><br />
    //sample output<br />
    //Generated: Left Out Number is: 31<br />
    //Found:     Left Out Number is: 31<br />
    //Generated: Left Out Number is: 840<br />
    //Found:     Left Out Number is: 840<br />
    //Generated: Left Out Number is: 6289<br />
    //Found:     Left Out Number is: 6289<br /><br />
}<br /><br /><br /><br />
public static int FindMissingNumber(List&lt;int&gt; numbers)<br />
{<br />
    int numbersSum = numbers.Sum();<br />
    int n = numbers.Count;<br />
    int sum1ToNPlus1 = (n * (n + 1)) / 2; // this is much quicker than
actually summing 1 through n+1<br /><br />
    return sum1ToNPlus1 - numbersSum;<br />
}<br /><br />
public static List&lt;int&gt; CreateRandomList(int n)<br />
{<br />
    int nPlus1 = n + 1;<br />
    List&lt;int&gt; allNumbersList = new List&lt;int&gt;();<br />
    for (int i = 0; i &lt; nPlus1; i++)<br />
        allNumbersList.Add(i);<br /><br />
    Random rand = new Random();<br /><br />
    List&lt;int&gt; subsetNumbersList = new List&lt;int&gt;();<br />
    while (allNumbersList.Count &gt; 1)<br />
    {<br />
        int index = rand.Next(allNumbersList.Count);<br />
        subsetNumbersList.Add(allNumbersList[index]);<br />
        allNumbersList.RemoveAt(index);<br />
    }<br /><br />
    Console.WriteLine("Generated: Left Out Number is: " + allNumbersList[0]);<br /><br />
    return subsetNumbersList;<br />
}</font><p></p></div>
    </content>
  </entry>
  <entry>
    <title>Programming Job Interview Challenge #8 Answer</title>
    <link rel="alternate" type="text/html" href="http://www.vonsharp.net/ProgrammingJobInterviewChallenge8Answer.aspx" />
    <id>http://www.vonsharp.net/PermaLink,guid,1b069045-33ad-494b-b76e-875340e0fb38.aspx</id>
    <published>2008-06-16T10:48:03.488-07:00</published>
    <updated>2008-06-16T10:50:29.178745-07:00</updated>
    <category term="Stuff" label="Stuff" scheme="http://www.vonsharp.net/CategoryView,category,Stuff.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">I've been trying to keep up with the Job
Interview line of posts over at dev102.com, but unfortunately I've been running out
of time to do all of them. Anyway, this weeks question can be found <a href="http://www.dev102.com/net/a-programming-job-interview-challenge-8-a-needle-in-a-haystack/">here</a>.
Stop reading if you don't want the answer.<br /><br />
This problem can be solved using a Finite State Machine. The only thing you're going
to store is the current state of the machine, once you reach the end of the machine,
you'll alert. On initialization of the piping component, you'll build a finite state
machine for the given alert sequence. Every time you are given a message you check
what you should do with the state machine based on where you are currently at.<br /><br />
I was going to write out the code to do this, but it becomes a little tricky when
you have repeating data in your alert sequence because if you get a message you're
not expecting you don't necessarily want to reset the state machine to its initial
state. For example, if the alert sequence was "A, A, A, A, B", and your input is "A,
A, A, A, A, B", you don't want to reset the machine to the first state when you get
that 5th A, you want it to stay in its current state.<br /><br /><br /><img src="http://www.vonsharp.net/content/binary/Statemachine12.jpg" border="0" /></div>
    </content>
  </entry>
  <entry>
    <title>Quick Fix - Missing Using Statements</title>
    <link rel="alternate" type="text/html" href="http://www.vonsharp.net/QuickFixMissingUsingStatements.aspx" />
    <id>http://www.vonsharp.net/PermaLink,guid,8631367a-6e86-4e54-a217-5c4162b2dd22.aspx</id>
    <published>2008-06-05T11:03:01.2632668-07:00</published>
    <updated>2008-06-05T11:03:01.2632668-07:00</updated>
    <category term="Tips and Tricks" label="Tips and Tricks" scheme="http://www.vonsharp.net/CategoryView,category,Tips%2Band%2BTricks.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">Do yourself a favor, next time you have
a build error that says something like: The type or namespace name 'XYZ' could not
be found (are you missing a using directive or an assembly reference?). Double click
the error (which will highlight the unknown type) then hit Alt+Shift+F10. This will
bring up a dropdown that has all of the types that match that class name and hitting
enter will automatically add the appropriate using statement. Thank you Visual Studio!<br /><br /><br /><p></p></div>
    </content>
  </entry>
  <entry>
    <title>Programming Job Interview Challenge #4 Answer</title>
    <link rel="alternate" type="text/html" href="http://www.vonsharp.net/ProgrammingJobInterviewChallenge4Answer.aspx" />
    <id>http://www.vonsharp.net/PermaLink,guid,b432eb70-b3d2-4b60-9879-28e51533336a.aspx</id>
    <published>2008-05-19T10:48:47.86-07:00</published>
    <updated>2008-05-27T06:41:34.4432036-07:00</updated>
    <category term="Stuff" label="Stuff" scheme="http://www.vonsharp.net/CategoryView,category,Stuff.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">So one of the guys over at <a href="http://www.dev102.com">dev102.com</a> has
been posting job interview questions. While the most recent isn't the best interview
question in the world, I thought I'd share the answer if anyone is interested.<br /><br />
Here is the <a href="http://www.dev102.com/2008/05/19/a-programming-job-interview-challenge-4/">question</a>: <blockquote cite="Dev102" style="background-color: LightGray;"> How
would you implement the following method: Foo(7) = 17 and Foo(17) = 7. Any other input
to that method is not defined so you can return anything you want. Just follow those
rules: 
<ul><li>
Conditional statements (if, switch, …) are not allowed.</li><li>
Usage of containers (hash tables, arrays, …) are not allowed.</li></ul></blockquote><br />
The answer is crazy simple. Highlight the text below to reveal the answer.<br /><br /><font color="#ffffff">public int Foo(int x)<br />
{<br />
    return -1 * x + 24;<br />
}<br /><br />
My Algebra teacher would be so proud.</font><br /></div>
    </content>
  </entry>
  <entry>
    <title>DasBlog and the Web.Config Location Tag</title>
    <link rel="alternate" type="text/html" href="http://www.vonsharp.net/DasBlogAndTheWebConfigLocationTag.aspx" />
    <id>http://www.vonsharp.net/PermaLink,guid,b9340607-4494-47ae-b90c-85382bd1f571.aspx</id>
    <published>2008-05-18T09:02:55.771-07:00</published>
    <updated>2008-05-18T09:04:10.8344686-07:00</updated>
    <category term="Tips and Tricks" label="Tips and Tricks" scheme="http://www.vonsharp.net/CategoryView,category,Tips%2Band%2BTricks.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">So you may or may not have noticed that
I haven't posted anything for the past two months and that most of my links have been
broken. Well, the reason being is that I run a small intranet application for a group
I belonged to at Iowa State. When I moved the application over to <a href="http://www.discountasp.net">discountasp.net</a> the
application was living in a subdirectory of my vonsharp.net domain, and in doing so
I ran into all sorts of messy web.config problems.<br /><br />
Well, I think finally have solved all the issues and should be posting once or twice
a week from here on out. But just incase you're interested here is how I resolved
my issues.<br /><br />
In order to run a seperate web application in a subdirectory of your DasBlog directory
you'll need to do the following:<br /><br />
1. In your DasBlog web.config, wrap all of the configuration tag's children in one
single "location" tag, except for the configSections tag and the runtime tag. Your
dasBlog web.config should look something like:<br /><br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;">&lt;configuration&gt;
&lt;configSections&gt; ... &lt;/configSections&gt; &lt;runtime&gt; ... &lt;runtime&gt;
&lt;location path=<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">""</span> allowOverride=<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"true"</span>&gt;
&lt;newtelligence.ControlImages ... &gt; ... &lt;system.web ... &gt; ... &lt;/location&gt;
&lt;configuration&gt;</span></pre>
Note: DO NOT CHANGE THE PATH, LEAVE IT AS AN EMPTY STRING<br /><br />
2. Then in your new sub-directory web application's web.config you'll need to do the
same thing, and leave the path string empty as long as your web.config is actually
sitting in the sub directory. As I understand it, its possible to define this in your
dasBlog's web config but I'd recommend against it. 
<br /><br />
Here is the tricky part. The location tag for the sub-directory application acts just
like object inheritence. So your web application's web.config will inherit all of
the same configuration from its parent directory. Since you probably don't want dasBlog
running in your sub directory, you need to clear out it's http modules. So in your
new web app's web config add the following markup to the httpModules section<br /><br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;">&lt;httpModules&gt;
&lt;!-- gets rid of dasBlog Modules --&gt; &lt;remove name=<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"UrlMapperModule"</span>/&gt;
&lt;remove name=<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"TitleMapperModule"</span>/&gt;
&lt;remove name=<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"ProfileMapperModule"</span>/&gt;
&lt;remove name=<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"ControlImageModule"</span>/&gt;
&lt;remove name=<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"CompressionModule"</span>/&gt;
&lt;remove name=<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"IPBlackList"</span>/&gt;
&lt;/httpModules&gt;</span></pre>
Hope this helps anyone who is going through the same pain that I had.<br /><br /><p></p></div>
    </content>
  </entry>
  <entry>
    <title>Boost Your Productivity - Remove Firefox Quick Launch</title>
    <link rel="alternate" type="text/html" href="http://www.vonsharp.net/BoostYourProductivityRemoveFirefoxQuickLaunch.aspx" />
    <id>http://www.vonsharp.net/PermaLink,guid,c388f588-48ac-4c17-9b85-8c95b7144ad8.aspx</id>
    <published>2008-03-18T07:01:17.8954-07:00</published>
    <updated>2008-03-18T07:01:17.8954-07:00</updated>
    <category term="Tips and Tricks" label="Tips and Tricks" scheme="http://www.vonsharp.net/CategoryView,category,Tips%2Band%2BTricks.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">I have a big problem with launching firefox
everytime I have 20 seconds of downtime (like when I'm building a big solution, or
I'm waiting for a large file to load in my application). By the time I look at whatever
site I decide to go to, it sucks up more than the 20 seconds of downtime and costs
me a lot of time "context switching". 
<br /><br />
Something that has helped me with this problem: I took firefox off of my quick launch
bar and put it on the desktop. I now have to be much more deliberate in when I actually
need to use the "tubes".<br /><br /><p></p><img src="http://www.vonsharp.net/content/binary/removeQuickLaunch.jpg" border="0" /><br /><br /></div>
    </content>
  </entry>
  <entry>
    <title>Software Grandmaster</title>
    <link rel="alternate" type="text/html" href="http://www.vonsharp.net/SoftwareGrandmaster.aspx" />
    <id>http://www.vonsharp.net/PermaLink,guid,8dba1d96-07ed-496a-bf0d-1fe181376114.aspx</id>
    <published>2008-03-03T10:15:26.7677683-08:00</published>
    <updated>2008-03-03T10:15:26.7677683-08:00</updated>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">I just read this excellent essay by Jeff
Moser on his blog and thought I'd share it with all of you. I highly recommend it,
especially for students who are interested in a career in software development.<br /><br /><a href="http://www.moserware.com/2008/03/what-does-it-take-to-become-grandmaster.html">What
does it take to become a Software Grandmaster<br /></a><p></p></div>
    </content>
  </entry>
  <entry>
    <title>Most Everything the Immediate Window Can Do the Watch Window Can Do Better</title>
    <link rel="alternate" type="text/html" href="http://www.vonsharp.net/MostEverythingTheImmediateWindowCanDoTheWatchWindowCanDoBetter.aspx" />
    <id>http://www.vonsharp.net/PermaLink,guid,536dbbb8-ccd9-4251-9685-1dc83a95ef27.aspx</id>
    <published>2008-02-29T06:53:29.5955966-08:00</published>
    <updated>2008-02-29T06:53:29.5955966-08:00</updated>
    <category term="Tips and Tricks" label="Tips and Tricks" scheme="http://www.vonsharp.net/CategoryView,category,Tips%2Band%2BTricks.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">I saw a <a href="http://ohnull.com/blog/2008/02/27/immediate-window-a-forgotten-tool/">post</a> on <a href="http://ohnull.com/blog/2008/02/27/immediate-window-a-forgotten-tool/">oh
null!</a> that talks about how you can evaluate expressions within the immediate window,
whether you're simply retrieving a variable or performing a calculation on multiple
variables. I personally like to do all of this in the Watch Window because I can have
an easy history of what variables I've interrogated. Like the immediate window, you
can call Methods and display the results, but since methods can change state, they
will not execute automatically after you step into a new line of code, but there is
a nice little refresh button that will re-execute your method call. 
<br /><br /><img src="http://www.vonsharp.net/content/binary/watchWindow.jpg" border="0" /><br /><br />
As I understand it, the watch window is a wrapper around the simpler functionality
of the immediate window, so it also supports Intellisense. But there are some things
that you can only do in the immediate window, like the .load [dll] command which lets
you import another assembly for your use, which is pretty damn slick. I've only ever
really used that feature in one scenario: to load the Son of Strike assembly which
helps you get detailed information on your executable (very detailed call stack info,
very granular heap information, but kind of hard to use). 
<br /><br />
Another cool thing you can do in your immediate window: Declare new variables. Those
variables will have the same scope as the current breakpoint in execution, but the
ability opens the doors to some neat possibilities. For instance, you could declare
a new variable, copy a subset of information from your current in scope variables
and then use my <a href="http://www.vonsharp.net/PutDownTheXmlNodeAndStepAwayFromTheStringBuilder.aspx">Xml
Utility Methods</a> to copy your new object out to disk! I'm not quite sure why you
would need to do that, but its nice to be able to!<br /></div>
    </content>
  </entry>
  <entry>
    <title>Quick and Dirty Memory Utility Methods</title>
    <link rel="alternate" type="text/html" href="http://www.vonsharp.net/QuickAndDirtyMemoryUtilityMethods.aspx" />
    <id>http://www.vonsharp.net/PermaLink,guid,c8ff4f30-3dbd-427a-88ea-7252e5219a22.aspx</id>
    <published>2008-02-27T11:36:28.846-08:00</published>
    <updated>2008-02-27T11:58:07.0902221-08:00</updated>
    <category term="Tips and Tricks" label="Tips and Tricks" scheme="http://www.vonsharp.net/CategoryView,category,Tips%2Band%2BTricks.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">With my work, I regularly deal with obscene
amounts. Of course, our users demand that this information loads instantly and without
running out of memory. So optimizing our data storage objects for time and memory
constraints is incredibly important. So far, I think I've done a pretty good job,
because I can load 125 million data points in a shade over 3 seconds (using a database
to store some of this data flew out the window a while ago). But getting to this point
has been an interesting journey. Doing quick prototyping of prospective storage mechanisms
has been incredibly important and I thought I'd share some of the utility methods/classes
I've created to speed up the process.<br /><br />
The first piece of code is incredibly similar to my <a href="http://www.vonsharp.net/StopwatchWriterClass.aspx">StopwatchWriter</a> Class.
Instead of starting and stopping a Stopwatch, we're asking the garbage collector how
much total memory is being used (in bytes), before and after you instantiate an object
(or a whole set of objects)<br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> MemoryWriter
: IDisposable { <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">long</span> _startMem; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span> _text; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span> MemoryWriter(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span> text)
{ _text <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> text <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">+</span><span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"
- "</span>; _startMem <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> GC.GetTotalMemory(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">true</span>);
} <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">void</span> Dispose()
{ Console.WriteLine(<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"mem:
"</span><span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">+</span> _text <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">+</span> (GC.GetTotalMemory(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">true</span>) <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">-</span><br />
_startMem).ToString()); } } Usage looks like: <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> (<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> MemoryWriter(<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"CrazyBigObject"</span>))
{ CrazyBigObject myCrazyBigObject <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> LoadCrazyBigObject(42);
}</span></pre><br />
This is the most accurate way of figuring out how much memory a specific object is
taking up in memory. But there is a limitation to this method if you're using it in
a multi-threaded application, such as a Win Form because another running thread could
dereference objects on the heap after you've instantiated your MemoryWriter , but
before its been disposed. I prefer to only use this class in small throw away console
applications, to make my results as accurate as possible. 
<br /><br />
Sometimes though, you'll find yourself looking at an object in someone else's code
and you want to easily find out how much memory its taking up, but the object in question
is "built" over several methods along with several other objects that you don't care
about. Which makes it impossible to use theMemoryWriter Class. You can use a memory
profiling application to do this, but I've found that they are notoriously hard to
pick up and use. And due to the nature of how they work, they take ages to work because
they have to take a snapshot before and after the code you care about (i.e. copy your
700MB object heap twice and then "diff" the two heaps). So I've wrote two small methods
that serialize the object to a stream and then return the length of the stream. Note:
This is approximately how much data the object is holding in memory. It may besignificantly
less than how much space it takes up on the heap. Case in point: Dictionary&lt;K,
V&gt; takes up much more space in memory than it does when serialized.That's because
it only serialized the key value pairs and it re-hydrates the dictionary on deserialization.
Nor will it reflect the size of any properties on your object that are marked with
the NonSerialized attribute. So it is far from perfect, but it helps give you an idea
with out the pain of using a memory profiler . You can use these methods by setting
a break point and calling them in your watch window (I prefer doing that over the
immediate window).<br /><br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">///
&lt;summary&gt;</span><span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">///
Returns a rough approximation of the size of an object 
<br />
/// (including ALL objects in/directly referenced by the object)</span><span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">///
&lt;/summary&gt;</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">static</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">long</span> ApproxSize(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">object</span> obj)
{ BinaryFormatter formatter <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> BinaryFormatter(); <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">long</span> length; <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> (MemoryStream
stream <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> MemoryStream())
{ formatter.Serialize(stream, obj); stream.Seek(0, SeekOrigin.Begin); length <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> stream.Length;
} <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span> length;
} </span></pre><br />
Sometimes though, your object is very large and serializing the object in memory will
cause an OutOfMemory Exception. So use the below method instead, it serializes the
object to disk and gives you the size of the file (and deletes the file after its
done). 
<br /><br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Green; background-color: transparent; font-family: Courier New; font-size: 11px;">///
&lt;summary&gt;<br />
/// Returns the approximate size of very large objects 
<br />
/// (time intensive) in bytes.<br />
/// &lt;/summary&gt;</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">static</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">long</span> ApproxSizeLarge(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">object</span> obj)
{ FileInfo info <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> FileInfo(<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">@"c:\approxSizeTemp"</span>);
BinaryFormatter formatter <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> BinaryFormatter(); <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> (StreamWriter
writer <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> StreamWriter(<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">@"c:\approxSizeTemp"</span>))
{ formatter.Serialize(writer.BaseStream, obj); } <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">long</span> length <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span> info.Length;
info.Delete(); <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span> length;
}</span></pre><br /><p></p></div>
    </content>
  </entry>
  <entry>
    <title>What The Hell?</title>
    <link rel="alternate" type="text/html" href="http://www.vonsharp.net/WhatTheHell.aspx" />
    <id>http://www.vonsharp.net/PermaLink,guid,f7bea287-6eb1-4e1e-8965-98a7c8eafe7a.aspx</id>
    <published>2008-02-16T09:07:21.8351861-08:00</published>
    <updated>2008-02-16T09:07:21.8351861-08:00</updated>
    <category term="Stuff" label="Stuff" scheme="http://www.vonsharp.net/CategoryView,category,Stuff.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">So I was never really expecting to make
much money from my AdSense advertisements, if I made $10 in a year, I'd be happy.
But perhaps I've only made 80 cents thus far because Google is serving absurd ads
like:<br /><br /><p></p><img src="http://www.vonsharp.net/content/binary/crappy%20adsense%20ad.png" border="0" /><br /><br />
What AdWords did these people buy that is displayed on my site talking about Microsoft
Technologies? Is this some sort of joke perpetrated the FOSS Commie Zealots? My content
might be a little crappy (<a href="http://www.dotnetkicks.com/zeitgeist/2008/2">or
not</a>) but C#, WPF and LINQ are simply bad-ass.<br /></div>
    </content>
  </entry>
  <entry>
    <title>Put Down the XmlNode and Step Away From the StringBuilder</title>
    <link rel="alternate" type="text/html" href="http://www.vonsharp.net/PutDownTheXmlNodeAndStepAwayFromTheStringBuilder.aspx" />
    <id>http://www.vonsharp.net/PermaLink,guid,01d51ea8-2072-40fc-9dc9-dc75b078732d.aspx</id>
    <published>2008-02-12T06:29:33.338-08:00</published>
    <updated>2008-02-12T10:19:13.9939801-08:00</updated>
    <category term="Tips and Tricks" label="Tips and Tricks" scheme="http://www.vonsharp.net/CategoryView,category,Tips%2Band%2BTricks.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">I'm sure there are plenty of you out there
who have your boss hankering to use some of that "Xml Stuff". Sometimes we get sheltered
in our own little world and haven't actually had to work with a whole lot of xml data
that wasn't already wrapped up by the Project Settings object created by Visual Studio.
So naturally, you'd probably go to the "<a title="tubes" href="http://www.urbandictionary.com/define.php?term=intertubes" id="t59e">tubes</a>"
and search for some additional information. Undoubtedly, you will run across pages
telling you how to construct some Xml in C# by creating a new XmlDocument and adding
XmlNode children which will have attributes and subnodes and namespaces, ad infinitum.
And you'll realize you now have some of the world's fugliest code. So you decide,
"oh, well xml is really similar to html, I'll just build it using a StringBuilder"
and you end up with slightly less fugly code, that is until you try to read it back
in.<br /><br />
These methods are completely unnecessary. PUT DOWN THE XMLNODE AND STEP AWAY FROM
THE STRINGBUILDER. Slowly move your hand to the mouse and scroll down to read about
the easiest way to write <b>and </b>read Xml in the .net framework. 
<br /><br />
Ok, quick show of hands: who thinks working with simple .net business model objects
is brain dead easy? Alright, if your hand isn't up, you are beyond hope, please leave
now. Of course, working with simple objects is about the simplest exercise for code
next to "Hello World". If you can create a .net object that models the xml you want
to store, you are 95% done with outputting well formed Xml. 
<br /><br />
Example: If I wanted to store information about a few people and their pets I would
create three classes:<br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> PetClub<br />
{<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span> List&lt;Person&gt;
Members { get; set; }<br />
}<br /><br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> Person<br />
{<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span> Name
{ get; set; }<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span> List&lt;Pet&gt;
Pets { get; set; }<br />
}<br /><br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> Pet<br />
{<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span> Name
{ get; set; }<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span> Type
{ get; set; }<br />
}</span></pre>
To get an Xml representation of an instance of PetClub, all you have to do is use
the XmlSerializer found in the System.Xml.Serialization namespace. I've wrapped up
all the necessary code into a short utility method.<br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">static</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span> ConvertToXml(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">object</span> item)<br />
{<br />
XmlSerializer xmlser <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> XmlSerializer(item.GetType());<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> (System.IO.MemoryStream
ms <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> System.IO.MemoryStream())<br />
{<br />
xmlser.Serialize(ms, item);<br />
UTF8Encoding textconverter <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> UTF8Encoding();<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span> textconverter.GetString(ms.ToArray());<br />
}<br />
}</span></pre><br />
Simply calling: ConvertToXml(myPetClubInstance) will spit back the following xml:<br /><br /><font face="Courier New">&lt;PetClub xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;<br />
  &lt;Members&gt;<br />
    &lt;Person&gt;<br />
      &lt;Name&gt;Jon&lt;/Name&gt;<br />
      &lt;Pets&gt;<br />
        &lt;Pet&gt;<br />
          &lt;Name&gt;Chester&lt;/Name&gt;<br />
          &lt;Type&gt;Savannah Cat&lt;/Type&gt;<br />
        &lt;/Pet&gt;<br />
        &lt;Pet&gt;<br />
          &lt;Name&gt;Abby&lt;/Name&gt;<br />
          &lt;Type&gt;Domestic Miniature
Panther&lt;/Type&gt;<br />
        &lt;/Pet&gt;<br />
      &lt;/Pets&gt;<br />
    &lt;/Person&gt;<br />
    &lt;Person&gt;<br />
      &lt;Name&gt;Dan&lt;/Name&gt;<br />
      &lt;Pets&gt;<br />
        &lt;Pet&gt;<br />
          &lt;Name&gt;Lucy&lt;/Name&gt;<br />
          &lt;Type&gt;Semi-sweet Chocolate
Lab&lt;/Type&gt;<br />
        &lt;/Pet&gt;<br />
      &lt;/Pets&gt;<br />
    &lt;/Person&gt;<br />
  &lt;/Members&gt;<br />
&lt;/PetClub&gt;</font><br /><br />
At this point you might be saying: "That is cool and all, Jon, but this looks like
a lot of xml to be outputting for such little actual data. And what if I want to have
more control over xml element naming". Dear friend, let not your heart be troubled
for there is help on the way. By adding some simple Property Attributes to our classes
we can completely change how our xml is constructed.  We can have the object
in whatever form is most convienient for our program while still allowing us to interoperate
with an xml document created by a different application with a different idea of what
good names are.<br /><br />
Changing our two classes to:<br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> Person<br />
{<br />
[XmlElement(<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"FirstName"</span>)]<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span> Name
{ get; set; }<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span> List&lt;Pet&gt;
Pets { get; set; }<br />
}<br /><br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">class</span> Pet<br />
{<br />
[XmlAttribute(<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"Name"</span>)]<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span> Name
{ get; set; }<br />
[XmlAttribute(<span style="color: rgb(102, 102, 102); background-color: rgb(228, 228, 228); font-family: Courier New; font-size: 11px;">"Breed"</span>)]<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span> Type
{ get; set; }<br />
} </span></pre><br />
Will output this xml:<br /><br /><font face="Courier New"> &lt;PetClub xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;<br />
  &lt;Members&gt;<br />
    &lt;Person&gt;<br />
      &lt;FirstName&gt;Jon&lt;/FirstName&gt;<br />
      &lt;Pets&gt;<br />
        &lt;Pet Name="Chester" Breed="Savannah
Cat" /&gt;<br />
        &lt;Pet Name="Abby" Breed="Domestic Miniature
Panther" /&gt;<br />
      &lt;/Pets&gt;<br />
    &lt;/Person&gt;<br />
    &lt;Person&gt;<br />
      &lt;FirstName&gt;Dan&lt;/FirstName&gt;<br />
      &lt;Pets&gt;<br />
        &lt;Pet Name="Lucy" Breed="Semi-sweet Chocolate
Lab" /&gt;<br />
      &lt;/Pets&gt;<br />
    &lt;/Person&gt;<br />
  &lt;/Members&gt;<br />
&lt;/PetClub&gt;</font><br /><br /><br />
Pretty simple, huh? Can you imagine how difficult it would be to make these changes
via XmlNodes or a StringBuilder? There are about 10 other property attributes available
for xml serialization, including the very useful XmlIgnore() attribute which makes
the appropriate property not serialized. I'll leave the rest of the research up to
you. 
<br /><br />
And I almost forgot, to read your xml back into an object use the following method:<br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">public</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">static</span> T
FromXml&lt;T&gt;(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span> xml)<br />
{<br />
XmlSerializer xmlser <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> XmlSerializer(<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">typeof</span>(T));<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">using</span> (System.IO.StringReader
sr <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">=</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">new</span> System.IO.StringReader(xml))<br />
{<br /><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">return</span> (T)xmlser.Deserialize(sr);<br />
}<br />
}<br /></span></pre><h1><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"></span></h1><p></p>
Sample Code - <a href="http://www.vonsharp.net/content/binary/XmlSerializerSample.%5Brename%20to%20cs%5D" temp_href="http://www.vonsharp.net/content/binary/XmlSerializerSample.[rename to cs]">XmlSerializerSample.cs
(2.33 KB)</a><br /><br /></div>
    </content>
  </entry>
  <entry>
    <title>Custom Created Theme</title>
    <link rel="alternate" type="text/html" href="http://www.vonsharp.net/CustomCreatedTheme.aspx" />
    <id>http://www.vonsharp.net/PermaLink,guid,fa13cb82-1f8d-4fec-bf61-10df57731a11.aspx</id>
    <published>2008-02-10T21:47:16.093-08:00</published>
    <updated>2008-02-11T08:14:31.3890088-08:00</updated>
    <category term="Stuff" label="Stuff" scheme="http://www.vonsharp.net/CategoryView,category,Stuff.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">Well, as you can tell I've got a new DasBlog
theme I created myself. I'm not the most artistic person in the world, but I'm happy
with what I've accomplished. Good lord did it take a long time, its been a while since
I've pounded out HTML and CSS, but I'm glad to have it fresh in my head again but
I'm totally disgusted at how differently IE and Firefox render, what a gigantic waste
of time. 
<br /><br />
Have a good week!<br /><p></p></div>
    </content>
  </entry>
  <entry>
    <title>Damn It Feels Good To Write Unit Tests Again</title>
    <link rel="alternate" type="text/html" href="http://www.vonsharp.net/DamnItFeelsGoodToWriteUnitTestsAgain.aspx" />
    <id>http://www.vonsharp.net/PermaLink,guid,482fcfaa-f3c6-4a1f-a21c-b3cf7f0e7a1b.aspx</id>
    <published>2008-02-08T10:45:33.0883108-08:00</published>
    <updated>2008-02-08T10:45:33.0883108-08:00</updated>
    <category term="Stuff" label="Stuff" scheme="http://www.vonsharp.net/CategoryView,category,Stuff.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">That is all.<br /><p></p></div>
    </content>
  </entry>
  <entry>
    <title>Most Useful VS Feature No One Knows About</title>
    <link rel="alternate" type="text/html" href="http://www.vonsharp.net/MostUsefulVSFeatureNoOneKnowsAbout.aspx" />
    <id>http://www.vonsharp.net/PermaLink,guid,1cc24b4b-cfdb-46f2-af0d-320ce151fb19.aspx</id>
    <published>2008-02-07T09:23:45.488-08:00</published>
    <updated>2008-02-07T09:56:25.9166187-08:00</updated>
    <category term="Tips and Tricks" label="Tips and Tricks" scheme="http://www.vonsharp.net/CategoryView,category,Tips%2Band%2BTricks.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">It really does shock and amaze me how many
developers get through the day without knowing the wonders of the Exceptions Dialog
box. You can access it via Debug&gt;Exceptions or you can hit Ctrl-Alt-E. What this
dialog box allows you do is to pick a set of exceptions (or all of them for that matter)
and the debugger will automatically break when the exception is thrown. This allows
you to inspect your call stack and interrogate your variables to determine the state
of your application when the exception occurred. No longer do you have to look through
an error message to find a line number and start setting break points. This becomes
especially helpful if the error is occurring in a gigantic loop and your not sure
how many times the loop executes before it runs into a null reference exception. It
breaks automatically and voila, you can easily see all the variables that are associated
with your null object (or whatever the error may be).<br /><br />
Sometimes you'll be responsible for code, that for one reason or another uses exception
catching/handling as a normal part of code flow. This usually is considered a bad
practice but sometimes you don't have a choice under the circumstances. When that
happens, it is very easy to turn off custom typed exceptions, just click the "Add..."
button and type in the fully qualified name of the exception you don't want to see
every time it is thrown. As you can see in my screenshot, I've done this with the
Sybase.Data.AseClient.AseException exception. 
<br /><br />
I hope this helps speed up any future debugging.<br /><br /><br /><p></p><img src="http://www.vonsharp.net/content/binary/ExceptionsDialogBox2.jpg" border="0" /></div>
    </content>
  </entry>
  <entry>
    <title>Patterns</title>
    <link rel="alternate" type="text/html" href="http://www.vonsharp.net/Patterns.aspx" />
    <id>http://www.vonsharp.net/PermaLink,guid,460688c0-11c1-477f-87cb-855cdba1ccc7.aspx</id>
    <published>2008-02-04T06:46:19.0336373-08:00</published>
    <updated>2008-02-04T06:46:19.0336373-08:00</updated>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">I just found this site off of dotnetkicks,
and it looks like a great reference site for information on patterns and anti-patterns<br /><br /><a href="http://sourcemaking.com/">http://sourcemaking.com/</a><br /><p></p></div>
    </content>
  </entry>
  <entry>
    <title>Dictionary Naming Guidelines</title>
    <link rel="alternate" type="text/html" href="http://www.vonsharp.net/DictionaryNamingGuidelines.aspx" />
    <id>http://www.vonsharp.net/PermaLink,guid,4cc182ab-3611-4d0f-bea4-3a921e71dd16.aspx</id>
    <published>2008-01-23T08:05:02.233-08:00</published>
    <updated>2008-02-13T08:45:52.3580099-08:00</updated>
    <category term="Tips and Tricks" label="Tips and Tricks" scheme="http://www.vonsharp.net/CategoryView,category,Tips%2Band%2BTricks.aspx" />
    <category term="Wishlist" label="Wishlist" scheme="http://www.vonsharp.net/CategoryView,category,Wishlist.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">Generic dictionaries are a great thing,
they let you add and retrieve objects very quickly. My current gig has a lot of need
for them because we've got tons of data that needs to be quickly referenced (I'll
post more about Dictionary memory performance later). The problem is that developers
everywhere like to use very un-descriptive names for their dictionaries, especially
nested dictionaries. Usually you'd like to avoid nesting these things, but sometimes
you don't have a choice and making a bunch of derived classes adds a lot of code and
more "stuff" you have to sift through.<br /><br />
Simple Example:<br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;">Dictionary&lt;<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">int</span>, <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span>&gt;
_peopleDictionary;</span></pre>
What is the key? Is it a PersonId? Is it a CustomerId? Is it an EmployeeId? Is it
a Social Security Number?<br /><br />
What about the value? Is it First Name, Last Name, First then Last, Last then First,
Nickname?<br /><br />
In order to find out, a developer who takes over your code has to look up all references
to PeopleDictionary and find out what the Key and Values are, yielding a gigantic
waste of time. (Worse yet, it could be you looking up all the references because you
haven't looked at this particular code in 7 months). And that is even a non-nested
dictionary!<br /><br />
Now, can you tell me what kind of data is stored in the below object?<br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;">Dictionary&lt;<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">int</span>,
Dictionary&lt;<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">int</span>,
List&lt;<span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">string</span>&gt;&gt;&gt;
_customerIdToOrderIdToDescriptions;</span></pre>
Sure the name is a little long, but think about how many different kinds of data are
being stored in it. Plus you didn't have to make a custom object for a one-off need.
Anyone who has to modify your code can easily use this object without trying to figure
out what "OrdersDictionary" is actually storing. 
<br /><br />
FXCop Guys: I'll give you $10 if you add a rule saying that generic dictionary variable
names have to contain the string literal "To".<br /><br />
Of note: it's generally considered bad practice to use nested generics, but if you
do, just make sure that you don't expose nested generic types by returning them in
public methods or by making a public property with a nested type. Keep them for internal
use within your class only.<br /><br /><p></p></div>
    </content>
  </entry>
  <entry>
    <title>Free Copy of VS 2008</title>
    <link rel="alternate" type="text/html" href="http://www.vonsharp.net/FreeCopyOfVS2008.aspx" />
    <id>http://www.vonsharp.net/PermaLink,guid,a3fb8bcc-9e2e-478b-9a73-a4aad99141b0.aspx</id>
    <published>2008-01-18T11:33:04.6587079-08:00</published>
    <updated>2008-01-18T11:33:04.6587079-08:00</updated>
    <category term="Stuff" label="Stuff" scheme="http://www.vonsharp.net/CategoryView,category,Stuff.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">Register and attend for your local launch
event and receive a free copy of Visual Studio 2008, SQL Server 2008 AND Windows Server
2008. Which by my estimate is at least $2000 in free software.<br /><br /><a href="http://www.microsoft.com/heroeshappenhere/register/default.mspx">http://www.microsoft.com/heroeshappenhere/register/default.mspx</a><br /><br />
(If you're feeling generous, I would accept a 10% finders fee)<br /><p></p></div>
    </content>
  </entry>
  <entry>
    <title>StopwatchWriter Class</title>
    <link rel="alternate" type="text/html" href="http://www.vonsharp.net/StopwatchWriterClass.aspx" />
    <id>http://www.vonsharp.net/PermaLink,guid,beb68c16-4e73-4dca-92c0-0dbfb35ddb85.aspx</id>
    <published>2008-01-15T07:06:25.111-08:00</published>
    <updated>2008-01-15T19:08:01.2994764-08:00</updated>
    <category term="Tips and Tricks" label="Tips and Tricks" scheme="http://www.vonsharp.net/CategoryView,category,Tips%2Band%2BTricks.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">Do you ever get sick of having to write
4 whole lines for timing certain parts of your code. I know 4 lines isn't that bad,
but they multiply quickly when you're testing a lot of different code, or different
parts of the same method. That is why I came up with a StopwatchWriter class, it implements
IDisposable so you can use it in a using statement which reduces the needed code to
one line, put at the top of the code your testing (as opposed to before to setup and
after to write). The constructor starts a stopwatch and when the Dispose method gets
called, it stops the stopwatch and writes the time to the console, but it could easily
be changed to write any type of log.<br /><br />
public class StopwatchWriter : IDisposable<br />
{<br />
    Stopwatch _stopwatch = new Stopwatch();<br />
    string _text;<br /><br />
    public StopwatchWriter(string text)<br />
    {<br />
        _text = text + " - ";<br />
        _stopwatch.Start();<br />
    }<br /><br />
    public void Dispose()<br />
    {<br />
        _stopwatch.Stop();<br />
        Console.WriteLine("stopw: "+_text + _stopwatch.ElapsedMilliseconds);<br />
    }<br />
}<br /><br />
Usage looks like:<br /><br />
using (new StopwatchWriter("populateStuff"))<br />
{<br />
    this.PopulateStep1();<br />
    this.PopulateStep2();<br />
}<br /><br />
-or-<br /><br />
using (new StopwatchWriter("doStuff"))<br />
    DoStuff();<br /><br /><br /><br /><p></p></div>
    </content>
  </entry>
  <entry>
    <title>Error Creating Window Handle</title>
    <link rel="alternate" type="text/html" href="http://www.vonsharp.net/ErrorCreatingWindowHandle.aspx" />
    <id>http://www.vonsharp.net/PermaLink,guid,047ace4f-12ab-4486-b6d8-ca7ce6722e7a.aspx</id>
    <published>2008-01-09T07:48:59.3624871-08:00</published>
    <updated>2008-01-09T07:48:59.3624871-08:00</updated>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">So I was getting this real sweet "Error
Creating Window Handle" exception in one of my apps when I went to ".Show()" a window
in the app (not during the form's construction". I found that if I commented out attaching
a DataSource to the a combobox, showing the form worked fine. After reading a lot
about window handles and GDI objects and getting no where, lots of trial and error
commenced. Apparently Combobox does not like having the "DisplayMember" property set
after the datasource is set, but only if one of the objects in the datasource has
null for its DisplayMember property. (The DisplayMember was a property called "Name",
if one of the objects had null for its "Name" property, it blows up). But apparently
if you set the DisplayMember BEFORE you set your datasource, it is ok to have a displaymember
prop be null. Oy veh.<br /><p></p></div>
    </content>
  </entry>
  <entry>
    <title>Accessing The Control Responsible For A ContextMenuStrip</title>
    <link rel="alternate" type="text/html" href="http://www.vonsharp.net/AccessingTheControlResponsibleForAContextMenuStrip.aspx" />
    <id>http://www.vonsharp.net/PermaLink,guid,f146f701-49d6-4d33-92bf-9229aa4542ad.aspx</id>
    <published>2007-12-27T15:06:29.4692146-08:00</published>
    <updated>2007-12-27T15:06:29.4692146-08:00</updated>
    <category term="Tips and Tricks" label="Tips and Tricks" scheme="http://www.vonsharp.net/CategoryView,category,Tips%2Band%2BTricks.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">It certainly isn't easy to access what winform
control was ultimately responsible for bringing up a ContextMenuStrip, but it is indeed
possible, there is just some casting involved :)<br /><br /><br />
private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)<br />
{<br />
    if (((ContextMenuStrip)((ToolStripMenuItem)sender).Owner).SourceControl
== 
<br />
        this.PrimeNumbersCheckBoxList)<br />
    {<br />
        this.SelectAllThePrimeNumbers();<br />
    }<br />
    
<br />
    if (((ContextMenuStrip)((ToolStripMenuItem)sender).Owner).SourceControl
== 
<br />
        this.CompositeNumbersCheckBoxList)<br />
    {<br />
        this.SelectAllTheCompositeNumbers();<br />
    }<br />
}<br /><br /><br /><p></p></div>
    </content>
  </entry>
  <entry>
    <title>How To Easily Add A DotNetKicks Button To DasBlog </title>
    <link rel="alternate" type="text/html" href="http://www.vonsharp.net/HowToEasilyAddADotNetKicksButtonToDasBlog.aspx" />
    <id>http://www.vonsharp.net/PermaLink,guid,a1f97c5a-0ac1-48e4-a6ea-519f912ff37f.aspx</id>
    <published>2007-12-17T18:03:54.2243273-08:00</published>
    <updated>2007-12-17T18:03:54.2243273-08:00</updated>
    <category term="Tips and Tricks" label="Tips and Tricks" scheme="http://www.vonsharp.net/CategoryView,category,Tips%2Band%2BTricks.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">It is incredibly easy to add a <a href="http://www.dotnetkicks.com/">DotNetKicks</a> "Kick
It" Button to any DasBlog website. Simply navigate to your "themes" directory and
open up the "itemTemplate.blogtemplate" file with text editor. And simply paste the
following HTML wherever you'd like the button to display.<br /><br />
&lt;a href="http://www.dotnetkicks.com/kick/?url=&lt;%PermalinkUrl%&gt;"&gt;&lt;img
src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=&lt;%PermalinkUrl%&gt;"
border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;<br /><br />
Pretty simple, huh? Thanks to <a href="http://www.jforsythe.com/">John Forsythe</a> for
posting all the available <a href="http://www.jforsythe.com/jforsythe/projects/dasBlogMacros.html">DasBlog
Macros</a>.<br /><br />
If you're thinking about starting a blog, I highly recommend <a href="http://www.dasblog.info/">DasBlog</a>,
it was a very easy to setup, especially since it doesn't require a database! That
makes the cost of hosting a blog at <a href="http://www.discountasp.net/index.aspx?refcode=VONSHARP">DiscountAsp.net</a> only
$10/month! Subtext on the other hand needs a database to store and will run you $20
a month. I don't really know why DasBlog doesn't advertise that fact more, I would
have tried installing it first, is a relational database really that necessary for
a blog? It also makes it brain dead simple to backup your blog or move hosting providers.
I know because I started out hosting my blog at 1and1 (stay away!), I've been with <a href="http://www.discountasp.net/index.aspx?refcode=VONSHARP">DiscountAsp.net</a> for
a couple months and absolutely love it. Everything just works, I copied my DasBlog
files over and I was done. Plus its cheap and since all they do is focus on Microsoft
technologies, they always have the latest and greatest beta stuff running as soon
as it comes out (like .Net 3.5 and IIS 7.0).<p></p></div>
    </content>
  </entry>
  <entry>
    <title>Zune - Does NOT Play For Sure</title>
    <link rel="alternate" type="text/html" href="http://www.vonsharp.net/ZuneDoesNOTPlayForSure.aspx" />
    <id>http://www.vonsharp.net/PermaLink,guid,87601a25-bdc8-45e5-a3ba-d2c0e62acf8a.aspx</id>
    <published>2007-12-13T20:53:46.713-08:00</published>
    <updated>2007-12-15T07:27:34.9408906-08:00</updated>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">So I was totally stoked when I won my new
Zune last night, I've been using <a href="http://www.rhapsody.com">Rhapsody</a> for
quite some time and have really enjoyed it thus far, but I was looking forward to
using a device with it other than my Sansa e260. So tonight I fire up the rhapsody
client app on my desktop, plug in my new Zune, and there is no "device" icon popping
up in rhapsody (like there is with my Sansa). I think: oh, I wonder if I have to install
the Zune software first for some bizzare driver, then the rhapsody client will be
able to talk with my Zune. I install the Zune software, setup everything as necessary
and then start the rhapsody client. Nothing, still. Shit. I google "Zune Plays For
Sure", thinking that surely, Microsoft after spending millions of dollars coming up
with <a href="http://en.wikipedia.org/wiki/Microsoft_PlaysForSure">PlaysForSure</a>.
But no, Zune only works with the Zune Marketplace, not even music from the MSN MUSIC
STORE will play on a Zune. 
<br /><br />
Microsoft: I really am a big fan, a "fanboy" would probably be an acurate description
of my opinion on your company. I know that you're going through kind of an image problem,
people don't seem to be (or say at least) that they're usually satisfied with you
as a company. Here is why: people have a bad experience with one of your products
and that one experience colors their entire perception. It sucks to be in that position
because it puts a lot of pressure to have everything be top-notch, a difficult glass
to fill. I'm still a "fanboy". Your office productivity tools: Second-To-None. Your
developer tools: Awesome. Your development frameworks: Magnificent. Your gaming console:
Pretty Bad Ass. I know that having the best product isn't going to be possible, but
for the love of God, having the Zune support PlaysForSure is a NO BRAINER. This has
really left a bad taste in my mouth, I'm going gargle with some Minty <a href="http://www.microsoft.com/expression/products/overview.aspx?key=blend">Blend</a> to
get rid of it. 
<p></p></div>
    </content>
  </entry>
  <entry>
    <title>Game Of Life Demo</title>
    <link rel="alternate" type="text/html" href="http://www.vonsharp.net/GameOfLifeDemo.aspx" />
    <id>http://www.vonsharp.net/PermaLink,guid,35b305da-fe25-4dcd-be1b-051a956d021e.aspx</id>
    <published>2007-12-13T07:25:18.3562952-08:00</published>
    <updated>2007-12-13T07:25:18.3562952-08:00</updated>
    <category term="IADNUG" label="IADNUG" scheme="http://www.vonsharp.net/CategoryView,category,IADNUG.aspx" />
    <category term="Projects" label="Projects" scheme="http://www.vonsharp.net/CategoryView,category,Projects.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <img src="http://www.vonsharp.net/content/binary/gameOfLifeThumbnail.jpg" align="left" border="0" />Thanks
to everyone who came to the <a href="http://www.IADNUG.org">IADNUG</a> install-fest
last night. I received a 8GB Zune and second place with my Game of Life Demo. Thanks
to Microsoft and <a href="http://www.benkotips.com">Mike Benkovich</a> for supplying
the prizes, I thoroughly look forward to using my new Zune. Mike should have a post
at <a href="http://www.benkotips.com">BenkoTIPS</a> that will link to the other contestants
projects (if they're allowed to release them).<br /><br />
As promised, I've posted a link to the code I demo'd last night. It's a little messy
because I was more concerned with getting functionality and a pretty front-end than
in producing pretty code. I do, however intend on continuing work on it as a hobby
project, I think I've got a lot of neat ideas for new directions to take the project.
So if you're interested in staying updated, either subscribe to my "Projects" RSS
feed, or feel free to leave a comment with your email address and I'll be sure to
send you a note when I post a new version.<br /><br /><br /><br />
Thanks again to everyone who came.<br /><br /><a href="http://www.vonsharp.net/content/binary/GameOfLifeWpf.zip">GameOfLifeWpf.zip
(191.98 KB)</a><br /><br />
Note: When launching the 3D Form, you have to click on the "Cube" button first, before
you can hit start.<br /></div>
    </content>
  </entry>
  <entry>
    <title>Blend Tutorial Videos</title>
    <link rel="alternate" type="text/html" href="http://www.vonsharp.net/BlendTutorialVideos.aspx" />
    <id>http://www.vonsharp.net/PermaLink,guid,e58a296c-1a6c-4bf6-92f7-12c5e06e3ea7.aspx</id>
    <published>2007-12-11T18:18:52.008415-08:00</published>
    <updated>2007-12-11T18:18:52.008415-08:00</updated>
    <category term="Blend" label="Blend" scheme="http://www.vonsharp.net/CategoryView,category,Blend.aspx" />
    <category term="IADNUG" label="IADNUG" scheme="http://www.vonsharp.net/CategoryView,category,IADNUG.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">I've been working a lot lately on a demo
for tomorrow night's <a href="http://www.iadnug.org">VS 2008 Installfest Event</a>.
I plan on posting my code after the demo (because I probably won't be done until right
before I have to present), but I wanted to let everyone know what tutorials I used
to help create my demo. Expression Blend is frickin awesome, I had been dabling in
WPF a little bit, but not a whole lot was "clicking", but I had about 12 "A-HA" moments
while watching these videos. Highly recommended, especially if you're visual learner.<br /><br /><a href="http://channel9.msdn.com">Channel 9</a>: Introduction to Blend (<a href="http://channel9.msdn.com/Showpost.aspx?postid=286758">Part
1</a>) (<a href="http://channel9.msdn.com/Showpost.aspx?postid=286763">Part 2</a>)
(<a href="http://channel9.msdn.com/ShowPost.aspx?PostID=286767">Part 3</a>) (<a href="http://channel9.msdn.com/ShowPost.aspx?PostID=289325">Part
4</a>)<br /><br />
Way to go Mike Grayson and Richard Godfrey.<br /><p></p></div>
    </content>
  </entry>
  <entry>
    <title>Black vs White Text</title>
    <link rel="alternate" type="text/html" href="http://www.vonsharp.net/BlackVsWhiteText.aspx" />
    <id>http://www.vonsharp.net/PermaLink,guid,43c3d6b1-6525-4c36-9a44-c270815897a9.aspx</id>
    <published>2007-12-06T20:01:31.845-08:00</published>
    <updated>2008-01-15T07:27:31.0579158-08:00</updated>
    <category term="Design" label="Design" scheme="http://www.vonsharp.net/CategoryView,category,Design.aspx" />
    <category term="Tips and Tricks" label="Tips and Tricks" scheme="http://www.vonsharp.net/CategoryView,category,Tips%2Band%2BTricks.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">Whether it is a heat map or a simple legend
on a graph, color has the uncanny ability to instantly convey to users the intricacies
of their data. But like most powerful constructs, color can easily be <a href="http://www.w3.org/WAI/ER/IG/ert/test22A.htm" target="_blank" title="misunderstood and abused">misunderstood
and abused</a>. I highly recommend that all GUI developers read up on all the crazy
(and cool) things you can do to <a href="http://www.codeproject.com/cs/algorithms/colorspace1.asp" target="_blank" title="manipulate colors">manipulate
colors</a>. 
<br /><br />
Last year I spent a fair amount of time scouring the net trying to find a solution
to a frequently experienced problem: background - foreground color readability. And
for the most part I came up empty handed, so I did even more digging and researching
and have found the optimal way of determining whether white or black text will look
best on an arbitrary background color. At first I tried solely looking at the "V"
component of the <a href="http://en.wikipedia.org/wiki/HSV_color_space" target="_blank" title="HSV">HSV</a> color
space, but that would dictate the I use black text on a blue backgro<font face="Tahoma">und
(<span style="background: rgb(0, 0, 153) none repeat scroll 0% 50%; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">which
doesn't work</span>). </font><br /><br />
I tried making my own custom metrics like if (R + G + B)/(255 * 3) was greater than
1/2 then use black, else white, but when you test the results, it just doesn't seem
to work all that well. The problem lies with the human eye, while the monitor has
a certain color addressing space, people actually perceive color differently. It makes
sense when you think about it, for most of homo sapiens history have primarily looked
at all the green vegitation surrounding us; so naturally the human eye is more perceptive
to <a href="http://www.hownightvisionworks.com/" target="_blank" title="shades of green">shades
of green</a> than red or blue. After some extra long digging, I stumbled upon the <a href="http://en.wikipedia.org/wiki/YIQ" target="_blank" title="YIQ">YIQ</a> color
space, where "Y" is the <a href="http://en.wikipedia.org/wiki/Luminance_%28video%29" target="_blank" title="luma">luma</a> a.k.a.
perceived luminance. The formula for calculating Y from RGB is Y = 0.299 * R + 0.587
* G + 0.114 * B<br /><br />
We can now calculate the maximum perceived luminance of white (0.299 * 255 + 0.587
* 255 + 0.114 * 255) and the midpoint at which something is "half-bright" by dividing
the max into 2. Because I'm calculating values relative to one another, I simply made
each color coefficient a whole integer to make an infinitesimally small difference
in performance. You should try it out, it works really well.<br /><br /><br />
//perceived luminance<br />
private const int RED_LUMINANCE = 299;<br />
private const int GREEN_LUMINANCE = 587;<br />
private const int BLUE_LUMINANCE = 114;<br />
//calculated from <a href="http://en.wikipedia.org/wiki/Luminance%28video%29" target="_blank">http://en.wikipedia.org/wiki/Luminance(video)</a><br />
private const int MAX_LUMINANCE = (RED_LUMINANCE * 255 + GREEN_LUMINANCE * 255 + BLUE_LUMINANCE
* 255);<br />
private const int MID_LUMINANCE = MAX_LUMINANCE / 2;<br /><br />
/// &lt;summary&gt; 
<summary><br />
/// Finds the foreground (white or black) that will be easiest to read<br />
/// with the given background<br />
/// &lt;/summary&gt;
</summary><br />
public static Color CalculateForeColor(Color backColor)<br />
{<br />
    int totalCustomBrightness = 
<br />
        ((backColor.R * RED_LUMINANCE) + (backColor.G
* GREEN_LUMINANCE) + (backColor.B * BLUE_LUMINANCE));<br /><br />
    if (totalCustomBrightness &lt;= MID_LUMINANCE)<br />
        return Color.White;<br />
    else<br />
        return Color.Black;<br />
}<div><div id="_com_1"><br /></div></div><br /></div>
    </content>
  </entry>
  <entry>
    <title>Retroactive Interfaces - Strong Duck Typing</title>
    <link rel="alternate" type="text/html" href="http://www.vonsharp.net/RetroactiveInterfacesStrongDuckTyping.aspx" />
    <id>http://www.vonsharp.net/PermaLink,guid,4b13cf5b-f7e6-4932-a836-2dc934264243.aspx</id>
    <published>2007-11-27T19:28:39.136-08:00</published>
    <updated>2007-11-28T09:32:19.4440781-08:00</updated>
    <category term="Wishlist" label="Wishlist" scheme="http://www.vonsharp.net/CategoryView,category,Wishlist.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">Anyone who has read about <a href="http://en.wikipedia.org/wiki/Ruby_%28programming_language%29">Ruby</a> may
have heard about <a href="http://en.wikipedia.org/wiki/Duck_typing">Duck Typing</a>;
it allows any object to be passed into a method as long as the object has the properties/methods
used by the originating method. If this were to be implemented in C# it would look
something like:<br /><br />
public void Blah(object foo)<br />
{<br />
    Console.WriteLine(foo.Bar);<br />
    Console.WriteLine(foo.Kung("Jackie Chan"));<br />
}<br /><br />
And it would work completely fine at run-time as long as the object passed to the
Blah method had a property called "Bar" and a method called "Kung" that took in a
string as an argument. Unfortunately this won't work in C# because C# is a strongly
typed language, "object" simply does not have a "Bar" property, nor a "Kung" method.
Strong typing does have its advantages though, IntelliSense and compile time checking.
If in Ruby you were to call the Blah method and it didn't have the appropriate properties
and methods you would receive a run-time error. Run-time errors are bad because they
make end-users cranky, which makes your boss cranky, which makes you cranky.<br /><br />
Strongly typed languages can have some of the benefits of this flexibility with Interfaces.
Just create an IFoo interface with the needed properties and you're off to the races,
right? Well, yeah, but only on objects that you have the ability to edit, which leaves
you praying that the authors of your dependencies have your <strike>bizarre</strike> perfectly
reasonable needs in mind (not likely).<br /><br />
For instance, I recently wrote a windows component that would listen for some events
on a textbox and process the results. The only problem is I wanted to let the user
hook up the component to either a TextBox or a ToolStripTextBox. Since those objects
don't share a common interface, I have to have two separate properties on the component,
a TextBox and a ToolStripTextBox and at run-time I have to throw an error if the user
assigned a value to both properties (and remember, run-time error != desirable).<br /><br />
What I propose is the natural extension (pun intended) of <a href="http://en.wikipedia.org/wiki/Extension_method">Extension
Methods</a> -&gt; Retroactive Interfaces. Simply put, I'd like to be able to tell
the compiler that certain classes implement an interface, but do so from the interface,
not from the class (because I may not be able to edit the class). I don't really care
what the exact syntax is but I could imagine it going something like:<br /><br />
using System.Windows.Forms;<br /><br />
public Interface IText ImplementedBy TextBox, ToolStripTextBox<br />
{<br />
    string Text { get; set; }<br />
    event EventHandler TextChanged;<br />
}<br /><br />
This is if you wanted to be fairly precise about which classes implemented your new
interface, but it would be also nice if to tell the compiler that you wanted any object
that has the appropriate properties/methods/events to have it implement said interface.
Such syntax could look like:<br /><br />
public Interface IText ImplementedByAllAppropriate  { ... }<br /><br />
And voilà, you now have the flexibility of Duck Typing and the compile time checking
and IntelliSense of Strong Typing. I know bureaucracy can be a wildly beast, so <a href="http://en.wikipedia.org/wiki/Anders_Hejlsberg">Anders</a>,
if you're reading, I'm willing to grease proverbial wheels with $2,000,000 to get
this feature in the next version of C#. (No, I don't have the $2,000,000 right now,
but I'm working out a very lucrative deal with a rich Nigerian General) 
<br /><br /><br />
Post Script: Like Extension Methods, Retroactive Interfaces aren't aimed at solving
some unsolvable problem. You can definitely use the <a href="http://en.wikipedia.org/wiki/Adapter_pattern">Adapter
Pattern</a> to wrap up the object and have the adapter class implement the needed
interface. But lets be honest, its simply a pain in the ass, it takes time to write
the adapter class and you have to explicitly create an adapter for every class you
want to implement your interface (ImplementedByAllAppropriate, isn't really possible
with an adapter). A Retroactive Interface is some syntatic sugar aimed at making developers
lives easier. 
<br /><p><br /></p><br /><br /></div>
    </content>
  </entry>
  <entry>
    <title>Booyah - First Post</title>
    <link rel="alternate" type="text/html" href="http://www.vonsharp.net/BooyahFirstPost.aspx" />
    <id>http://www.vonsharp.net/PermaLink,guid,1a39e6d7-66af-444e-b64a-8748b6b67041.aspx</id>
    <published>2007-11-10T16:04:01.224-08:00</published>
    <updated>2007-11-13T06:53:52.0936242-08:00</updated>
    <category term="Stuff" label="Stuff" scheme="http://www.vonsharp.net/CategoryView,category,Stuff.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">After battling with with all the little
quirks involved with switching hosting providers, I finally have a blog engine. I
originally tried installing subtext but the most recent version is using the MS AJAX
extensions, thus requiring that my 1and1 have the AJAX extensions installed and unfortunately
1and1 refuses to install it. Even though its been an officially supported MS product
for almost a year. Anyway, I had to go with DasBlog and things seem to be going ok
thus far, it took a while to tweak all the different config files just right. By the
way, those are some ginormous config files (web.config =&gt; 190k). One confusing
thing about DasBlog: it doesn't use a database! I always kind of thought a sql driven
blogging engine was a little overkill, and if I knew that DasBlog just wrote everything
to a xml config file, I would have started with it to begin with.<br /><br />
Anyway, I look forward to sharing my thoughts on .Net and development in general.
Feel free to drop me a line at BOOjvongillernSPAM@gmail.com<br /><p></p></div>
    </content>
  </entry>
</feed>