<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>General</title>
        <link>http://geekswithblogs.net/michelotti/category/3766.aspx</link>
        <description>General</description>
        <language>en-US</language>
        <copyright>Steve Michelotti</copyright>
        <managingEditor>stevemic21@yahoo.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>EntLib Validation Application Block - Subclassing PropertyProxyValidator for ValueConvert</title>
            <link>http://geekswithblogs.net/michelotti/archive/2008/06/16/122893.aspx</link>
            <description>&lt;p&gt;The Enterprise Library VAB provides the PropertyProxyValidator for ASP.NET applications so that you can attach it to a single control and it will display any validation messages for that business object property.  This automatic UI validation is great because it avoids duplicating your business layer validation logic in your UI.  Additionally, you can just use ONE validator control whereas using traditional ASP.NET validation controls you might need to attach 3-4 validation controls to a single UI element (e.g., RequireFieldValidator, RangeValidator, RegexValidator, etc.) which can clutter your aspx pages quickly.&lt;/p&gt; &lt;p&gt;However, one of the gotchas with working with the PropertyProxyValidator control is that, while it is quite elegant for strings, you have to do a little extra work for data types such as DateTime.  Specifically, if you are working with a DateTime, you have to attach a ValueConvert method to your PropertyProxyValidator control in which you perform the data type conversion.  The following code snippet is directly out of the VAB documentation:&lt;/p&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt; &lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}

.csharpcode .lnum { color: #606060; }
]]&gt;&lt;/style&gt;  &lt;div class="csharpcode"&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; dateOfBirthValidator_ValueConvert(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, ValueConvertEventArgs e)&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;        &lt;span class="kwrd"&gt;string&lt;/span&gt; &lt;span class="kwrd"&gt;value&lt;/span&gt; = e.ValueToConvert &lt;span class="kwrd"&gt;as&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;        &lt;span class="kwrd"&gt;try&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;        {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;            e.ConvertedValue = DateTime.Parse(&lt;span class="kwrd"&gt;value&lt;/span&gt;, System.Globalization.CultureInfo.CurrentCulture);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;        &lt;span class="kwrd"&gt;catch&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;        {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;            e.ConversionErrorMessage = &lt;span class="str"&gt;"Date Of Birth is not in the correct format."&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;            e.ConvertedValue = &lt;span class="kwrd"&gt;null&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This extensibility is great but if I need to perform multiple DateTime validations like this throughout my application, I don't want to have to copy/paste this code everywhere.  Even if I encapsulate it in a static utility method, I still have to go through the pain of wiring up an event for any control where I need this just to make that 1-line call to the utility method.&lt;/p&gt;
&lt;p&gt;So...a better approach is to simply subclass the PropertyProxyValidator control.  In the following example, I just wire up the conversion in the contructor.  This enables me to avoid having to wire up the event in the aspx code and and also enables me to avoid having to include the event in the code behind.  I just specify the DateTimePropertyProxyValidator in my aspx code the normal way:&lt;/p&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}

.csharpcode .lnum { color: #606060; }
]]&gt;&lt;/style&gt;

&lt;div class="csharpcode"&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;asp:TextBox&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="txtDateOfBirth"&lt;/span&gt; &lt;span class="attr"&gt;runat&lt;/span&gt;&lt;span class="kwrd"&gt;="server"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;vab:DateTimePropertyProxyValidator&lt;/span&gt; &lt;span class="attr"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;="dobVal"&lt;/span&gt; &lt;span class="attr"&gt;runat&lt;/span&gt;&lt;span class="kwrd"&gt;="server"&lt;/span&gt; &lt;span class="attr"&gt;ControlToValidate&lt;/span&gt;&lt;span class="kwrd"&gt;="txtDateOfBirth"&lt;/span&gt; &lt;span class="attr"&gt;PropertyName&lt;/span&gt;&lt;span class="kwrd"&gt;="DateOfBirth"&lt;/span&gt; &lt;span class="attr"&gt;SourceTypeName&lt;/span&gt;&lt;span class="kwrd"&gt;="Person"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The same pattern could obviously be used for other data types.  Below is the complete code for the DateTimePropertyProxyValidator:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;div class="csharpcode"&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; DateTimePropertyProxyValidator : PropertyProxyValidator&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; DateTimePropertyProxyValidator()&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;        &lt;span class="kwrd"&gt;this&lt;/span&gt;.ValueConvert += OnValueConvert;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; OnValueConvert(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, ValueConvertEventArgs e)&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;    {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;        DateTime result;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;        &lt;span class="kwrd"&gt;if&lt;/span&gt; (DateTime.TryParse((&lt;span class="kwrd"&gt;string&lt;/span&gt;)e.ValueToConvert, &lt;span class="kwrd"&gt;out&lt;/span&gt; result))&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;        {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;            e.ConvertedValue = result;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;        &lt;span class="kwrd"&gt;else&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;        {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;            e.ConversionErrorMessage = &lt;span class="str"&gt;"Date is not in the correct format."&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;            e.ConvertedValue = &lt;span class="kwrd"&gt;null&lt;/span&gt;;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=122893"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=122893" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/michelotti/aggbug/122893.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Steve Michelotti</dc:creator>
            <guid>http://geekswithblogs.net/michelotti/archive/2008/06/16/122893.aspx</guid>
            <pubDate>Mon, 16 Jun 2008 19:19:57 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/michelotti/comments/122893.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/michelotti/archive/2008/06/16/122893.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/michelotti/comments/commentRss/122893.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/michelotti/services/trackbacks/122893.aspx</trackback:ping>
        </item>
        <item>
            <title>EntLib Validation Application Block - Required String Validator</title>
            <link>http://geekswithblogs.net/michelotti/archive/2008/06/12/122836.aspx</link>
            <description>&lt;p&gt;In a previous &lt;a target="_blank" href="http://geekswithblogs.net/michelotti/archive/2008/06/10/122780.aspx"&gt;post&lt;/a&gt; I discussed validating strings with the VAB in the context of ASP.NET applications.  In summary, you must have a NotNullValidator and also a StringLengthValidator that prevents a string length of zero on the lower bound and prevents a string length above your upper bound (e.g., 50 characters).  That attribute might look like this:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;[StringLengthValidator(1, 50, MessageTemplate=&lt;span class="str"&gt;"First Name must be 1-50 characters."&lt;/span&gt;)] &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;But the problem is that the StringLengthValidator is really doing 2 jobs - one to check if the user entered a value and another to check the upper bound. Although including the NotNullValidator complete and correct, it doesn't do much in the context of asp.net applications which default to an empty string.  &lt;/p&gt;
&lt;p&gt;So from a user perspective, it would be nice to have one message if the user leaves it blank and another message if they've exceed the upper bound (or didn't follow an appropriate Regex pattern, etc., etc.).  In other words, it would be nice to have a RequiredStringValidator so we could write our business object like this (notice we no longer have to care about the lower bound on the string length validator):&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Person&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    [RequiredStringValidator(MessageTemplate = &lt;span class="str"&gt;"First Name is required."&lt;/span&gt;)]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    [StringLengthValidator(50, MessageTemplate = &lt;span class="str"&gt;"First Name must be less than 50 characters."&lt;/span&gt;)]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; FirstName { get; set; }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;} &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Also, specifiying a MessageTemplate on the RequiredStringValidator is optional.&lt;/p&gt;
&lt;p&gt;So creating your own custom validator to do this is actually quite easy.  The complete implementation follows:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; RequiredStringValidatorAttribute : ValueValidatorAttribute&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; Validator DoCreateValidator(Type targetType)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; RequiredStringValidator(&lt;span class="kwrd"&gt;this&lt;/span&gt;.MessageTemplate, &lt;span class="kwrd"&gt;this&lt;/span&gt;.Negated);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;}&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; RequiredStringValidator : ValueValidator&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; RequiredStringValidator(&lt;span class="kwrd"&gt;string&lt;/span&gt; messageTemplate, &lt;span class="kwrd"&gt;bool&lt;/span&gt; negated) : &lt;span class="kwrd"&gt;base&lt;/span&gt;(messageTemplate, &lt;span class="kwrd"&gt;null&lt;/span&gt;, negated)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;    &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;    &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DoValidate(&lt;span class="kwrd"&gt;string&lt;/span&gt; objectToValidate, &lt;span class="kwrd"&gt;object&lt;/span&gt; currentTarget, &lt;span class="kwrd"&gt;string&lt;/span&gt; key, ValidationResults validationResults)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;        &lt;span class="kwrd"&gt;if&lt;/span&gt; (&lt;span class="kwrd"&gt;string&lt;/span&gt;.IsNullOrEmpty(objectToValidate) != Negated)&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;        {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;            LogValidationResult(validationResults, GetMessage(objectToValidate, key), currentTarget, key);&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;        }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;    &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; DefaultNegatedMessageTemplate&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;        get&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;        {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="str"&gt;"Field cannot have a value."&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt;        }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  29:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  30:  &lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  31:  &lt;/span&gt;    &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; DefaultNonNegatedMessageTemplate&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  32:  &lt;/span&gt;    {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  33:  &lt;/span&gt;        get&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  34:  &lt;/span&gt;        {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  35:  &lt;/span&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="str"&gt;"Field is required."&lt;/span&gt;;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  36:  &lt;/span&gt;        }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  37:  &lt;/span&gt;    }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  38:  &lt;/span&gt;} &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;I've supported Negations for completeness but I can't really think of a real-world example where you'd want to use it in this context.  &lt;/p&gt;
&lt;p&gt;So overall, the RequiredStringValidator check for both null and empty string.  Thus, you don't have to feel like you're kludging the StringLengthValidator into doing two different jobs.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=122836"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=122836" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/michelotti/aggbug/122836.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Steve Michelotti</dc:creator>
            <guid>http://geekswithblogs.net/michelotti/archive/2008/06/12/122836.aspx</guid>
            <pubDate>Fri, 13 Jun 2008 03:23:24 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/michelotti/comments/122836.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/michelotti/archive/2008/06/12/122836.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/michelotti/comments/commentRss/122836.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/michelotti/services/trackbacks/122836.aspx</trackback:ping>
        </item>
        <item>
            <title>EntLib Validation Application Block - Validate a string in ASP.NET</title>
            <link>http://geekswithblogs.net/michelotti/archive/2008/06/10/122780.aspx</link>
            <description>&lt;p&gt;You've created a class like this and all your unit tests pass just fine when you check for null strings or strings greater than 50 characters:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Person&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    [NotNullValidator(MessageTemplate=&lt;span class="str"&gt;"First Name is required."&lt;/span&gt;)]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    [StringLengthValidator(50, MessageTemplate=&lt;span class="str"&gt;"First Name must be 1-50 characters."&lt;/span&gt;)]&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; FirstName { get; set; }&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;But then when you run it in an ASP.NET application with the PropertyProxyValidator validation control provided by VAB, your business object suddenly shows as valid even though the user left the text box blank.&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;asp:TextBox&lt;/span&gt; &lt;span class="attr"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;="txtFirstName"&lt;/span&gt; &lt;span class="attr"&gt;runat&lt;/span&gt;&lt;span class="kwrd"&gt;="server"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;vab:PropertyProxyValidator&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="firstNameVal"&lt;/span&gt; &lt;span class="attr"&gt;runat&lt;/span&gt;&lt;span class="kwrd"&gt;="server"&lt;/span&gt; &lt;span class="attr"&gt;ControlToValidate&lt;/span&gt;&lt;span class="kwrd"&gt;="txtFirstName"&lt;/span&gt; &lt;/pre&gt;
&lt;pre&gt;                                   &lt;span class="attr"&gt;PropertyName&lt;/span&gt;&lt;span class="kwrd"&gt;="FirstName"&lt;/span&gt; &lt;span class="attr"&gt;SourceTypeName&lt;/span&gt;&lt;span class="kwrd"&gt;="WebApplication1.Person"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Why is this happening?  The reason is because the ASP.NET infrasture will put an empty string (i.e., a zero-length string) in that property rather than a null.  In point of fact, the validator was not set up correctly to begin with.  The intent is also to prevent an empty string.  So to correct the implementation of the StringLengthValidator, you must give the lower bound of 1 as the first argument as shown here:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;div class="csharpcode"&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;[StringLengthValidator(1, 50, MessageTemplate=&lt;span class="str"&gt;"First Name must be 1-50 characters."&lt;/span&gt;)] &lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Additionally, the VAB will allow you to specify whether you want the number specified for the lower/upper bound to be considered inclusvie/exclusive for the range.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=122780"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=122780" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/michelotti/aggbug/122780.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Steve Michelotti</dc:creator>
            <guid>http://geekswithblogs.net/michelotti/archive/2008/06/10/122780.aspx</guid>
            <pubDate>Wed, 11 Jun 2008 01:27:42 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/michelotti/comments/122780.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/michelotti/archive/2008/06/10/122780.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/michelotti/comments/commentRss/122780.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/michelotti/services/trackbacks/122780.aspx</trackback:ping>
        </item>
        <item>
            <title>BizTalk Web Service - SoapException: Internal SOAP Processing Failure</title>
            <link>http://geekswithblogs.net/michelotti/archive/2006/11/23/97953.aspx</link>
            <description>&lt;P&gt;When exposing a BizTalk orchestration as a web service, I ran into a basic problem that was not obvious to me at first.&amp;nbsp; When try to invoke the web service I got the exception: "SoapException: Internal SOAP Processing Failure".&amp;nbsp; The basic BizTalk help offered no insight into this problem.&amp;nbsp; The answer can be found on MSDN however.&amp;nbsp; According to MSDN:&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#0000ff&gt;Check the identity under which the application pool is running. Is it a member of the IIS_WPG and BizTalk Isolated Host Users groups? These two groups give the ASMX page (the proxy Web service that the BizTalk Web Service Publishing wizard generated) the appropriate rights to insert the SOAP request message into the BizTalk MessageBox in SQL Server that will then launch the orchestration. Symptom may be "SoapException: Internal SOAP Processing Failure".&lt;BR&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;This fixed the problem for me.&lt;/P&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=97953"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=97953" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/michelotti/aggbug/97953.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Steve Michelotti</dc:creator>
            <guid>http://geekswithblogs.net/michelotti/archive/2006/11/23/97953.aspx</guid>
            <pubDate>Fri, 24 Nov 2006 00:07:00 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/michelotti/comments/97953.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/michelotti/archive/2006/11/23/97953.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/michelotti/comments/commentRss/97953.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/michelotti/services/trackbacks/97953.aspx</trackback:ping>
        </item>
        <item>
            <title>Consolas Font for Visual Studio 2005</title>
            <link>http://geekswithblogs.net/michelotti/archive/2006/06/27/83339.aspx</link>
            <description>&lt;P&gt;At Tech Ed this year, the presenter at one of the Windows Presentation Foundation talks was raving about the new Consolas ClearType font was was recently developed for Visual Studio 2005.&amp;nbsp; I have downloaded it and I must say it is the best fixed width developer font I have ever seen.&amp;nbsp; Here is an example screen shot (although my screen shot really does not do it justice):&lt;/P&gt;&lt;IMG src="/images/geekswithblogs_net/michelotti/4145/r_consolas.jpg"&gt; 
&lt;P&gt;You can download the font for free here:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.microsoft.com/downloads/details.aspx?familyid=22e69ae4-7e40-4807-8a86-b3d36fab68d3&amp;amp;displaylang=en"&gt;http://www.microsoft.com/downloads/details.aspx?familyid=22e69ae4-7e40-4807-8a86-b3d36fab68d3&amp;amp;displaylang=en&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;You have to make sure that your system has ClearType is turned on.&amp;nbsp; The first question (&amp;#8221;How do I turn it on?&amp;#8221;) at this link will show you how:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.microsoft.com/typography/ClearTypeFAQ.mspx"&gt;http://www.microsoft.com/typography/ClearTypeFAQ.mspx&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=83339"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=83339" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/michelotti/aggbug/83339.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Steve Michelotti</dc:creator>
            <guid>http://geekswithblogs.net/michelotti/archive/2006/06/27/83339.aspx</guid>
            <pubDate>Tue, 27 Jun 2006 21:30:00 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/michelotti/comments/83339.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/michelotti/archive/2006/06/27/83339.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/michelotti/comments/commentRss/83339.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/michelotti/services/trackbacks/83339.aspx</trackback:ping>
        </item>
        <item>
            <title>SuppressMessage with FxCop and NON-Team System Visual Studio</title>
            <link>http://geekswithblogs.net/michelotti/archive/2006/01/11/65548.aspx</link>
            <description>&lt;P&gt;If you're using Visual Studio 2005 Team System then FxCop is completely integrated and when you exclude messages it automatically applies the new SuppressMessage attributes to the appropriate location in code.  But if you're using Visual Studio Professional edition then you have to use the external FxCop 1.35 UI.  If this is the case, you can STILL utilize the SuppressMessage attribute but have to do a couple of things first.  I had to do a little digging but the steps are pretty simple.&lt;/P&gt;
&lt;P&gt; &lt;/P&gt;&lt;FONT size=2&gt;
&lt;P&gt;It turns out you have to enable your project to recognize the SuppressMessage attribute by FIRST adding a condition compilation symbol.&lt;/P&gt;
&lt;P&gt;To include this symbol do the following:&lt;BR&gt;C#&lt;BR&gt;   1. In &lt;STRONG&gt;Solution Explorer&lt;/STRONG&gt;, right-click your project and choose &lt;STRONG&gt;Properties&lt;/STRONG&gt;&lt;BR&gt;   2. In the &lt;STRONG&gt;Properties&lt;/STRONG&gt; window, choose the &lt;STRONG&gt;Build&lt;/STRONG&gt; tab&lt;BR&gt;   3. In the &lt;STRONG&gt;Conditional compilation symbols&lt;/STRONG&gt; text box enter &lt;STRONG&gt;CODE_ANALYSIS&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;VB&lt;BR&gt;   1. In &lt;STRONG&gt;Solution Explorer&lt;/STRONG&gt;, right-click your project and choose &lt;STRONG&gt;Properties&lt;/STRONG&gt;&lt;BR&gt;   2. In the &lt;STRONG&gt;Properties&lt;/STRONG&gt; window, choose the &lt;STRONG&gt;Compile&lt;/STRONG&gt; tab and click &lt;STRONG&gt;Advanced Compile Options&lt;/STRONG&gt;&lt;BR&gt;   3. In the &lt;STRONG&gt;Custom&lt;/STRONG&gt; &lt;STRONG&gt;constants&lt;/STRONG&gt; text box enter &lt;STRONG&gt;CODE_ANALYSIS&lt;/STRONG&gt;&lt;BR&gt;Found these steps   &lt;A href="http://blogs.msdn.com/fxcop/archive/2006/03/23/559149.aspx"&gt;here&lt;/A&gt;:&lt;/P&gt;
&lt;P&gt;Now for the cool part.   In Team System, you can just right-click on the rule and say &amp;#8220;Suppress&amp;#8221; and it will automatically add the SuppressMessage attribute in the right spot.  In Professional edition, we don&amp;#8217;t have this integrated into the IDE so we have to use the normal FxCop 1.35 UI (which of course has no knowledge of Visual Studio) &amp;#8211; and correctly specifying these attributes is not easy to do by hand.  It turns out all you have to do, is to right-click the error message in FxCop UI and select &amp;#8220;Copy As&amp;#8221; &amp;#8211; &amp;#8220;SuppressMessage&amp;#8221; (or &amp;#8220;Module-level SupressMessage&amp;#8221;).  Then you can simply paste the attribute into your code at the appropriate location.  Very slick!&lt;BR&gt;&lt;/P&gt;&lt;/FONT&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=65548"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=65548" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/michelotti/aggbug/65548.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Steve Michelotti</dc:creator>
            <guid>http://geekswithblogs.net/michelotti/archive/2006/01/11/65548.aspx</guid>
            <pubDate>Wed, 11 Jan 2006 18:40:00 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/michelotti/comments/65548.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/michelotti/archive/2006/01/11/65548.aspx#feedback</comments>
            <slash:comments>4</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/michelotti/comments/commentRss/65548.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/michelotti/services/trackbacks/65548.aspx</trackback:ping>
        </item>
        <item>
            <title>Not all string comparisons created equal</title>
            <link>http://geekswithblogs.net/michelotti/archive/2005/12/11/62841.aspx</link>
            <description>&lt;P class=MsoNormal dir=ltr style="MARGIN: 0in 0in 0pt"&gt;Not all string comparisons will perform the same but I was interested in seeing what exactly the differences were. Regardless of whether I was just performing 1 string comparison or 1 million, using the instance Equals() method was BY FAR the fastest. Of course the danger there is it will fail if the first string is null. Using == will always work regardless of nulls and for a million comparisons was almost as fast as Equals() and WAY faster than any other method (although it should be noted that using == is identical to using the static String.Equals() method). However, for 1 comparison it was by far the slowest. Note, for case-insensitive comparisons it is much faster to use the Compare() method with the case insensitive switch. Here are results below: &lt;BR&gt;&lt;BR&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;U&gt;1 COMPARISON: &lt;BR&gt;&lt;/U&gt;&lt;/B&gt;&lt;SPAN style="COLOR: red"&gt;first == second              &lt;SPAN style="mso-tab-count: 3"&gt;                                   &lt;/SPAN&gt;: 0.00021175875704873100 &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;first.Equals(second)            &lt;SPAN style="mso-tab-count: 3"&gt;                              &lt;/SPAN&gt;: 0.00000866031856004045 &lt;BR&gt;&lt;/SPAN&gt;(first.CompareTo(second) == -1)      &lt;SPAN style="mso-tab-count: 2"&gt;               &lt;/SPAN&gt;: 0.00005531429273832290 &lt;BR&gt;(string.Compare(first, second) == -1)   &lt;SPAN style="mso-tab-count: 1"&gt; &lt;SPAN style="FONT-SIZE: 12pt; COLOR: blue; FONT-FAMILY: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;            &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;: 0.00001396825574200070 &lt;BR&gt;string.Compare(first, second, true) == -1&lt;SPAN style="mso-tab-count: 1"&gt;          &lt;/SPAN&gt;: 0.00001145396970844060 &lt;BR&gt;first.ToLower() == second.ToLower()    &lt;SPAN style="mso-tab-count: 1"&gt;          &lt;/SPAN&gt;: 0.00002458413010592130 &lt;BR&gt;&lt;BR&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;U&gt;1000000 COMPARISONS: &lt;BR&gt;&lt;/U&gt;&lt;/B&gt;first == second              &lt;SPAN style="mso-tab-count: 3"&gt;                                   &lt;/SPAN&gt;: 0.05631637540525400000 &lt;BR&gt;&lt;SPAN style="COLOR: blue"&gt;first.Equals(second)            &lt;SPAN style="mso-tab-count: 3"&gt;                              &lt;/SPAN&gt;: 0.03321874707539650000 &lt;BR&gt;&lt;/SPAN&gt;(first.CompareTo(second) == -1)      &lt;SPAN style="mso-tab-count: 2"&gt;               &lt;/SPAN&gt;: 0.28999719238059600000 &lt;BR&gt;&lt;SPAN style="COLOR: red"&gt;(string.Compare(first, second) == -1)   &lt;SPAN style="FONT-SIZE: 12pt; COLOR: blue; FONT-FAMILY: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;            &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-tab-count: 1"&gt; &lt;/SPAN&gt;: 0.29059363690077900000 &lt;BR&gt;&lt;/SPAN&gt;string.Compare(first, second, true) == -1&lt;SPAN style="mso-tab-count: 1"&gt;          &lt;/SPAN&gt;: 0.30738375966777900000 &lt;BR&gt;first.ToLower() == second.ToLower()    &lt;SPAN style="mso-tab-count: 1"&gt;          &lt;/SPAN&gt;: 0.77666714624344700000 &lt;BR&gt;&lt;BR&gt;Incidentally, a look at the instance Equals() method in Reflector: &lt;BR&gt;[MethodImpl(MethodImplOptions.InternalCall)] &lt;BR&gt;public extern bool Equals(string value);&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Doing a million string comparisons in a loop might not be your common every day programming task and the timing difference will obviously not be perceptible to a human.&lt;SPAN style="mso-spacerun: yes"&gt;  &lt;/SPAN&gt;But let's say you have a web page that does a single string comparison - if that page is accessed in a high volume environment (by thousands of browsers at once), then every little performance enhancement you can make is quite important.&lt;/P&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=62841"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=62841" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/michelotti/aggbug/62841.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Steve Michelotti</dc:creator>
            <guid>http://geekswithblogs.net/michelotti/archive/2005/12/11/62841.aspx</guid>
            <pubDate>Sun, 11 Dec 2005 15:03:00 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/michelotti/comments/62841.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/michelotti/archive/2005/12/11/62841.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/michelotti/comments/commentRss/62841.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/michelotti/services/trackbacks/62841.aspx</trackback:ping>
        </item>
    </channel>
</rss>