<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" 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:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>OOP - There It Is - Generics</title>
    <link>http://www.vpsw.com/blogbaby/</link>
    <description>A Very Practical Blog</description>
    <language>en-us</language>
    <copyright>Dean Fiala</copyright>
    <lastBuildDate>Wed, 06 Jun 2007 03:46:14 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.1.8102.813</generator>
    <managingEditor>dfiala@vpsw.com</managingEditor>
    <webMaster>dfiala@vpsw.com</webMaster>
    <item>
      <trackback:ping>http://www.vpsw.com/blogbaby/Trackback.aspx?guid=a1478711-4218-4c4c-a102-ad0eb2246a40</trackback:ping>
      <pingback:server>http://www.vpsw.com/blogbaby/pingback.aspx</pingback:server>
      <pingback:target>http://www.vpsw.com/blogbaby/PermaLink,guid,a1478711-4218-4c4c-a102-ad0eb2246a40.aspx</pingback:target>
      <dc:creator>Dean</dc:creator>
      <wfw:comment>http://www.vpsw.com/blogbaby/CommentView,guid,a1478711-4218-4c4c-a102-ad0eb2246a40.aspx</wfw:comment>
      <wfw:commentRss>http://www.vpsw.com/blogbaby/SyndicationService.asmx/GetEntryCommentsRss?guid=a1478711-4218-4c4c-a102-ad0eb2246a40</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">I hate writing the same code twice. 
Not just because I am a horrible typist, but because writing the same code twice means
testing it twice, debugging it twice, maintaining it twice, etc.  I recently
ran into a situation where this looked unavoidable.  I needed to write (a little
more than) a wee bit of code that evaluated values against arbitrary conditions in
the same way.  The twist was that these conditions could have different types.  
<br /><br />
I didn't want to duplicate the code for each type. However, I also wanted it to be
strongly typed, so I didn't want to resort to using the object type for everything. 
Since all the types in question are numeric, I could have simply coerced everything
into a decimal or a double type and written the bulk of the code against that one
type.  But I didn't like this for three reasons: 
<br /><ol><li>
It would be imprecise: I wanted to compare integers to integers when I could<br /></li><li>
It would require adding a layer of conversion logic</li><li>
It just didn't feel right.  It wasn't elegant.</li></ol>
Not all code is elegant, but it seems to flow much more easily and get working more
quickly when it is.  So while I was counting up the types I'd need to support
and contemplating the heinous sin of cut and paste coding, it occurred to me that
.NET 2.0 should allow me to solve this very elegantly, using <a href="http://msdn2.microsoft.com/en-US/library/512aeb7t%28vs.80%29.aspx">Generics</a>.  
<br /><br />
I have already fallen in love with the generic collections and have kicked the ArrayList
to the curb, so it was time to figure out how to make a generic class of my own devising.<br /><br />
The &lt;T&gt; is the key...<br /><br /><blockquote><font color="#0000ff" face="Courier New">public class OneRuleToBindThem&lt;T&gt; </font><br /></blockquote><br />
This tells the complier that the class will be created for the specified type in &lt;&gt;.<br /><br />
So I whipped up my first generic class. The simplified version looked vaguely like
this....<br /><br /><blockquote><font color="#0000ff" face="Courier New">public class OneRuleToBindThem&lt;T&gt;<br />
{<br />
    private T mMinimumValue;<br />
    private T mMaximumValue;<br />
    private const string PASS = "Pass";<br />
    private const string FAIL = "Fail";<br /><br />
    public OneRuleToBindThem(T MinimumValue, T MaximumValue)<br />
    {<br />
        mMinimumValue = MinimumValue;<br />
        mMaximumValue = MaximumValue;<br />
    }<br /><br />
    public string Evaluate(T ObservedValue)<br />
    {<br />
        string Result = PASS;<br />
        if (ObservedValue &gt; mMaximumValue)<br />
            Result = FAIL;<br />
        if (ObservedValue &lt; mMinimumValue)<br />
            Result = FAIL;<br /><br />
        return Result;<br />
    }<br />
}</font><br /></blockquote><br /><br />
That was too easy!  Let's compile it and move on...<br /><br /><blockquote><b><font color="#ff0000">Operator '&gt;' cannot be applied to operands
of type 'T' and 'T'   </font></b><br /></blockquote><br />
What in the la-di-da does that mean? Shazbot.  Why can't I compare a T with a
T?  They'll be the same type for pete's sake.  A little googling and reading
and OK, the compiler doesn't know if a supplied type will support the given operand,
so it properly throws an error.  Surely there must be a way around this, some
way to reassure the compiler that only appropriate types will be used, really!<br /><br />
A hint from <a href="http://community.strongcoders.com/blogs/ryan/default.aspx">Ryan
Olshan</a> led me to check out IComparable. Ah, it is possible to <a href="http://msdn2.microsoft.com/en-US/library/d5x73970%28VS.80%29.aspx">constrain</a> the
types that can instantiate the class.  So I tried...<br /><br /><blockquote><font color="#0000ff" face="Courier New">public class OneRuleToBindThem&lt;T&gt;
where T:IComparable</font><br /><font color="#0000ff" face="Courier New">{<br /></font><blockquote><font color="#0000ff" face="Courier New">//code you've seen before...</font><br /></blockquote><font color="#0000ff" face="Courier New">}<br /></font></blockquote><br />
Compile and...the same errors appear in the build.  Fudgesticks.<br /><br />
An idea flickered.  Since I would only be using types that implement IComparable,
maybe I could use the interface's <a href="http://msdn2.microsoft.com/en-us/library/system.icomparable.compareto.aspx">CompareTo</a> method
somehow.<br /><br />
So, I rewrote the Evaluate method...<br /><br /><blockquote><p><font color="#0000ff"><font face="Courier New">public string Evaluate(T ObservedValue)</font></font><font color="#0000ff" face="Courier New"><br />
{</font></p></blockquote><font color="#0000ff"><font face="Courier New">      
string Result = PASS;<br />
        if (ObservedValue.CompareTo(mMaximumValue)
&gt; 0)<br />
            Result = FAIL;<br />
        if (ObservedValue.CompareTo(mMinimumValue)
&lt; 0)<br />
            Result = FAIL;<br /><br />
        return Result;<br />
    }<br /></font></font><br />
F5 to compile and -- hoorah.  The darn thing works, well compiles anyway. 
Let's see if it actually works with a little test code...<br /><br /><font color="#0000ff"><font face="Courier New">       
OneRuleToBindThem&lt;int&gt; AnIntegerRule = new OneRuleToBindThem&lt;int&gt;(4, 10);<br />
        for (int Index = 1; Index &lt;= 11; Index++)<br />
        {<br />
            Debug.WriteLine(String.Format("{0}
: {1}", Index, AnIntegerRule.Evaluate(Index)));<br />
        }<br /><br />
        OneRuleToBindThem&lt;decimal&gt; ADecimalRule
= new OneRuleToBindThem&lt;decimal&gt;(4, 10);<br />
        for (decimal Index = 1M; Index &lt;= 11M;
Index+= 0.2M)<br />
        {<br />
            Debug.WriteLine(String.Format("{0}
: {1}", Index, ADecimalRule.Evaluate(Index)));<br />
        }</font></font><br /><br />
Good golly.  Worked on the first, ahem, try.  Now is the time on "OOP -
There it Is" when we dance.<br /><br />
Granted the example Evaluate method is only a few lines of code (and could be slimmer).
In this example it wouldn't be too onerous to store the MinimumValue and MaximumValue
as doubles, and then write an overloaded Evaluate method for each type using the appropriate
convert functions.  But when Evaluate is 20+ lines of code and the class needs
to support 3+ types, that's a shameful amount of cut-and-paste code. Shameful. 
<br /><br />
But with the Generic implementation, everything is strongly typed. And best of all,
there's only one method to test, debug and maintain! Once again, my fear of typing
led to a sweet little implementation.<br /><p></p><img width="0" height="0" src="http://www.vpsw.com/blogbaby/aggbug.ashx?id=a1478711-4218-4c4c-a102-ad0eb2246a40" /></body>
      <title>Operands, I Don't Need No Stinkin' Operands</title>
      <guid isPermaLink="false">http://www.vpsw.com/blogbaby/PermaLink,guid,a1478711-4218-4c4c-a102-ad0eb2246a40.aspx</guid>
      <link>http://www.vpsw.com/blogbaby/PermaLink,guid,a1478711-4218-4c4c-a102-ad0eb2246a40.aspx</link>
      <pubDate>Wed, 06 Jun 2007 03:46:14 GMT</pubDate>
      <description>I hate writing the same code twice.&amp;nbsp; Not just because I am a horrible typist, but because writing the same code twice means testing it twice, debugging it twice, maintaining it twice, etc.&amp;nbsp; I recently ran into a situation where this looked unavoidable.&amp;nbsp; I needed to write (a little more than) a wee bit of code that evaluated values against arbitrary conditions in the same way.&amp;nbsp; The twist was that these conditions could have different types.&amp;nbsp; &lt;br&gt;
&lt;br&gt;
I didn't want to duplicate the code for each type. However, I also wanted it to be
strongly typed, so I didn't want to resort to using the object type for everything.&amp;nbsp;
Since all the types in question are numeric, I could have simply coerced everything
into a decimal or a double type and written the bulk of the code against that one
type.&amp;nbsp; But I didn't like this for three reasons: 
&lt;br&gt;
&lt;ol&gt;
&lt;li&gt;
It would be imprecise: I wanted to compare integers to integers when I could&lt;br&gt;
&lt;/li&gt;
&lt;li&gt;
It would require adding a layer of conversion logic&lt;/li&gt;
&lt;li&gt;
It just didn't feel right.&amp;nbsp; It wasn't elegant.&lt;/li&gt;
&lt;/ol&gt;
Not all code is elegant, but it seems to flow much more easily and get working more
quickly when it is.&amp;nbsp; So while I was counting up the types I'd need to support
and contemplating the heinous sin of cut and paste coding, it occurred to me that
.NET 2.0 should allow me to solve this very elegantly, using &lt;a href="http://msdn2.microsoft.com/en-US/library/512aeb7t%28vs.80%29.aspx"&gt;Generics&lt;/a&gt;.&amp;nbsp; 
&lt;br&gt;
&lt;br&gt;
I have already fallen in love with the generic collections and have kicked the ArrayList
to the curb, so it was time to figure out how to make a generic class of my own devising.&lt;br&gt;
&lt;br&gt;
The &amp;lt;T&amp;gt; is the key...&lt;br&gt;
&lt;br&gt;
&lt;blockquote&gt;&lt;font color="#0000ff" face="Courier New"&gt;public class OneRuleToBindThem&amp;lt;T&amp;gt; &lt;/font&gt;
&lt;br&gt;
&lt;/blockquote&gt;
&lt;br&gt;
This tells the complier that the class will be created for the specified type in &amp;lt;&amp;gt;.&lt;br&gt;
&lt;br&gt;
So I whipped up my first generic class. The simplified version looked vaguely like
this....&lt;br&gt;
&lt;br&gt;
&lt;blockquote&gt;&lt;font color="#0000ff" face="Courier New"&gt;public class OneRuleToBindThem&amp;lt;T&amp;gt;&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; private T mMinimumValue;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; private T mMaximumValue;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; private const string PASS = "Pass";&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; private const string FAIL = "Fail";&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public OneRuleToBindThem(T MinimumValue, T MaximumValue)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mMinimumValue = MinimumValue;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mMaximumValue = MaximumValue;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public string Evaluate(T ObservedValue)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string Result = PASS;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (ObservedValue &amp;gt; mMaximumValue)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Result = FAIL;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (ObservedValue &amp;lt; mMinimumValue)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Result = FAIL;&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return Result;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
}&lt;/font&gt;
&lt;br&gt;
&lt;/blockquote&gt;
&lt;br&gt;
&lt;br&gt;
That was too easy!&amp;nbsp; Let's compile it and move on...&lt;br&gt;
&lt;br&gt;
&lt;blockquote&gt;&lt;b&gt;&lt;font color="#ff0000"&gt;Operator '&amp;gt;' cannot be applied to operands
of type 'T' and 'T'&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/b&gt;
&lt;br&gt;
&lt;/blockquote&gt;
&lt;br&gt;
What in the la-di-da does that mean? Shazbot.&amp;nbsp; Why can't I compare a T with a
T?&amp;nbsp; They'll be the same type for pete's sake.&amp;nbsp; A little googling and reading
and OK, the compiler doesn't know if a supplied type will support the given operand,
so it properly throws an error.&amp;nbsp; Surely there must be a way around this, some
way to reassure the compiler that only appropriate types will be used, really!&lt;br&gt;
&lt;br&gt;
A hint from &lt;a href="http://community.strongcoders.com/blogs/ryan/default.aspx"&gt;Ryan
Olshan&lt;/a&gt; led me to check out IComparable. Ah, it is possible to &lt;a href="http://msdn2.microsoft.com/en-US/library/d5x73970%28VS.80%29.aspx"&gt;constrain&lt;/a&gt; the
types that can instantiate the class.&amp;nbsp; So I tried...&lt;br&gt;
&lt;br&gt;
&lt;blockquote&gt;&lt;font color="#0000ff" face="Courier New"&gt;public class OneRuleToBindThem&amp;lt;T&amp;gt;
where T:IComparable&lt;/font&gt;
&lt;br&gt;
&lt;font color="#0000ff" face="Courier New"&gt;{&lt;br&gt;
&lt;/font&gt;&lt;blockquote&gt;&lt;font color="#0000ff" face="Courier New"&gt;//code you've seen before...&lt;/font&gt;
&lt;br&gt;
&lt;/blockquote&gt;&lt;font color="#0000ff" face="Courier New"&gt;}&lt;br&gt;
&lt;/font&gt;&lt;/blockquote&gt;
&lt;br&gt;
Compile and...the same errors appear in the build.&amp;nbsp; Fudgesticks.&lt;br&gt;
&lt;br&gt;
An idea flickered.&amp;nbsp; Since I would only be using types that implement IComparable,
maybe I could use the interface's &lt;a href="http://msdn2.microsoft.com/en-us/library/system.icomparable.compareto.aspx"&gt;CompareTo&lt;/a&gt; method
somehow.&lt;br&gt;
&lt;br&gt;
So, I rewrote the Evaluate method...&lt;br&gt;
&lt;br&gt;
&lt;blockquote&gt;
&lt;p&gt;
&lt;font color="#0000ff"&gt;&lt;font face="Courier New"&gt;public string Evaluate(T ObservedValue)&lt;/font&gt;&lt;/font&gt;&lt;font color="#0000ff" face="Courier New"&gt;
&lt;br&gt;
{&lt;/font&gt;
&lt;/p&gt;
&lt;/blockquote&gt;&lt;font color="#0000ff"&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
string Result = PASS;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (ObservedValue.CompareTo(mMaximumValue)
&amp;gt; 0)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Result = FAIL;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (ObservedValue.CompareTo(mMinimumValue)
&amp;lt; 0)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Result = FAIL;&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return Result;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&lt;/font&gt;&lt;/font&gt;
&lt;br&gt;
F5 to compile and -- hoorah.&amp;nbsp; The darn thing works, well compiles anyway.&amp;nbsp;
Let's see if it actually works with a little test code...&lt;br&gt;
&lt;br&gt;
&lt;font color="#0000ff"&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;
OneRuleToBindThem&amp;lt;int&amp;gt; AnIntegerRule = new OneRuleToBindThem&amp;lt;int&amp;gt;(4, 10);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (int Index = 1; Index &amp;lt;= 11; Index++)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Debug.WriteLine(String.Format("{0}
: {1}", Index, AnIntegerRule.Evaluate(Index)));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OneRuleToBindThem&amp;lt;decimal&amp;gt; ADecimalRule
= new OneRuleToBindThem&amp;lt;decimal&amp;gt;(4, 10);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (decimal Index = 1M; Index &amp;lt;= 11M;
Index+= 0.2M)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Debug.WriteLine(String.Format("{0}
: {1}", Index, ADecimalRule.Evaluate(Index)));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/font&gt;
&lt;br&gt;
&lt;br&gt;
Good golly.&amp;nbsp; Worked on the first, ahem, try.&amp;nbsp; Now is the time on "OOP -
There it Is" when we dance.&lt;br&gt;
&lt;br&gt;
Granted the example Evaluate method is only a few lines of code (and could be slimmer).
In this example it wouldn't be too onerous to store the MinimumValue and MaximumValue
as doubles, and then write an overloaded Evaluate method for each type using the appropriate
convert functions.&amp;nbsp; But when Evaluate is 20+ lines of code and the class needs
to support 3+ types, that's a shameful amount of cut-and-paste code. Shameful. 
&lt;br&gt;
&lt;br&gt;
But with the Generic implementation, everything is strongly typed. And best of all,
there's only one method to test, debug and maintain! Once again, my fear of typing
led to a sweet little implementation.&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.vpsw.com/blogbaby/aggbug.ashx?id=a1478711-4218-4c4c-a102-ad0eb2246a40" /&gt;</description>
      <comments>http://www.vpsw.com/blogbaby/CommentView,guid,a1478711-4218-4c4c-a102-ad0eb2246a40.aspx</comments>
      <category>2.0</category>
      <category>Generics</category>
    </item>
  </channel>
</rss>