<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Arrays with non-zero upper and lower bounds...</title><link>http://blogs.msdn.com/ericgu/archive/2004/03/18/92353.aspx</link><description>In the comments to my post on zero and one based arrays, several people mentioned that they wanted to be able to have collections that ran from 4 to 10, or from 1900 to 2004 for years. Consider the following: public class YearClass { const int StartDate</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Arrays with non-zero upper and lower bounds...</title><link>http://blogs.msdn.com/ericgu/archive/2004/03/18/92353.aspx#92370</link><pubDate>Fri, 19 Mar 2004 00:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:92370</guid><dc:creator>Bill</dc:creator><description>That's nice.  Very nice solution for right now, with current tools.&lt;br&gt;&lt;br&gt;But what we really want is to code our business problems declaratively.  So if you built a little collection-class code generator that would build that little bit of code, plus bounds checking, that would be totally awesome.&lt;br&gt;&lt;br&gt;Essentially, we don't want to be bothered with error-prone code, cut-and-pasted from collection to collection, that we must worry about testing in each every location that it's used.  We just want to use a relatively direct model of our businesses.&lt;br&gt;&lt;br&gt;MS has done a really great job with the low-level stuff.  Now let's build some use&lt;br&gt;&lt;br&gt;P.S. Challenge/Puzzle&lt;br&gt;Could the code you published by refactored into and Interface or Base class, so that all such collections provide start and end indexes in exactly the same way?</description></item><item><title>re: Arrays with non-zero upper and lower bounds...</title><link>http://blogs.msdn.com/ericgu/archive/2004/03/18/92353.aspx#92381</link><pubDate>Fri, 19 Mar 2004 00:36:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:92381</guid><dc:creator>Rick Ross</dc:creator><description>In order to avoid a System.IndexOutOfRangeException, I would check the index to insure that it is within the appropriate range.</description></item><item><title>re: Arrays with non-zero upper and lower bounds...</title><link>http://blogs.msdn.com/ericgu/archive/2004/03/18/92353.aspx#92505</link><pubDate>Fri, 19 Mar 2004 05:39:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:92505</guid><dc:creator>Steve Maine</dc:creator><description>public class OffsetArray&amp;lt;T&amp;gt;&lt;br&gt;{&lt;br&gt;   readonly int start;&lt;br&gt;   readonly int end;&lt;br&gt;   T[] arr; &lt;br&gt;  &lt;br&gt;   public OffsetCollection&amp;lt;T&amp;gt;( int start, int end)&lt;br&gt;   {&lt;br&gt;     this.start = start;&lt;br&gt;     this.end   = end;&lt;br&gt;     this.arr   = new T[ end - start + 1];&lt;br&gt;   }&lt;br&gt;&lt;br&gt;   public T this[int num]&lt;br&gt;   {&lt;br&gt;      get { return arr[ num - start]; }&lt;br&gt;      set { arr[num - start] = value; }&lt;br&gt;   }&lt;br&gt;}&lt;br&gt;&lt;br&gt;You'd probably want to sanity check start and end in the constructor. Requesting an index that's out of range *should* throw an IndexOutOfRangeException, IMHO.</description></item><item><title>re: Arrays with non-zero upper and lower bounds...</title><link>http://blogs.msdn.com/ericgu/archive/2004/03/18/92353.aspx#92577</link><pubDate>Fri, 19 Mar 2004 10:35:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:92577</guid><dc:creator>Alfred Thompson</dc:creator><description>You know I looked at that and thought &amp;quot;why didn't I think of that?&amp;quot; The reason is of course that I've been programming too long with the old model of programming and things like this, which should probably be obvious to someone who has really internalized OOP, don't come natural to me. This will get me thinking in some new directions. Thanks.</description></item><item><title>re: Arrays with non-zero upper and lower bounds...</title><link>http://blogs.msdn.com/ericgu/archive/2004/03/18/92353.aspx#92589</link><pubDate>Fri, 19 Mar 2004 11:28:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:92589</guid><dc:creator>Rick Ross</dc:creator><description>Steve, in most cases, I would tend to agree with you that throwing an IndexOutOfRangeException would be appropriate. However, in this case, it would be more appropriate, IMHO, to raise a custom expression like DateOutOfRangeException. This gives the user of the class a better clue as to what the problem is.</description></item><item><title>re: Arrays with non-zero upper and lower bounds...</title><link>http://blogs.msdn.com/ericgu/archive/2004/03/18/92353.aspx#92811</link><pubDate>Fri, 19 Mar 2004 18:14:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:92811</guid><dc:creator>Dilton McGowan II</dc:creator><description>What do templates have to do with C#? Surely they aren't going to show up in future versions since that would uglify the currently readable C# syntax. Eric's solution is ok for now. Just move that into the core and call it a day.</description></item><item><title>re: Arrays with non-zero upper and lower bounds...</title><link>http://blogs.msdn.com/ericgu/archive/2004/03/18/92353.aspx#93139</link><pubDate>Sat, 20 Mar 2004 08:58:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:93139</guid><dc:creator>Thomas Eyde</dc:creator><description>Shouldn't it be StartYear, EndYear and YearOutOfRangeException? I think IndexOutOfRangeException is just fine, because I am after all indexing it.</description></item><item><title>re: Arrays with non-zero upper and lower bounds...</title><link>http://blogs.msdn.com/ericgu/archive/2004/03/18/92353.aspx#93472</link><pubDate>Sun, 21 Mar 2004 14:20:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:93472</guid><dc:creator>Douglas McClean</dc:creator><description>You can also use Array.CreateInstance(Type, int[], int[]) to create an array with a specified element type, and specified dimensionality, lengths, and lower bounds.</description></item><item><title>Workaround: Arrays with non-zero lower bounds</title><link>http://blogs.msdn.com/ericgu/archive/2004/03/18/92353.aspx#116668</link><pubDate>Tue, 20 Apr 2004 14:39:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:116668</guid><dc:creator>Visual Basic Frequently Asked Questions</dc:creator><description /></item><item><title>Recalling Visual Fred</title><link>http://blogs.msdn.com/ericgu/archive/2004/03/18/92353.aspx#117181</link><pubDate>Wed, 21 Apr 2004 02:42:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:117181</guid><dc:creator>Mark Treadwell</dc:creator><description /></item><item><title>Recalling Visual Fred</title><link>http://blogs.msdn.com/ericgu/archive/2004/03/18/92353.aspx#117187</link><pubDate>Wed, 21 Apr 2004 02:59:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:117187</guid><dc:creator>Mark Treadwell</dc:creator><description /></item></channel></rss>