CodeSmith Community
Your Code. Your Way. Faster!

Hashtable.Keys.CopyTo() when hashtable is empty throws ArgumentException

Latest post 05-13-2004 2:11 PM by cnahr. 3 replies.
  • 03-23-2004 4:08 PM

    • kblaney
    • Not Ranked
    • Joined on 03-23-2004
    • Posts 3
    • Points 100

    Hashtable.Keys.CopyTo() when hashtable is empty throws ArgumentException

    My CodeSmith-generated hashtable throws an exception in a case when Hashtable does not.
     
    When I use CodeSmith to generate a StringCarHashtable class where the keys are of type string and the values are of type Car (a class), the following code throws an ArgumentException.
     
    StringCarHashtable stringCarHashtable = new StringCarHashtable();
    string[] stringArray = new string[ stringCarHashtable.Keys.Count ];
    stringCarHashtable.Keys.CopyTo( stringArray, 0 );
    When I replace StringCarHashtable with Hashtable, no exception is thrown.
     
    The exception throw when StringCarHashtable is used is as follows:
     
    Unhandled Exception: System.ArgumentException: Argument must be less than array length.
    Parameter name: arrayIndex
       at ConsoleApplication1.StringCarHashtable.CheckTargetArray(Array array, Int32 arrayIndex) in d:\visualstudioprojects\consoleapplication1\class3.cs:line 2443
     
    Kyle Blaney
    • Post Points: 90
  • 03-23-2004 6:00 PM In reply to

    • kblaney
    • Not Ranked
    • Joined on 03-23-2004
    • Posts 3
    • Points 100

    RE: Hashtable.Keys.CopyTo() when hashtable is empty throws ArgumentException

    I believe this problem can be fixed by changing the implementation of the CheckTargetArray method in Hashtable.cst as follows:

    Change this line:

    if (arrayIndex >= array.Length)
    throw new ArgumentException(
    "Argument must be less than array length.", "arrayIndex");

    to the following:

    if ((arrayIndex >= array.Length) &&
    ((arrayIndex != 0) || (array.Length != 0)))
    throw new ArgumentException(
    "Argument must be less than array length.", "arrayIndex");

    Kyle Blaney
    • Post Points: 5
  • 03-23-2004 7:02 PM In reply to

    • ejsmith
    • Top 10 Contributor
    • Joined on 12-27-2002
    • Dallas, TX USA
    • Posts 2,204
    • Points 922,455

    RE: Hashtable.Keys.CopyTo() when hashtable is empty throws ArgumentException

    Kyle,

    I will pass this on to Chris. He is the maintainer of these collection templates.

    Thanks,
    Eric J. Smith

    Eric J. Smith
    CodeSmith Tools, LLC
    Chief Software Architect

    • Post Points: 5
  • 03-29-2004 5:46 PM In reply to

    • kblaney
    • Not Ranked
    • Joined on 03-23-2004
    • Posts 3
    • Points 100

    RE: Hashtable.Keys.CopyTo() when hashtable is empty throws ArgumentException

    Note that the same problem occurs with a CodeSmith generated ArrayList. For example, the following code throws an ArgumentException when it should not:

    IntArrayList intArrayList = new IntArrayList();
    int[] intArray = new int[0];
    intArrayList.CopyTo( intArray );

    This problem can be fixed by making the same modifications to the CheckTargetArray method as detailed above.
    • Post Points: 5
  • 03-30-2004 5:22 PM In reply to

    • cnahr
    • Top 50 Contributor
    • Joined on 06-03-2003
    • Posts 105
    • Points 2,275

    RE: Hashtable.Keys.CopyTo() when hashtable is empty throws ArgumentException

    Sorry for the late reply, I'm rather busy these days and won't be able to produce a fix right away.

    Actually I was a bit doubtful at first if a fix was needed at all. CopyTo(array, index) is an ICollection method whose MSDN documentation clearly states that it SHOULD throw an ArgumentException if "index is equal to or greater than the length of array". If array is empty, an index of zero is illegal according to that rule.

    So you might argue that the template is correct and the Microsoft class is wrong!

    However, you have a good point with ArrayList.CopyTo(array). This one shouldn't throw if array is empty as long as the ArrayList is empty, too.

    Seeing that I'll have to fix this anyway I'll reproduce Microsoft's undocumented ICollection.CopyTo behavior as well, in all the templates, as soon as I get around to it.
    • Post Points: 5
  • 05-13-2004 2:11 PM In reply to

    • cnahr
    • Top 50 Contributor
    • Joined on 06-03-2003
    • Posts 105
    • Points 2,275

    RE: Hashtable.Keys.CopyTo() when hashtable is empty throws ArgumentException

    Fixed in the latest version 1.5.1 for all templates. Thanks for reporting!
    • Post Points: 5
Page 1 of 1 (6 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems