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))
4
  • Side-note: Why don't you use a List<String> instead? Commented Jul 30, 2014 at 9:51
  • @Tim Schmelter 'cause is for usage in MY.Settings, does not accept generic collections :(. thanks for your comment Commented Jul 30, 2014 at 9:52
  • Then you could use a StringCollection. stackoverflow.com/a/10419321/284240 Commented Jul 30, 2014 at 9: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. Commented Jul 30, 2014 at 9:55

1 Answer 1

3

I would simply use ArrayList.AddRange:

ArrayList2.AddRange(ArrayList1)
Sign up to request clarification or add additional context in comments.

5 Comments

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: 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.
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.
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.
OK, really thanks for the clarifications, if there is no way to use the CopyTo method then I accept the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.