<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>.Net Framework</title>
        <link>http://blogs.interakting.co.uk/mattnield/category/88.aspx</link>
        <description>.NET technology provides the ability to quickly build, deploy, manage, and use connected, security-enhanced solutions</description>
        <language>en-GB</language>
        <copyright>Matt Nield</copyright>
        <managingEditor>mnield@businessdecision.co.uk</managingEditor>
        <generator>Subtext Version 1.9.5.177</generator>
        <item>
            <title>.Net: Controlling Math.Round()</title>
            <link>http://blogs.interakting.co.uk/mattnield/archive/2008/12/17/.net-controlling-math.round.aspx</link>
            <description>&lt;p&gt;Having recently been subject to a number of rounding issues in an application, we've had to take a closer look at how rounding works in the .Net framework.&lt;/p&gt;  &lt;p&gt;It seams that when using &lt;a href="http://msdn.microsoft.com/en-us/library/ms131275.aspx"&gt;Math.Round()&lt;/a&gt;, there is an overload that allows us to specify the type of rounding that is used in the form of the &lt;a href="http://msdn.microsoft.com/en-us/library/system.midpointrounding.aspx"&gt;MidpointRounding&lt;/a&gt; enumeration.&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;As the &lt;a title="Microsoft" href="http://www.microsoft.com" target="_blank"&gt;Microsoft&lt;/a&gt; documentation will tell you, there are two possible values we can choose from:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;ToEven:&lt;/strong&gt; Round to the nearest even number. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;AwayFromZero:&lt;/strong&gt; Round to the nearest number &lt;u&gt;away from zero&lt;/u&gt;. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;This proves to be quite interesting when you start thinking about it in a little more detail.  I was generally taught at school that you usually round up to the nearest number so that 0.5 becomes 1, -4.5 becomes for and so on.  This is not the case in .Net it would seem and I find it a little surprising that it has taken me this long to notice.&lt;/p&gt;  &lt;p&gt;If you write a little console application in &lt;a title="Visual Studio" href="http://msdn.microsoft.com/vstudio/" target="_blank"&gt;Visual Studio&lt;/a&gt;, you can take a look at how this works:&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;            Console.WriteLine(&lt;/pre&gt;

  &lt;pre&gt;                &lt;span class="str"&gt;"Value   Default   Even   FromZero"&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;                );&lt;/pre&gt;

  &lt;pre&gt;            &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;decimal&lt;/span&gt; myValue = -2.0M; myValue &amp;lt; 2.0M; myValue += 0.1M)&lt;/pre&gt;

  &lt;pre class="alt"&gt;            {&lt;/pre&gt;

  &lt;pre&gt;                Console.WriteLine(String.Empty.PadLeft(35,&lt;span class="str"&gt;'-'&lt;/span&gt;));&lt;/pre&gt;

  &lt;pre class="alt"&gt;                Console.WriteLine(&lt;/pre&gt;

  &lt;pre&gt;                    &lt;span class="str"&gt;"{0}   {1}   {2}   {3}"&lt;/span&gt;,&lt;/pre&gt;

  &lt;pre class="alt"&gt;                    myValue.ToString(&lt;span class="str"&gt;"0.0"&lt;/span&gt;).PadLeft(5),&lt;/pre&gt;

  &lt;pre&gt;                    Math.Round(myValue, 0).ToString(&lt;span class="str"&gt;"00"&lt;/span&gt;).PadLeft(7),&lt;/pre&gt;

  &lt;pre class="alt"&gt;                    Math.Round(myValue, 0, MidpointRounding.AwayFromZero).ToString(&lt;span class="str"&gt;"00"&lt;/span&gt;).PadLeft(4),&lt;/pre&gt;

  &lt;pre&gt;                    Math.Round(myValue, 0, MidpointRounding.ToEven).ToString(&lt;span class="str"&gt;"00"&lt;/span&gt;).PadLeft(8)&lt;/pre&gt;

  &lt;pre class="alt"&gt;                    );&lt;/pre&gt;

  &lt;pre&gt;            }&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The following table give you an idea of how these work for some values.&lt;/p&gt;

&lt;table cellspacing="0" cellpadding="2" width="433" border="1"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="134"&gt;&lt;strong&gt;Value&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top" width="140"&gt;&lt;strong&gt;Round to Even&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top" width="157"&gt;&lt;strong&gt;Round Away from Zero&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="134"&gt;0.4&lt;/td&gt;

      &lt;td valign="top" width="140"&gt;0&lt;/td&gt;

      &lt;td valign="top" width="157"&gt;0&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="134"&gt;0.5&lt;/td&gt;

      &lt;td valign="top" width="140"&gt;0&lt;/td&gt;

      &lt;td valign="top" width="157"&gt;1&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="134"&gt;1.4&lt;/td&gt;

      &lt;td valign="top" width="140"&gt;1&lt;/td&gt;

      &lt;td valign="top" width="157"&gt;1&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="134"&gt;1.5&lt;/td&gt;

      &lt;td valign="top" width="140"&gt;2&lt;/td&gt;

      &lt;td valign="top" width="157"&gt;2&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;The interesting point is that the default value for Math.Round when not specified is &lt;strong&gt;ToEven &lt;/strong&gt;which is not really what I would have expected.  I was also a little intrigued to find that there is not provision of a &lt;strong&gt;TowardsZero&lt;/strong&gt; value.  Having a look at &lt;a href="http://www.wikipedia.com"&gt;Wikipedia&lt;/a&gt;, there are numerous &lt;a href="http://en.wikipedia.org/wiki/Rounding"&gt;methods of rounding&lt;/a&gt; and they give a handy list of which languages make which preference.  .Net favors what Wikipedia calls '&lt;em&gt;Round-half-even&lt;/em&gt;', which is &lt;strong&gt;MidpointRounding.RoundToEven&lt;/strong&gt;.  It's sometimes refereed to as &lt;a href="http://www.diycalculator.com/sp-round.shtml#A5"&gt;Banker's Rounding&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This clearly is not a massive revelation, but when you start to deal with systems that need to perform calculations and correlate the results of those calculations with other, less-precise systems you should take note of how you will be rounding as checking this out early can prevent headaches later.&lt;/p&gt;&lt;img src="http://blogs.interakting.co.uk/mattnield/aggbug/371.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Matt Nield</dc:creator>
            <guid>http://blogs.interakting.co.uk/mattnield/archive/2008/12/17/.net-controlling-math.round.aspx</guid>
            <pubDate>Wed, 17 Dec 2008 12:41:26 GMT</pubDate>
            <wfw:comment>http://blogs.interakting.co.uk/mattnield/comments/371.aspx</wfw:comment>
            <comments>http://blogs.interakting.co.uk/mattnield/archive/2008/12/17/.net-controlling-math.round.aspx#feedback</comments>
            <wfw:commentRss>http://blogs.interakting.co.uk/mattnield/comments/commentRss/371.aspx</wfw:commentRss>
            <trackback:ping>http://blogs.interakting.co.uk/mattnield/services/trackbacks/371.aspx</trackback:ping>
        </item>
        <item>
            <title>BizTalk: Division Functiod Returns Double</title>
            <link>http://blogs.interakting.co.uk/mattnield/archive/2008/10/01/biztalk-division-functiod-returns-double.aspx</link>
            <description>&lt;p&gt;Just a little note here that we noticed testing something (how it had not occurred before, I do not know).  We have a function that we were referencing from a referenced assembly that would format values to the slightly odd format being used by the ERP that we were talking too.  In  most cases it seamed to work OK, but for smaller values it didn't quite product the result we expected.&lt;/p&gt;  &lt;p&gt;It turns out that the result of the Division functiod in &lt;a title="BizTalk Server" href="http://www.microsoft.com/biztalk/default.mspx" target="_blank"&gt;BizTalk Server&lt;/a&gt; 2006 is a double.  The mapper being the way it is converts this to a string (assumably using .ToString()).  At least this is how it looks when I point &lt;a title="ILDASM Reference" href="http://msdn.microsoft.com/en-us/library/f7dy01k1(VS.80).aspx"&gt;ildasm.exe&lt;/a&gt; at &lt;a title="Microsoft" href="http://www.microsoft.com" target="_blank"&gt;Microsoft&lt;/a&gt;.BizTalk.BaseFunctoids.dll.&lt;/p&gt;  &lt;p&gt;Now, rather simply put, I was being a fool and trying to convert this to a decimal and wondering what was going on.  I was passing what I though was a value of "0.00002" to my referenced method and expecting it to be formatted properly.  When we looked deeper in to the issue, we noticed that the value coming out was in fact "2E-05", which is how a float or a double would represent the value 0.00002.&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;Quick use of the &lt;a title="Double.TryParse()" href="http://msdn.microsoft.com/en-us/library/system.double.tryparse.aspx"&gt;double.TryParse()&lt;/a&gt; fixes this issue and off we trundle.&lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;Worth keeping in mind, and also worth having a look at the base functoids using ildasm.exe when relying on their output.&lt;/p&gt;&lt;img src="http://blogs.interakting.co.uk/mattnield/aggbug/354.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Matt Nield</dc:creator>
            <guid>http://blogs.interakting.co.uk/mattnield/archive/2008/10/01/biztalk-division-functiod-returns-double.aspx</guid>
            <pubDate>Wed, 01 Oct 2008 14:49:06 GMT</pubDate>
            <wfw:comment>http://blogs.interakting.co.uk/mattnield/comments/354.aspx</wfw:comment>
            <comments>http://blogs.interakting.co.uk/mattnield/archive/2008/10/01/biztalk-division-functiod-returns-double.aspx#feedback</comments>
            <wfw:commentRss>http://blogs.interakting.co.uk/mattnield/comments/commentRss/354.aspx</wfw:commentRss>
            <trackback:ping>http://blogs.interakting.co.uk/mattnield/services/trackbacks/354.aspx</trackback:ping>
        </item>
        <item>
            <title>.Net: Methods with a Variable Number of Arguments</title>
            <link>http://blogs.interakting.co.uk/mattnield/archive/2008/09/12/.net-methods-with-a-variable-number-of-arguments.aspx</link>
            <description>&lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Quite often we want to be able to perform the same operation on some information given a differing number of arguments.  Typically, you might pass these arguments in as an array, thus avoiding the need to specify the number of arguments.  In order to call a function like this, we then need to go ahead and build and array of to pass in our arguments.  To me that seems like a little bit of an effort, at each point in my application, I know how many things I want to pass in.  So how else can I do this?&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Now, consider the funtion &lt;a title="System.String.Format() on MSDN" href="http://msdn.microsoft.com/en-us/library/fht0f5be.aspx"&gt;System.String.Format().&lt;/a&gt;  In one form, the Format function can take a string and an array of type Objects.  A typical call to String.Format will be as follows:&lt;/p&gt;  &lt;div class="csharpcode-wrapper"&gt;   &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;string&lt;/span&gt; myString = System.String.Format(
    &lt;span class="str"&gt;"Employee #{0} had been with the company since {1}{2}"&lt;/span&gt;,
    currentEmployee.Number,
    currentEmployee.ContractCommenceDate.ToShortDateString(),
    Environment.NewLine
    );&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Notice that our array of objects is represented as a comma separated list.  Lets see what happens if we try this and create a simple console application as follows:&lt;/p&gt;

&lt;div class="csharpcode-wrapper"&gt;
  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Text;

&lt;span class="kwrd"&gt;namespace&lt;/span&gt; VariadicFunctions
{
    &lt;span class="kwrd"&gt;class&lt;/span&gt; Program
    {
        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)
        {
            Console.WriteLine(
                Concat(
                    &lt;span class="str"&gt;"String 1 "&lt;/span&gt;,
                    &lt;span class="str"&gt;"String 2 "&lt;/span&gt;,
                    &lt;span class="str"&gt;"Stirng 3 "&lt;/span&gt;
                    )
                );
        }

        &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
        &lt;span class="rem"&gt;/// Glue a lots of strings together in to the mother of all strings.&lt;/span&gt;
        &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
        &lt;span class="rem"&gt;/// &amp;lt;param name="myStrings"&amp;gt;A collection of obedient strings&amp;lt;/param&amp;gt;&lt;/span&gt;
        &lt;span class="rem"&gt;/// &amp;lt;returns&amp;gt;The mother of all strings&amp;lt;/returns&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; String Concat(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] myStrings)
        {
            StringBuilder sBuild = &lt;span class="kwrd"&gt;new&lt;/span&gt; StringBuilder();

            &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (String str &lt;span class="kwrd"&gt;in&lt;/span&gt; myStrings)
            {
                sBuild.Append(str);
            }

            &lt;span class="kwrd"&gt;return&lt;/span&gt; sBuild.ToString();
        }
    }
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;When we compile, we get an error telling us "&lt;em&gt;No overload for method 'Concat' takes '3' arguments&lt;/em&gt;".  That makes sense, the method is looking for a string array, not a load of separate strings.  What we need to do is turn this in to what is know as a &lt;strong&gt;Variadic Function&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A variadic functions is a function that takes an variable number of arguments.  Essentially, to turn our function 'Concat' in to a variadic function we just need to use the &lt;a title="Params keywork on MSDN" href="http://msdn.microsoft.com/en-gb/library/w5zay9db.aspx"&gt;params keyword&lt;/a&gt;.  What this keyword does is, in simple-speak, allows us to enter our additional arguments in a comma separated list.  Simply adding this keyword in will then allow us to comma-separate our arguments:&lt;/p&gt;

&lt;div class="csharpcode-wrapper"&gt;
  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Text;

&lt;span class="kwrd"&gt;namespace&lt;/span&gt; VariadicFunctions
{
    &lt;span class="kwrd"&gt;class&lt;/span&gt; Program
    {
        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)
        {
            Console.WriteLine(
                Concat(
                    &lt;span class="str"&gt;"String 1 "&lt;/span&gt;,
                    &lt;span class="str"&gt;"String 2 "&lt;/span&gt;,
                    &lt;span class="str"&gt;"Stirng 3 "&lt;/span&gt;
                    )
                );
        }

        &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
        &lt;span class="rem"&gt;/// Glue a lots of strings together in to the mother of all strings.&lt;/span&gt;
        &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
        &lt;span class="rem"&gt;/// &amp;lt;param name="myStrings"&amp;gt;A collection of obedient strings&amp;lt;/param&amp;gt;&lt;/span&gt;
        &lt;span class="rem"&gt;/// &amp;lt;returns&amp;gt;The mother of all strings&amp;lt;/returns&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; String Concat(&lt;span class="kwrd"&gt;params&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt;[] myStrings)
        {
            StringBuilder sBuild = &lt;span class="kwrd"&gt;new&lt;/span&gt; StringBuilder();

            &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (String str &lt;span class="kwrd"&gt;in&lt;/span&gt; myStrings)
            {
                sBuild.Append(str);
            }

            &lt;span class="kwrd"&gt;return&lt;/span&gt; sBuild.ToString();
        }
    }
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And there we go, no build errors, we can add as many stings as we like without issue.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;References:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Variadic Functions on Wikipedia: &lt;a title="http://en.wikipedia.org/wiki/Varargs" href="http://en.wikipedia.org/wiki/Varargs"&gt;http://en.wikipedia.org/wiki/Varargs&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;Params Keyword on MSDN: &lt;a title="http://msdn.microsoft.com/en-gb/library/w5zay9db.aspx" href="http://msdn.microsoft.com/en-gb/library/w5zay9db.aspx"&gt;http://msdn.microsoft.com/en-gb/library/w5zay9db.aspx&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Keywords:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Variadic, params, arguments array, variable arguments, variable parameters, parameters array &lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://blogs.interakting.co.uk/mattnield/aggbug/349.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Matt Nield</dc:creator>
            <guid>http://blogs.interakting.co.uk/mattnield/archive/2008/09/12/.net-methods-with-a-variable-number-of-arguments.aspx</guid>
            <pubDate>Fri, 12 Sep 2008 17:02:42 GMT</pubDate>
            <wfw:comment>http://blogs.interakting.co.uk/mattnield/comments/349.aspx</wfw:comment>
            <comments>http://blogs.interakting.co.uk/mattnield/archive/2008/09/12/.net-methods-with-a-variable-number-of-arguments.aspx#feedback</comments>
            <wfw:commentRss>http://blogs.interakting.co.uk/mattnield/comments/commentRss/349.aspx</wfw:commentRss>
            <trackback:ping>http://blogs.interakting.co.uk/mattnield/services/trackbacks/349.aspx</trackback:ping>
        </item>
        <item>
            <title>BizTalk: Unit Testing BizTalk Maps in Visual Studio</title>
            <link>http://blogs.interakting.co.uk/mattnield/archive/2008/07/10/BizTalk-Unit-Testing-BizTalk-Maps-in-Visual-Studio.aspx</link>
            <description>&lt;p&gt;If you're using &lt;a title="BizTalk Server" href="http://www.microsoft.com/biztalk/default.mspx" target="_blank"&gt;BizTalk Server&lt;/a&gt; maps, it is about 99.999% likely that you are using logic in them.  Testing this functionality in &lt;a title="Visual Studio" href="http://msdn.microsoft.com/vstudio/" target="_blank"&gt;Visual Studio&lt;/a&gt; can be tedious as the only way to change the test document is in the properties of the map.  So if you have quite a few scenarios to test, this method sucks.  &lt;a title="BizTalk Server" href="http://www.microsoft.com/biztalk/default.mspx" target="_blank"&gt;BizTalk Server&lt;/a&gt; 2000 and 2002 allowed you to select the test document at runtime, which was nice I suppose but it still meant that you had to do it manually all the time.&lt;/p&gt;  &lt;p&gt;With the exception of either figuring out how to use BizUnit and actually deploying your solution, there are not too many ways to quickly test multiple scenarios of documents in your maps.  I wanted to create a series of tests for a project that I was working on so that I could very quickly test the outcome of my &lt;a title="BizTalk Server" href="http://www.microsoft.com/biztalk/default.mspx" target="_blank"&gt;BizTalk Server&lt;/a&gt; maps and essentially 'tick some boxes' before moving in to system testing.  There is nothing worse than a &lt;a title="BizTalk Server" href="http://www.microsoft.com/biztalk/default.mspx" target="_blank"&gt;BizTalk Server&lt;/a&gt; deploy just to find out you've forgotten something that is key in your map.&lt;/p&gt;  &lt;h2&gt;Using &lt;a title="BizTalk Server" href="http://www.microsoft.com/biztalk/default.mspx" target="_blank"&gt;BizTalk Server&lt;/a&gt; Maps in Code&lt;/h2&gt;  &lt;p&gt;I use &lt;a title="Visual Studio Unit Tests (Test Edition)" href="http://msdn.microsoft.com/en-us/library/ms243146.aspx"&gt;Test Projects&lt;/a&gt; in &lt;a title="Visual Studio" href="http://msdn.microsoft.com/vstudio/" target="_blank"&gt;Visual Studio&lt;/a&gt; to do a majority of my unit tests have recently stopped using &lt;a title="NUnit" href="http://www.nunit.com/index.php"&gt;NUnit&lt;/a&gt;.  What I wanted was a nice way to simply perform multiple &lt;a title="BizTalk Server" href="http://www.microsoft.com/biztalk/default.mspx" target="_blank"&gt;BizTalk Server&lt;/a&gt; map tests at the click of a button.  Saving me a great deal of time and helping me to ensure my deployments contain maps that fulfil the project requirements.&lt;/p&gt;  &lt;p&gt;I'll not go in to how to create the actual test projects, the purpose of this article is to call a &lt;a title="BizTalk Server" href="http://www.microsoft.com/biztalk/default.mspx" target="_blank"&gt;BizTalk Server&lt;/a&gt; map in code so that we can examine the output.&lt;/p&gt;  &lt;p&gt;In order to reference &lt;a title="BizTalk Server" href="http://www.microsoft.com/biztalk/default.mspx" target="_blank"&gt;BizTalk Server&lt;/a&gt; maps, you must first add a reference to &lt;a title="Microsoft" href="http://www.microsoft.com" target="_blank"&gt;Microsoft&lt;/a&gt;.XLANGs.BaseTypes which can be found in &lt;font face="Courier New"&gt;C:\Program Files\Microsoft &lt;a title="BizTalk Server" href="http://www.microsoft.com/biztalk/default.mspx" target="_blank"&gt;BizTalk Server&lt;/a&gt; 2006\Microsoft.XLANGs.BaseTypes.dll&lt;/font&gt; or wherever you ended up installing &lt;a title="BizTalk Server" href="http://www.microsoft.com/biztalk/default.mspx" target="_blank"&gt;BizTalk Server&lt;/a&gt;.  Once you have this, it's actually pretty simple (although it took me a while to figure everything out and I was trying to do stuff manually that was already done for me).&lt;/p&gt;  &lt;p&gt;Below is the code I used to execute a map.&lt;/p&gt;  &lt;div class="csharpcode-wrapper"&gt;   &lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;// Create an instance of the map from the &lt;a title="BizTalk Server" href="http://www.microsoft.com/biztalk/default.mspx" target="_blank"&gt;BizTalk Server&lt;/a&gt; project&lt;/span&gt;
MyBizTalkProjects.Schemas.MapProduct map = &lt;span class="kwrd"&gt;new&lt;/span&gt; MyBizTalkProjects.Schemas.MapProduct();

&lt;span class="rem"&gt;// Create an XPath document based on the XML file that is our source&lt;/span&gt;
System.Xml.XPath.XPathDocument source;

&lt;span class="rem"&gt;// Open the XML document from the file system and load it in to the XPath document&lt;/span&gt;
&lt;span class="kwrd"&gt;using&lt;/span&gt; (
    System.IO.StreamReader sRead = &lt;span class="kwrd"&gt;new&lt;/span&gt; StreamReader(
        &lt;span class="str"&gt;@"D:\BraitrimDotNet\BizTalk\Schemas TestProject\EBizDocuments\DespatchConfirmation.xml"&lt;/span&gt;
        )
    )
{
    source = &lt;span class="kwrd"&gt;new&lt;/span&gt; System.Xml.XPath.XPathDocument(sRead);
}

&lt;span class="rem"&gt;// This took a while to figure out.  Essentially the map is a fully declared XslTransform.  as such, the&lt;/span&gt;
&lt;span class="rem"&gt;// extension objects (i.e. functiod references) are already referenced and so we can use this reference&lt;/span&gt;
&lt;span class="rem"&gt;// rather than trying to build it ourselves.&lt;/span&gt;
System.Xml.Xsl.XsltArgumentList args = map.TransformArgs;

&lt;span class="rem"&gt;// Set the XmlReader to the transorm.&lt;/span&gt;
System.Xml.XmlReader xReader = map.Transform.Transform(source, args);

&lt;span class="rem"&gt;// Perform the transform and load it in to an XmlDocument for examination&lt;/span&gt;
System.Xml.XmlDocument result = &lt;span class="kwrd"&gt;new&lt;/span&gt; System.Xml.XmlDocument(xReader.NameTable);
result.Load(xReader);&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Once I have the XmlDocument, I can simply use XPath queries to check values in the XML match my anticipated output.&lt;/p&gt;

&lt;p&gt;In &lt;a title="Visual Studio" href="http://msdn.microsoft.com/vstudio/" target="_blank"&gt;Visual Studio&lt;/a&gt;, I essentially create a test method for piece of map logic that I want to test.  That way, when I run my tests from the Test View window, I can quickly see what has worked and what has not.&lt;/p&gt;&lt;img src="http://blogs.interakting.co.uk/mattnield/aggbug/326.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Matt Nield</dc:creator>
            <guid>http://blogs.interakting.co.uk/mattnield/archive/2008/07/10/BizTalk-Unit-Testing-BizTalk-Maps-in-Visual-Studio.aspx</guid>
            <pubDate>Thu, 10 Jul 2008 15:27:45 GMT</pubDate>
            <wfw:comment>http://blogs.interakting.co.uk/mattnield/comments/326.aspx</wfw:comment>
            <comments>http://blogs.interakting.co.uk/mattnield/archive/2008/07/10/BizTalk-Unit-Testing-BizTalk-Maps-in-Visual-Studio.aspx#feedback</comments>
            <wfw:commentRss>http://blogs.interakting.co.uk/mattnield/comments/commentRss/326.aspx</wfw:commentRss>
            <trackback:ping>http://blogs.interakting.co.uk/mattnield/services/trackbacks/326.aspx</trackback:ping>
        </item>
        <item>
            <title>.Net: Adding GacUtil.exe to the 'Send To' Menu</title>
            <link>http://blogs.interakting.co.uk/mattnield/archive/2008/07/09/.Net-Adding-GacUtil.exe-to-the-Send-To-Menu.aspx</link>
            <description>&lt;p&gt;I tend to spend a lot of time adding assemblies to the GAC (Global Assembly Cache) in order to run unit tests or work with &lt;a title="BizTalk Server" href="http://www.microsoft.com/biztalk/default.mspx" target="_blank"&gt;BizTalk Server&lt;/a&gt; assemblies.  Usually, I'll do this using the &lt;a title="Visual Studio" href="http://msdn.microsoft.com/vstudio/" target="_blank"&gt;Visual Studio&lt;/a&gt; command prompt or the .Net configuration tool. I wanted to find a marginally quicker way to add assemblies to the GAC so I started to look at the 'Send To' menu.&lt;/p&gt;  &lt;p&gt;In order to add shortcuts to your 'Send To',  simply navigate to the following folder:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;C:\Documents and Settings\&amp;lt;User Name&amp;gt;\SendTo&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;In here you can create simple shortcuts to applications.  So for example you can add a 'Send To Notepad' shortcut.  I'm not going to go in to all of the detail of &lt;a title="Windows XP: Add a Shortcut To Your Desktop" href="http://www.microsoft.com/windowsxp/using/setup/learnmore/tips/shortcut.mspx"&gt;creating a shortcut&lt;/a&gt; here though.&lt;/p&gt;  &lt;p&gt;What I wanted to do was add shortcut to gacutil.exe.  The first thing to think about is where &lt;a title="Microsoft" href="http://www.microsoft.com" target="_blank"&gt;Microsoft&lt;/a&gt; decided to put the thing in your particular version of .Net.  To help you here, I created a little list of places that you are likely to find gacutil.exe for different versions of the framework&lt;/p&gt;  &lt;table border="1" cellspacing="0" cellpadding="2" width="498"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="78"&gt;&lt;strong&gt;Framework Version&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="418"&gt;&lt;strong&gt;Possible Locations&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="78"&gt;1.0&lt;/td&gt;        &lt;td valign="top" width="418"&gt;C:\WINDOWS\Microsoft.NET\Framework\v1.0.3705&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="78"&gt;1.1&lt;/td&gt;        &lt;td valign="top" width="418"&gt;C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="78"&gt;2.0&lt;/td&gt;        &lt;td valign="top" width="418"&gt;C:\Program Files\Microsoft &lt;a title="Visual Studio" href="http://msdn.microsoft.com/vstudio/" target="_blank"&gt;Visual Studio&lt;/a&gt; 8\SDK\v2.0\bin\&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="78"&gt;3.0&lt;/td&gt;        &lt;td valign="top" width="418"&gt;C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin  &lt;em&gt;(not too sure about this one)&lt;/em&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="78"&gt;3.5&lt;/td&gt;        &lt;td valign="top" width="418"&gt;C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;Once you've located your gacutil.exe, create a shortcut to it in the SendTo folder mentioned above.  In order to get it to work, simply modify the target of the shortcut so that there is a '/i' at the end of the target (outside of the quotes) as in the following diagram:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.interakting.co.uk/images/blogs_interakting_co_uk/mattnield/WindowsLiveWriter/.NetAddingGacUtil.exetotheSendToMenu_8F86/SendToGAC_2.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" border="0" alt="SendToGAC" src="http://blogs.interakting.co.uk/images/blogs_interakting_co_uk/mattnield/WindowsLiveWriter/.NetAddingGacUtil.exetotheSendToMenu_8F86/SendToGAC_thumb.png" width="174" height="244" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;That should do it, from now on you should get the following in your context menu:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.interakting.co.uk/images/blogs_interakting_co_uk/mattnield/WindowsLiveWriter/.NetAddingGacUtil.exetotheSendToMenu_8F86/SendToGAC_inuse_2.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" border="0" alt="SendToGAC_inuse" src="http://blogs.interakting.co.uk/images/blogs_interakting_co_uk/mattnield/WindowsLiveWriter/.NetAddingGacUtil.exetotheSendToMenu_8F86/SendToGAC_inuse_thumb.png" width="260" height="232" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.interakting.co.uk/mattnield/aggbug/324.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Matt Nield</dc:creator>
            <guid>http://blogs.interakting.co.uk/mattnield/archive/2008/07/09/.Net-Adding-GacUtil.exe-to-the-Send-To-Menu.aspx</guid>
            <pubDate>Wed, 09 Jul 2008 09:12:23 GMT</pubDate>
            <wfw:comment>http://blogs.interakting.co.uk/mattnield/comments/324.aspx</wfw:comment>
            <comments>http://blogs.interakting.co.uk/mattnield/archive/2008/07/09/.Net-Adding-GacUtil.exe-to-the-Send-To-Menu.aspx#feedback</comments>
            <wfw:commentRss>http://blogs.interakting.co.uk/mattnield/comments/commentRss/324.aspx</wfw:commentRss>
            <trackback:ping>http://blogs.interakting.co.uk/mattnield/services/trackbacks/324.aspx</trackback:ping>
        </item>
        <item>
            <title>BizTalk: Type 'System.Xml.XmlDocument' in Assembly 'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.</title>
            <link>http://blogs.interakting.co.uk/mattnield/archive/2008/05/27/BizTalk-Type-System.Xml.XmlDocument-is-not-marked-as-serializable.aspx</link>
            <description>&lt;p&gt;I ran in to this problem recently while developing a solution for a client.  For me it took a little while to figure out what was happening and the error was reported to be coming from a send shape that sent a message of type System.Xml.XmlDocument.&lt;/p&gt;
&lt;p&gt;While this is the correct place for the error to be raised, it was not strictly related to them message that I was trying to send.&lt;/p&gt;
&lt;p&gt;The actual cause of the problem is that I have a class that is used to return information back from a series of helper methods in a library that I have built.  The class has 3 public properties:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Result: An inumeration indicatign the level of success &lt;/li&gt;
    &lt;li&gt;Document: when successful, this is an XML document otherwise, it is null. &lt;/li&gt;
    &lt;li&gt;Exception: this is null if the call was successful, otherwise it contains all relevant information to describe the failure. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Document&lt;/em&gt; is a System.Xml.XmlDocument and it is this that is causing the error.  Essentially, &lt;a title="BizTalk Server" target="_blank" href="http://www.microsoft.com/biztalk/default.mspx"&gt;BizTalk Server&lt;/a&gt; seems to be trying to serialize this class, either for dehydration, analyst reporting or some other reason.  All we need to do to avoid this error is simply mark the property as nonserializable, I.e:&lt;/p&gt;
&lt;div class="csharpcode-wrapper"&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// Contains the result of a receipt processing attempt.&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
[Serializable()]
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ReceiptResult
{
    &lt;span class="kwrd"&gt;private&lt;/span&gt; ReceiptSuccessResult _result;
    &lt;strong&gt;&lt;u&gt;[NonSerialized()]&lt;/u&gt;&lt;/strong&gt;
    &lt;span class="kwrd"&gt;private&lt;/span&gt; XmlDocument _document;
    &lt;span class="kwrd"&gt;private&lt;/span&gt; ReceiptException _exception;

    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// Gets the overall result of the validation attempt.&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; ReceiptSuccessResult Result
    {
        get { &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;._result; }
    }

    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// Gets any XML document that may have been returned from the validation attempt.&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    [System.Diagnostics.CodeAnalysis.SuppressMessage(&lt;span class="str"&gt;"Microsoft.Design"&lt;/span&gt;, &lt;span class="str"&gt;"CA1059:MembersShouldNotExposeCertainConcreteTypes"&lt;/span&gt;, MessageId = &lt;span class="str"&gt;"System.Xml.XmlNode"&lt;/span&gt;)]
    &lt;span class="kwrd"&gt;public&lt;/span&gt; XmlDocument Document
    {
        get { &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;._document; }
    }

    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// Gets any exception that was returned from the validation attempt.&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; ReceiptException Exception
    {
        get { &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;._exception; }
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; ReceiptResult(ReceiptSuccessResult result)
    {
        &lt;span class="kwrd"&gt;this&lt;/span&gt;._result = result;
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; ReceiptResult(ReceiptSuccessResult result, XmlDocument document)
    {
        &lt;span class="kwrd"&gt;this&lt;/span&gt;._result = result;
        &lt;span class="kwrd"&gt;this&lt;/span&gt;._document = document;
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; ReceiptResult(ReceiptSuccessResult result, ReceiptException exception)
    {
        &lt;span class="kwrd"&gt;this&lt;/span&gt;._result = result;
        &lt;span class="kwrd"&gt;this&lt;/span&gt;._exception = exception;
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;After that (for me at least) the error goes away.&lt;/p&gt;&lt;img src="http://blogs.interakting.co.uk/mattnield/aggbug/299.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Matt Nield</dc:creator>
            <guid>http://blogs.interakting.co.uk/mattnield/archive/2008/05/27/BizTalk-Type-System.Xml.XmlDocument-is-not-marked-as-serializable.aspx</guid>
            <pubDate>Tue, 27 May 2008 10:44:43 GMT</pubDate>
            <wfw:comment>http://blogs.interakting.co.uk/mattnield/comments/299.aspx</wfw:comment>
            <comments>http://blogs.interakting.co.uk/mattnield/archive/2008/05/27/BizTalk-Type-System.Xml.XmlDocument-is-not-marked-as-serializable.aspx#feedback</comments>
            <wfw:commentRss>http://blogs.interakting.co.uk/mattnield/comments/commentRss/299.aspx</wfw:commentRss>
            <trackback:ping>http://blogs.interakting.co.uk/mattnield/services/trackbacks/299.aspx</trackback:ping>
        </item>
        <item>
            <title>ASP.Net: Performance and Scaling Tips from MSDN</title>
            <link>http://blogs.interakting.co.uk/mattnield/archive/2008/05/15/ASP.Net-Performance-and-Scaling-Tips-from-MSDN.aspx</link>
            <description>&lt;p&gt;I found these two great articles on MSDN about performance and scaling of ASP.Net applications.  They're well worth a read in my opinion.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a title="10 Tips for Writing High-Performance Web Applications" href="http://msdn.microsoft.com/en-us/magazine/cc163854.aspx" target="_blank"&gt;10 Tips for Writing High-Performance Web Applications&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a title="Scaling Strategies for ASP.NET Applications" href="http://msdn.microsoft.com/en-us/magazine/cc500561.aspx" target="_blank"&gt;Scaling Strategies for ASP.NET Applications&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;&lt;img src="http://blogs.interakting.co.uk/mattnield/aggbug/279.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Matt Nield</dc:creator>
            <guid>http://blogs.interakting.co.uk/mattnield/archive/2008/05/15/ASP.Net-Performance-and-Scaling-Tips-from-MSDN.aspx</guid>
            <pubDate>Thu, 15 May 2008 12:20:31 GMT</pubDate>
            <wfw:comment>http://blogs.interakting.co.uk/mattnield/comments/279.aspx</wfw:comment>
            <comments>http://blogs.interakting.co.uk/mattnield/archive/2008/05/15/ASP.Net-Performance-and-Scaling-Tips-from-MSDN.aspx#feedback</comments>
            <wfw:commentRss>http://blogs.interakting.co.uk/mattnield/comments/commentRss/279.aspx</wfw:commentRss>
            <trackback:ping>http://blogs.interakting.co.uk/mattnield/services/trackbacks/279.aspx</trackback:ping>
        </item>
        <item>
            <title>ASP.Net Membership Provider</title>
            <link>http://blogs.interakting.co.uk/mattnield/archive/2008/04/10/ASP.Net-Membership-Provider.aspx</link>
            <description>&lt;p&gt;&lt;strong&gt;&lt;u&gt;Overview&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;I'm making a concerted effort to give myself a more understanding of ASP.Net.  One of the things I recall from classic ASP and my early forays in to ASP.Net was creating authentication for site.  Generally we created a standard set of functions that we could reuse, but the generally always ended up being 'tweaked' from project to project.  Along with ASP.Net 2.0 &lt;a title="Microsoft" href="http://www.microsoft.com" target="_blank"&gt;Microsoft&lt;/a&gt; delivered provider models.  These models can be used for site membership and roles and so looking at the membership role immediately came to mind as being something interesting to look at.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Implementation&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;I'm just going to create a simple ASP.Net web application and add some authentication to it.  In essence, this is a simple as dropping on a Login and a Create User Wizard web user control on to a page or two in to the project. This will automatically create a SQL Express database to store our user information.  All of the Login controls will pretty much work using this approach.  Me being me however, I don't want to use the MS SQL Express database as it is.  Yes, I will be using SQL Express, but I want to use my tables and store the information how I want.  To the end, I need to write my own membership provider to provide the kind of information that I need to e stored and retrieved.&lt;/p&gt;  &lt;p&gt;So, the fist thing I do is add a class to my web project called MySqlMembershipProvider in the App_Code folder.  From reading various posts around the interned, all I need to do is make this class inherit from the System.Web.Security.MembershipProvider class and all of the stubbed out derived methods will be created for me.  Nah, not really - I'm using C# so I have to right click on the inherited class and select '&lt;em&gt;Implement Abstract Class&lt;/em&gt;' to tell it to go do this for me.  VB.Net (*spit) does do this automatically however if you're using that.&lt;/p&gt;  &lt;p&gt;I'm going to implement two methods on this test application:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;CreateUser(): This will allow me to add new users to the database. &lt;/li&gt;    &lt;li&gt;ValidateUser(): Given a couple of details (namely user name and password) this will tell me if the user exists or not. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;These are enough to be able to log on to our site and create new users and that is all that I want to do for the moment.&lt;/p&gt;  &lt;p&gt;So, first of all, we'll create a database.  I'm using a SQL Express, but you really can use anything you like here; MySQL, an XML file or perhaps a link to Active Directory.  The choice is yours, but the SQL example is easier because pretty much everyone gets it.  In my database I'm going to create a table called 'User' as follows in my database:&lt;/p&gt;  &lt;table cellspacing="0" cellpadding="2" width="400" border="1"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="133"&gt;&lt;strong&gt;Name&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="133"&gt;&lt;strong&gt;Type&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="133"&gt;&lt;strong&gt;Key&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="133"&gt;Username&lt;/td&gt;        &lt;td valign="top" width="133"&gt;NVarChar(50)&lt;/td&gt;        &lt;td valign="top" width="133"&gt;Primary Key&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="133"&gt;Password&lt;/td&gt;        &lt;td valign="top" width="133"&gt;NVarChar(50)&lt;/td&gt;        &lt;td valign="top" width="133"&gt;No&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="133"&gt;EmailAddress&lt;/td&gt;        &lt;td valign="top" width="133"&gt;NVarChar(50)&lt;/td&gt;        &lt;td valign="top" width="133"&gt;No&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="133"&gt;PasswordQuestion&lt;/td&gt;        &lt;td valign="top" width="133"&gt;NVarChar(50)&lt;/td&gt;        &lt;td valign="top" width="133"&gt;No&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="133"&gt;PasswordAnswer&lt;/td&gt;        &lt;td valign="top" width="133"&gt;NVarChar(50)&lt;/td&gt;        &lt;td valign="top" width="133"&gt;No&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;Once I've created this, I can begin development.  We'll carry on with the MySqlMembershipProvider class.&lt;/p&gt;  &lt;p&gt;I'm going to override the &lt;strong&gt;Initialize&lt;/strong&gt; class and add some functionality to read some settings from the web.config file.  I'm going to store one property:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;connectionString&lt;/strong&gt; will store the connection string to my database.  In my case, this connection string is as follows:       &lt;br /&gt;&lt;font face="Courier New"&gt;Data Source=.\SQLEXPRESS;AttachDbFilename=F:\SQLMembershipProvider\App_Data\SQLMemberShipProvider.mdf;Integrated Security=True;User Instance=True&lt;/font&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Create this as private variable in the class to store this and then to the MySqlMembershipProvider add the following code:&lt;/p&gt;  &lt;div class="csharpcode-wrapper"&gt;   &lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// Set up the initial properties that we need when the provider is first loaded&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;param name="name"&amp;gt;The full name of the provider.&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;param name="config"&amp;gt;Contents of the web.config for the provider&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Initialize(&lt;span class="kwrd"&gt;string&lt;/span&gt; name, System.Collections.Specialized.NameValueCollection config)
{
    connectionString = config[&lt;span class="str"&gt;"connectionString"&lt;/span&gt;];
    &lt;span class="kwrd"&gt;base&lt;/span&gt;.Initialize(name, config);
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The above code loads the connection string so that we can use it later.  &lt;em&gt;Not that &lt;font face="Courier New"&gt;Initialize&lt;/font&gt; is not created when we implemented that abstract class, it's one that we need to do ourselves.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;As I touched on earlier, we then need to implement 'CreateUser' and 'ValidateUser' so that we will be able to logon and create new users on the site.  The two methods used for this have already been created so we will need to replace the line in the stubbed method with some real code to insert to the database and also retrieve users form the database.  So, in my example, modify those methods to look as follows:&lt;/p&gt;

&lt;div class="csharpcode-wrapper"&gt;
  &lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// Creates a user in the provider database and returne an instance of that user.&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;param name="username"&amp;gt;Provided username&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;param name="password"&amp;gt;Provided password&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;param name="email"&amp;gt;Provided email address&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;param name="passwordQuestion"&amp;gt;Provided password question&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;param name="passwordAnswer"&amp;gt;Provided password answer&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;param name="isApproved"&amp;gt;Boolean indicateing if the user is allowed to logon.&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;param name="providerUserKey"&amp;gt;The user identifier for the user that should be stored in the membership database&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;param name="status"&amp;gt;The status indicated success of user created or reason for failure.&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;returns&amp;gt;A MembershipUser object for the newly created user&amp;lt;/returns&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; MembershipUser CreateUser(&lt;span class="kwrd"&gt;string&lt;/span&gt; username, &lt;span class="kwrd"&gt;string&lt;/span&gt; password, &lt;span class="kwrd"&gt;string&lt;/span&gt; email, &lt;span class="kwrd"&gt;string&lt;/span&gt; passwordQuestion, &lt;span class="kwrd"&gt;string&lt;/span&gt; passwordAnswer, &lt;span class="kwrd"&gt;bool&lt;/span&gt; isApproved, &lt;span class="kwrd"&gt;object&lt;/span&gt; providerUserKey, &lt;span class="kwrd"&gt;out&lt;/span&gt; MembershipCreateStatus status)
{
    SqlConnection sqlConnection = &lt;span class="kwrd"&gt;new&lt;/span&gt; SqlConnection(&lt;span class="kwrd"&gt;this&lt;/span&gt;.connectionString);
    &lt;span class="kwrd"&gt;try&lt;/span&gt;
    {
        sqlConnection.Open();
        SqlCommand sqlCommand = &lt;span class="kwrd"&gt;new&lt;/span&gt; SqlCommand();
        sqlCommand.Connection = sqlConnection;
        sqlCommand.CommandText = &lt;span class="str"&gt;"INSERT INTO User VALUES (@username, @password, @email, @passwordQuestion, @passwordAnswer )"&lt;/span&gt;;
        sqlCommand.CommandType = CommandType.Text;
        sqlCommand.Parameters.AddWithValue(&lt;span class="str"&gt;"@username"&lt;/span&gt;, username);
        sqlCommand.Parameters.AddWithValue(&lt;span class="str"&gt;"@password"&lt;/span&gt;, password);
        sqlCommand.Parameters.AddWithValue(&lt;span class="str"&gt;"@email"&lt;/span&gt;, email);
        sqlCommand.Parameters.AddWithValue(&lt;span class="str"&gt;"@passwordQuestion"&lt;/span&gt;, passwordQuestion);
        sqlCommand.Parameters.AddWithValue(&lt;span class="str"&gt;"@passwordAnswer"&lt;/span&gt;, passwordAnswer);

        &lt;span class="kwrd"&gt;int&lt;/span&gt; result = sqlCommand.ExecuteNonQuery();

        sqlCommand = &lt;span class="kwrd"&gt;null&lt;/span&gt;;
        sqlConnection.Close();

        status = MembershipCreateStatus.Success;
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; MembershipUser(
            &lt;span class="str"&gt;"SQLMembershipProvider.MySQLMembershipProvider"&lt;/span&gt;,
            username,
            &lt;span class="kwrd"&gt;null&lt;/span&gt;,
            email,
            passwordQuestion,
            &lt;span class="kwrd"&gt;string&lt;/span&gt;.Empty,
            &lt;span class="kwrd"&gt;true&lt;/span&gt;,
            &lt;span class="kwrd"&gt;false&lt;/span&gt;,
            DateTime.Now,
            DateTime.MinValue,
            DateTime.MinValue,
            DateTime.MinValue,
            DateTime.MinValue
            );
    }
    &lt;span class="kwrd"&gt;catch&lt;/span&gt; (Exception ex)
    {
        System.Diagnostics.Trace.Write(&lt;span class="str"&gt;"CreateUserFailed: "&lt;/span&gt; + ex.Message);
        &lt;span class="rem"&gt;// HACK: I'm just going to say we failed here.  We can however be more creative and say _why_&lt;/span&gt;
        status = MembershipCreateStatus.UserRejected;
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;null&lt;/span&gt;;               
    }
}

&lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// Check to see if the username and password combination exists in the user table.&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;param name="username"&amp;gt;Supplied Username&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;param name="password"&amp;gt;Supplied Password&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;returns&amp;gt;Boolean indicating match success&amp;lt;/returns&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; ValidateUser(&lt;span class="kwrd"&gt;string&lt;/span&gt; username, &lt;span class="kwrd"&gt;string&lt;/span&gt; password)
{
    &lt;span class="rem"&gt;// Connect to the providers DB&lt;/span&gt;
    SqlConnection sqlConnection = &lt;span class="kwrd"&gt;new&lt;/span&gt; SqlConnection(&lt;span class="kwrd"&gt;this&lt;/span&gt;.connectionString);
    &lt;span class="kwrd"&gt;try&lt;/span&gt;
    {
        sqlConnection.Open();
        SqlCommand sqlCommand = &lt;span class="kwrd"&gt;new&lt;/span&gt; SqlCommand();
        sqlCommand.Connection = sqlConnection;
        &lt;span class="rem"&gt;// I spit at myself for hard-coded SQL, but this is only a demo :)&lt;/span&gt;
        sqlCommand.CommandText = &lt;span class="str"&gt;"Select Username, Password, Email, PasswordQuestion, PasswordAnswer From User WHERE username=@username AND password=@password"&lt;/span&gt;;
        sqlCommand.CommandType = CommandType.Text;

        sqlCommand.Parameters.AddWithValue(&lt;span class="str"&gt;"@username"&lt;/span&gt;, username);
        sqlCommand.Parameters.AddWithValue(&lt;span class="str"&gt;"@password"&lt;/span&gt;, password);

        SqlDataReader reader = sqlCommand.ExecuteReader();

        &lt;span class="rem"&gt;// If there are rows, then the login succeeds.  Due to the primary key, there should only ever be a maximum of one row anyway&lt;/span&gt;
        &lt;span class="kwrd"&gt;return&lt;/span&gt; reader.HasRows;

        sqlConnection.Close();
    }
    &lt;span class="kwrd"&gt;catch&lt;/span&gt; 
    {
        &lt;span class="rem"&gt;// HACK: Should really log any technical errors rather than just ignoring them in this way&lt;/span&gt;
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;false&lt;/span&gt;;
    }
}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This is almost everything fro this class, but I do want the user to be able to enter a secrete password question and answer,  There is a public property for this that at the moment throws and exception as we have not implemented it.  SO we'll just make this return true so that the web controls know what to do for now.  WE can put this in to the web.config file, but for this example I'm not going to.  Just modify the RequiresQuestionAndAnswer to return &lt;strong&gt;true&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We need to modify the web.config file to tell it which MembershipProvider to use.  So, in the web.config, change the authentication mode to 'Forms' and add the membership section as demonstrated below:&lt;/p&gt;

&lt;div class="csharpcode-wrapper"&gt;
  &lt;pre class="csharpcode"&gt;&amp;lt;authentication mode=&lt;span class="str"&gt;"Forms"&lt;/span&gt; /&amp;gt;
&amp;lt;membership defaultProvider=&lt;span class="str"&gt;"SQLMembershipProvider.MySQLMembershipProvider"&lt;/span&gt; &amp;gt;
    &amp;lt;providers&amp;gt;
        &amp;lt;add name=&lt;span class="str"&gt;"SQLMembershipProvider.MySQLMembershipProvider"&lt;/span&gt;
pe=&lt;span class="str"&gt;"SQLMembershipProvider.MySQLMembershipProvider"&lt;/span&gt;
nnectionString=&lt;span class="str"&gt;"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\SQLMembershipProvider\App_Data\SQLMemberShipProvider.mdf;Integrated Security=True;User Instance=True"&lt;/span&gt; /&amp;gt;
    &amp;lt;/providers&amp;gt;
&amp;lt;/membership&amp;gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Right, that is the trick bit.  Next we simply need to create two pages in out web application; one page to login and one page to be the homepage.  We'll call these (wait for it) Default.aspx and Login.aspx.&lt;/p&gt;

&lt;p&gt;So, to start with we'll create Login.aspx.  In design mode, drop a Login control on to the page.  Right, that's that page done.&lt;/p&gt;

&lt;p&gt;Now lets create Default.aspx.  Again, switch to design view so we can drag some controls on.  The first two controls are the Create User Wizard and the Login View.  &lt;/p&gt;

&lt;p&gt;Drag a CreateUserWizard control on to the page.  We need to change one property of this control, and the is &lt;strong&gt;ContinueDestinationPageUrl&lt;/strong&gt;.  Change this property to Login.aspx so that when a user is created they will then be asked to login.&lt;/p&gt;

&lt;p&gt;Next, drag a LoginView control on to the page.  This control allows us to see different things depending on who we are.  In the tasks tool-tip, select the AnonymousTemplate view.  In to the LoginView, drag a LoginStatus control.  This gives a simple login button for unauthenticated users.&lt;/p&gt;

&lt;p&gt;Next, switch to the LoggedInTemplate view of the LoginView control. In here we will also add a LoginStatus control.  We will also however add a LoginName control so that we can tell logged in users who they are.  Type some guff around thsi so that you end up with something like the following in your LoginView:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;You are logged in as [UserName].&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That pretty much should be it.  You should just be able to hit F5 and run the project.&lt;/p&gt;&lt;img src="http://blogs.interakting.co.uk/mattnield/aggbug/247.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Matt Nield</dc:creator>
            <guid>http://blogs.interakting.co.uk/mattnield/archive/2008/04/10/ASP.Net-Membership-Provider.aspx</guid>
            <pubDate>Thu, 10 Apr 2008 08:09:24 GMT</pubDate>
            <wfw:comment>http://blogs.interakting.co.uk/mattnield/comments/247.aspx</wfw:comment>
            <comments>http://blogs.interakting.co.uk/mattnield/archive/2008/04/10/ASP.Net-Membership-Provider.aspx#feedback</comments>
            <wfw:commentRss>http://blogs.interakting.co.uk/mattnield/comments/commentRss/247.aspx</wfw:commentRss>
            <trackback:ping>http://blogs.interakting.co.uk/mattnield/services/trackbacks/247.aspx</trackback:ping>
        </item>
        <item>
            <title>LINQ: Error 19 Could not find an implementation of the query pattern for source type</title>
            <link>http://blogs.interakting.co.uk/mattnield/archive/2008/01/25/LINQ-Error-19-Could-not-find-an-implementation-of-the.aspx</link>
            <description>&lt;p&gt;&lt;strong&gt;Overview&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;I reckon that I have probably seen the same LINQ demo as just about everyone else in the world.  You know, the one where you get a list of all of the processes running on the server and print them out to the console as follows:&lt;/p&gt;  &lt;div class="csharpcode-wrapper"&gt;   &lt;pre class="csharpcode"&gt;var processes = from activeProcesses &lt;span class="kwrd"&gt;in&lt;/span&gt; Process.GetProcesses() 
                select &lt;span class="kwrd"&gt;new&lt;/span&gt; { Name = activeProcesses.ProcessName, ProcessId = activeProcesses.Id };

&lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var row &lt;span class="kwrd"&gt;in&lt;/span&gt; processes) Console.WriteLine(row.ToString());

Console.ReadLine();&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Well, I tired to use the same principle to iterate through a custom collection for an idea I was trying out and I got a heavily descriptive error of:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;"Error 19 Could not find an implementation of the query pattern for source type"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So what does that mean?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It can actually mean a couple of things depending on the full error message.  The two main causes that I have noticed are:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Your source is not enumerable!! &lt;/li&gt;

  &lt;li&gt;LINQ doesn't have a clue what your source is. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The first issue is fairly easily addressed, make sure you use enumerable types.  It turns out that in the example I was trying to make work, I'd used the wrong object as the source which was why I got the error in the first place.  &lt;/p&gt;

&lt;p&gt;Once I had changed to the correct object, I still had the error.  The difference was that the error message now also included the following&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;"Consider explicitly specifying the type of the range variable"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Interesting I thought, why on earth would that be?  The code I had originally was as follows:&lt;/p&gt;

&lt;div class="csharpcode-wrapper"&gt;
  &lt;pre class="csharpcode"&gt;var pollingDirectories = from directory &lt;span class="kwrd"&gt;in&lt;/span&gt; serviceConfigSection.PollingDirectoires 
                select &lt;span class="kwrd"&gt;new&lt;/span&gt; { DirectoryPath = directory.Path, FileType = directory.FileType };

&lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var row &lt;span class="kwrd"&gt;in&lt;/span&gt; pollingDirectories ) Console.WriteLine(row.ToString());

Console.ReadLine();&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;What I need to to was to tell LINQ the type of source explicitly so that it would know what to do with it.  So I needed to make my code look as follows:&lt;/p&gt;

&lt;div class="csharpcode-wrapper"&gt;
  &lt;pre class="csharpcode"&gt;var pollingDirectories = from &lt;strong&gt;DirectoryElement&lt;/strong&gt; directory &lt;span class="kwrd"&gt;in&lt;/span&gt; serviceConfigSection.PollingDirectoires 
                select &lt;span class="kwrd"&gt;new&lt;/span&gt; { DirectoryPath = directory.Path, FileType = directory.FileType };

&lt;span class="kwrd"&gt;foreach&lt;/span&gt; (var row &lt;span class="kwrd"&gt;in&lt;/span&gt; pollingDirectories ) Console.WriteLine(row.ToString());

Console.ReadLine();&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;After that - everything seems to work just fine.  &lt;/p&gt;

&lt;p&gt;I guess in some cases, LINQ just needs to be given a little more information about what it is doing.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;References&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a title="LINQ: .NET Language-Integrated Query" href="http://msdn2.microsoft.com/en-us/library/bb308959.aspx"&gt;LINQ: .NET Language-Integrated Query&lt;/a&gt;&lt;/li&gt;

  &lt;li&gt;&lt;a title="Jeff Kwak: LINQ Line by Line (Part 1)" href="http://www.jeffkwak.com/blog/archive/2007/08/17/linqlinebyline1.aspx"&gt;Jeff Kwak: LINQ Line by Line (Part 1)&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;&lt;a title="Overview of C# 3.0" href="http://msdn2.microsoft.com/en-us/library/bb308966.aspx#csharp3.0overview_topic18b"&gt;Overview of C# 3.0&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Versions&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a title="Microsoft" href="http://www.microsoft.com" target="_blank"&gt;Microsoft&lt;/a&gt; .Net Framework 3.5 &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Metadata&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Categories&lt;/strong&gt;: .Net Framework 3.5, &lt;a title="Visual Studio" href="http://msdn.microsoft.com/vstudio/" target="_blank"&gt;Visual Studio&lt;/a&gt; 2008, Orcas, LINQ &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Additional Keywords&lt;/strong&gt;: Orcas, LINQ, Error 19, Enumerable, IEnumerable &lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://blogs.interakting.co.uk/mattnield/aggbug/192.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Matt Nield</dc:creator>
            <guid>http://blogs.interakting.co.uk/mattnield/archive/2008/01/25/LINQ-Error-19-Could-not-find-an-implementation-of-the.aspx</guid>
            <pubDate>Fri, 25 Jan 2008 10:12:48 GMT</pubDate>
            <wfw:comment>http://blogs.interakting.co.uk/mattnield/comments/192.aspx</wfw:comment>
            <comments>http://blogs.interakting.co.uk/mattnield/archive/2008/01/25/LINQ-Error-19-Could-not-find-an-implementation-of-the.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://blogs.interakting.co.uk/mattnield/comments/commentRss/192.aspx</wfw:commentRss>
            <trackback:ping>http://blogs.interakting.co.uk/mattnield/services/trackbacks/192.aspx</trackback:ping>
        </item>
        <item>
            <title>Formatting Numbers using IFormatProvider and ToString</title>
            <link>http://blogs.interakting.co.uk/mattnield/archive/2008/01/10/Formatting-Numbers-using-IFormatProvider-and-ToString.aspx</link>
            <description>&lt;p&gt;As I've had a number of people mention to me that they don't quite know how to use a class that implements the IFormatProvider interface.  In most case that come to my attention, we are specifically interested in date formats and number formats.&lt;/p&gt;
&lt;p /&gt;Below is the code for a simple console application in .Net 2.0 that will use the NumberFormatInfo class to format the display of a numeric value.  I've set up two instances of the NumberFormatInfo class, one for French formatting and one for British English formatting.  The CultureInfo class from the Globalization namespace has been used to load up the culture specific information that I need for this example.
&lt;p&gt;&lt;span style="font-family: Courier New;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;using &lt;/span&gt;System;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;using &lt;/span&gt;System.Collections.Generic;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;using &lt;/span&gt;System.Globalization;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;using &lt;/span&gt;System.Text;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;&lt;span style="color: rgb(0, 0, 255);" /&gt;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;namespace &lt;/span&gt;IFormatProvider&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;{&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;class &lt;/span&gt;Program&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    {&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;static void&lt;/span&gt; Main(&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;[] args)&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;        {&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;double &lt;/span&gt;testValue = &lt;span style="color: rgb(255, 0, 255);"&gt;10.234&lt;/span&gt;;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            &lt;span style="color: rgb(0, 128, 128);"&gt;NumberFormatInfo &lt;/span&gt;britishFormatter = &lt;span style="color: rgb(0, 128, 128);"&gt;CultureInfo&lt;/span&gt;.CreateSpecificCulture(&lt;span style="color: rgb(255, 0, 0);"&gt;"en-GB"&lt;/span&gt;).NumberFormat;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            &lt;span style="color: rgb(0, 128, 128);"&gt;NumberFormatInfo &lt;/span&gt;frenchFormatter = &lt;span style="color: rgb(0, 128, 128);"&gt;CultureInfo&lt;/span&gt;.CreateSpecificCulture(&lt;span style="color: rgb(255, 0, 0);"&gt;"fr-FR")&lt;/span&gt;.NumberFormat;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            &lt;span style="color: rgb(0, 128, 128);"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(255, 0, 0);"&gt;"In fr-FR, our value is {0}"&lt;/span&gt;, testValue.ToString(frenchFormatter));&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            Console.WriteLine(&lt;span style="color: rgb(255, 0, 0);"&gt;"In en-GB, our value is {0}"&lt;/span&gt;, testValue.ToString(britishFormatter));&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            &lt;span style="color: rgb(0, 128, 128);"&gt;Console&lt;/span&gt;.ReadLine();&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;        }&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    }&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;}&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;When you run the above code in Visual Studio, you will see the following output:&lt;/p&gt;
&lt;p style="font-family: Courier New; margin-left: 40px; background-color: rgb(0, 0, 0); color: rgb(255, 255, 255);"&gt;In fr-FR, our value is 10,234&lt;br /&gt;
In en-GB, our value is 10.234&lt;/p&gt;
This then got me thinking.  As I have now told the application what I want to do, I assume tha by default the ToString method on must have some kind of default based on the application thread.  I also know that the application thread has two sets of culture information; the UI culture and the system culture.  That led to the question; which is the default?&lt;br /&gt;
&lt;br /&gt;
So I went ahead and wrote the following to see which culture information is the default.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;using &lt;/span&gt;System;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;using &lt;/span&gt;System.Collections.Generic;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;using &lt;/span&gt;System.Globalization;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;using &lt;/span&gt;System.Text;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;using &lt;/span&gt;System.Threading;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;namespace &lt;/span&gt;IFormatProvider&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;{&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;class &lt;/span&gt;Program&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    {&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;static void &lt;/span&gt;Main(&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;[] args)&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;        {&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            &lt;span style="color: rgb(0, 0, 255);"&gt;double &lt;/span&gt;testValue = &lt;span style="color: rgb(255, 0, 255);"&gt;10.234&lt;/span&gt;;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            &lt;span style="color: rgb(0, 128, 128);"&gt;Console&lt;/span&gt;.WriteLine(&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;                &lt;span style="color: rgb(255, 0, 0);"&gt;"Default Culture information: UI [{0}] - SYSTEM [{1}]"&lt;/span&gt;, &lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;                &lt;span style="color: rgb(0, 128, 128);"&gt;Thread&lt;/span&gt;.CurrentThread.CurrentUICulture.Name, &lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;                &lt;span style="color: rgb(0, 128, 128);"&gt;Thread&lt;/span&gt;.CurrentThread.CurrentCulture.Name&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;                );&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            &lt;span style="color: rgb(0, 128, 128);"&gt;Console&lt;/span&gt;.WriteLine(&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;                &lt;span style="color: rgb(255, 0, 0);"&gt;"Our Value: {0}"&lt;/span&gt;, &lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;                testValue.ToString()&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;                );&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            &lt;span style="color: rgb(0, 128, 128);"&gt;Console.&lt;/span&gt;WriteLine(&lt;span style="color: rgb(255, 0, 0);"&gt;"&amp;gt; Change CurrentUICulture to French..."&lt;/span&gt;);&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            &lt;span style="color: rgb(0, 128, 128);"&gt;Thread&lt;/span&gt;.CurrentThread.CurrentUICulture = &lt;span style="color: rgb(0, 128, 128);"&gt;CultureInfo&lt;/span&gt;.CreateSpecificCulture(&lt;span style="color: rgb(255, 0, 0);"&gt;"fr-FR"&lt;/span&gt;);&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            &lt;span style="color: rgb(0, 128, 128);"&gt;Console&lt;/span&gt;.WriteLine(&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;                &lt;span style="color: rgb(255, 0, 0);"&gt;"&amp;gt; Our Value: {0}"&lt;/span&gt;, &lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;                testValue.ToString()&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;                );&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            &lt;span style="color: rgb(0, 128, 128);"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(255, 0, 0);"&gt;"Change CurrentCulture to French..."&lt;/span&gt;);&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            &lt;span style="color: rgb(0, 128, 128);"&gt;Thread&lt;/span&gt;.CurrentThread.CurrentCulture = &lt;span style="color: rgb(0, 128, 128);"&gt;CultureInfo&lt;/span&gt;.CreateSpecificCulture(&lt;span style="color: rgb(255, 0, 0);"&gt;"fr-FR"&lt;/span&gt;);&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            &lt;span style="color: rgb(0, 128, 128);"&gt;Console&lt;/span&gt;.WriteLine(&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;                &lt;span style="color: rgb(255, 0, 0);"&gt;"&amp;gt; Our Value: {0}"&lt;/span&gt;,&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;                testValue.ToString()&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;                );&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;            Console.ReadLine();&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;        }&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    }&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial;"&gt;&lt;/span&gt;&lt;br /&gt;
Interestingly, the output is as follows:&lt;br /&gt;
&lt;p style="font-family: Courier New; margin-left: 40px; background-color: rgb(0, 0, 0); color: rgb(255, 255, 255);"&gt;Our Value: 10.234&lt;br /&gt;
&amp;gt; Change CurrentUICulture to French...&lt;br /&gt;
&amp;gt; Our Value: 10.234&lt;br /&gt;
&amp;gt; Change CurrentCulture to French...&lt;br /&gt;
&amp;gt; Our Value: 10,234&lt;/p&gt;
&lt;p style="font-family: Courier New;"&gt;&lt;span style="font-family: Arial;"&gt;So, for the sake of interest; it looks like CurrentCulture is used as the default.&lt;/span&gt;&lt;/p&gt;
&lt;p style="font-family: Arial; font-weight: bold; text-decoration: underline;"&gt;References&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;a href="http://msdn2.microsoft.com/en-gb/library/0asazeez.aspx"&gt;Customizing Format Strings [MSDN]&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://msdn2.microsoft.com/en-gb/library/system.iformatprovider.aspx"&gt;IFormatProvider Interface [MSDN]&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p style="font-family: Courier New;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="font-family: Courier New;"&gt;&lt;span style="font-family: Arial;"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;/p&gt;
&lt;div style="margin-left: 40px;"&gt; &lt;/div&gt;&lt;img src="http://blogs.interakting.co.uk/mattnield/aggbug/156.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Matt Nield</dc:creator>
            <guid>http://blogs.interakting.co.uk/mattnield/archive/2008/01/10/Formatting-Numbers-using-IFormatProvider-and-ToString.aspx</guid>
            <pubDate>Thu, 10 Jan 2008 16:50:53 GMT</pubDate>
            <wfw:comment>http://blogs.interakting.co.uk/mattnield/comments/156.aspx</wfw:comment>
            <comments>http://blogs.interakting.co.uk/mattnield/archive/2008/01/10/Formatting-Numbers-using-IFormatProvider-and-ToString.aspx#feedback</comments>
            <wfw:commentRss>http://blogs.interakting.co.uk/mattnield/comments/commentRss/156.aspx</wfw:commentRss>
            <trackback:ping>http://blogs.interakting.co.uk/mattnield/services/trackbacks/156.aspx</trackback:ping>
        </item>
    </channel>
</rss>