2

I would like to know in C# or VB.NET how to use the CopyTo method of an ArrayList object to copy its contents to other ArrayList (not a simple Array).

Note: I'm not looking for anything else like the Clone method of the ArrayList or other sugestions, I need to use the CopyTo method to know whether I should discard a problem in other situation.

This is a code example in VB.NET:

Dim ArrayList1 as New ArrayList
Dim ArrayList2 as New ArrayList
ArrayList1.Add({"test-Item1-1", "test-Item1-2", "Test-Item1-3"})


' This says that the matrix of the destiny Array is too short.
ArrayList1.CopyTo(ArrayList2.ToArray)

' This shows the typical CastIterator exception 'cause LINQ.
ArrayList1.CopyTo(ArrayList2.Cast(Of Array))

' This says that the ArrayList can't be converted to an Array.
ArrayList1.CopyTo(CType(ArrayList2, Array))
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • Side-note: Why don't you use a `List` instead? – Tim Schmelter Jul 30 '14 at 09:51
  • @Tim Schmelter 'cause is for usage in MY.Settings, does not accept generic collections :(. thanks for your comment – ElektroStudios Jul 30 '14 at 09:52
  • Then you could use a [`StringCollection`](http://msdn.microsoft.com/en-us/library/system.collections.specialized.stringcollection(v=vs.100).ASPX). http://stackoverflow.com/a/10419321/284240 – Tim Schmelter Jul 30 '14 at 09:53
  • @Tim Schmelter yes but a stringcollection expects a String not a String(), the ArrayList should be easier to use instead using a dilimiter to split a normal String, I'm really not interested in alternatives, I know the alternatives, I just would like to know the proper usage of "CopyTo" method for discard a problem, thanks for your comment. – ElektroStudios Jul 30 '14 at 09:55

1 Answers1

3

I would simply use ArrayList.AddRange:

ArrayList2.AddRange(ArrayList1)
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Really I appreciate your help but sorry I'm only searching in the way to add the items to other ArrayLists using the CopyTo method, as I've explained this is to discard a problem in other situation (and also to learn the proper usage). thanks for your answer +1 – ElektroStudios Jul 30 '14 at 10:03
  • 2
    @ElektroStudios: i don't understand why you need to use `CopyTo`. An `ArrayList` is _not_ an array, so you cannot use this method. You would have to create another array from the list to be able to use `CopyTo`. This array needs to have the correct size(so at least old ArrayList + new content). Afterwards you need to create a new ArrayList with the constructor that takes a collection with this array. This is very inefficient and not readable. – Tim Schmelter Jul 30 '14 at 10:07
  • The real problem is that I've an ArrayList in My.Settings but any method saves the changes in the (initialized) arraylist on the next application run, but using the "Collections.ArrayList.Repeat" method to set the ArrayList (in my.settings) the changes remains on the next run, but that method does not work like expected ('cause is for adding only 1 value) and I hope that the CopyTo method finally will solve this problem but for this first I need to know how to use the CopyTo method to discard the problem. – ElektroStudios Jul 30 '14 at 10:07
  • The is NO WAY to use `CopyTo` for this matter, if you don't want to use reflection. `AddRange` is the only thing you've got here. – Alireza Jul 30 '14 at 10:09
  • OK, really thanks for the clarifications, if there is no way to use the CopyTo method then I accept the answer – ElektroStudios Jul 30 '14 at 10:10