CopyTo() in ArrayList vs. SortedList

D

David Veeneman

Does a SortedList have a more limited ability to cast that an ArrayList?

I have an array of objects that I store in an ArrayList. I use an ArrayList
because of its flexibility in adding and removing items. I use the array to
populate a list box, so I use the ArrayList CopyTo() method to get a typed
array, which I pass to the list box. That works fine.

Here's my problem: I'd like to do the same thing using a SortedList, so that
I can pull items from the list by ID (key). But when I do, I get an
InvalidCastException from the CopyTo() method, even though an ArrayList can
perform the cast.

Am I missing something here? I'd like to be able to use the SortedList. Is
there a way to get the cast to work?

Below is a complete console app that shows the issue. Thanks for your help.


--
Dave Veeneman
Foresight Systems
Chigago




using System;
using System.Collections;

namespace ConsoleApplication1
{
public class MyClass
{
public int ID;
public string Name;

public MyClass(int theID, string theName)
{
ID = theID;
Name = theName;
}
}

class Class1
{
[STAThread]
static void Main(string[] args)
{
// Declarations
MyClass c;

// Add 5 instances of MyClass to a SortedList
SortedList list = new SortedList();
for (int i = 0; i < 5; i++)
{
c = new MyClass(i, "tempname");
list.Add(c.ID, c);
}

// Copy the sorted list to a MyClass array
MyClass[] typedList = new MyClass[list.Count];
list.CopyTo(typedList, 0);
}

}
}
 
N

Nicholas Paldino [.NET/C# MVP]

David,

The reason you are getting an InvalidCastException is because the CopyTo
method on the SortedList class returns an array of DictionaryEntry
structures, which contain the key and the value. You will have to create
the typed array yourself. The easiest way to do this would be to create a
new ArrayList instance, and then pass the result of the Values property to
the constructor. From there, call CopyTo on the ArrayList to get your typed
array.

Hope this helps.
 
J

Justin Rogers

Any ICollection has to support a CopyTo. SortedList.Values is of type
ICollection.
That means you don't need the intermediate ArrayList. It'll save you some
memory
to copy the values directly.

Just do sortedList.Values.CopyTo(typedArray, 0);

Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

Nicholas Paldino said:
David,

The reason you are getting an InvalidCastException is because the CopyTo
method on the SortedList class returns an array of DictionaryEntry
structures, which contain the key and the value. You will have to create
the typed array yourself. The easiest way to do this would be to create a
new ArrayList instance, and then pass the result of the Values property to
the constructor. From there, call CopyTo on the ArrayList to get your typed
array.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

David Veeneman said:
Does a SortedList have a more limited ability to cast that an ArrayList?

I have an array of objects that I store in an ArrayList. I use an ArrayList
because of its flexibility in adding and removing items. I use the array to
populate a list box, so I use the ArrayList CopyTo() method to get a typed
array, which I pass to the list box. That works fine.

Here's my problem: I'd like to do the same thing using a SortedList, so that
I can pull items from the list by ID (key). But when I do, I get an
InvalidCastException from the CopyTo() method, even though an ArrayList can
perform the cast.

Am I missing something here? I'd like to be able to use the SortedList. Is
there a way to get the cast to work?

Below is a complete console app that shows the issue. Thanks for your help.


--
Dave Veeneman
Foresight Systems
Chigago




using System;
using System.Collections;

namespace ConsoleApplication1
{
public class MyClass
{
public int ID;
public string Name;

public MyClass(int theID, string theName)
{
ID = theID;
Name = theName;
}
}

class Class1
{
[STAThread]
static void Main(string[] args)
{
// Declarations
MyClass c;

// Add 5 instances of MyClass to a SortedList
SortedList list = new SortedList();
for (int i = 0; i < 5; i++)
{
c = new MyClass(i, "tempname");
list.Add(c.ID, c);
}

// Copy the sorted list to a MyClass array
MyClass[] typedList = new MyClass[list.Count];
list.CopyTo(typedList, 0);
}

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top