0

I have tried many ways like Cast<CustomObject>, as Customobject and ToArray(Customobject) but nothing worked.

How can I add List or ArrayList via AddRange to a CustomObject[] Array?

Code is really difficult. But if you have some time you can get the complete source of the destination list from here: http://www.codeproject.com/Articles/4012/C-List-View-v1-3?msg=3844172#xx3844172xx

This is a Custom Listview I activated a combobox for the second column, so I can select diferrent values for a cell. But before this, I have to add something to select. This is the hole problem.

Update: Firstly, thanks for the help ! Secondly, Found a solution in the comments from the website with the source. Had to add some code and changed the destination custom array to a List

3
  • 2
    Are the objects in your List<object> instances of CustomObject? Because if they aren't, you can't do it. Commented Sep 13, 2012 at 12:30
  • 2
    Could you at some test code that you've used please? Commented Sep 13, 2012 at 12:31
  • Try breaking down the problem. What's the very bit of code where you add the items to the combo box? Breaking it down to a little piece of code may help you too. Commented Sep 13, 2012 at 14:22

5 Answers 5

6
list.Cast<CustomObject>().ToArray()

Will work as long as the things in the list are actually CustomObject. If they might be other types, you can use OfType<CustomObject>() instead of Cast. This will filter out anything of an incompatible type.

Sign up to request clarification or add additional context in comments.

5 Comments

@Rene You are welcome. If this solved your problem, please click the check mark next to the answer to mark it as accepted.
In 4 minutes... (Wait time from tackoverflow)
Too fast, its not working... Says cannot convert string to GLSubItem, but if I only add one item (.Add("myString")) it works...
@Rene You should really show us some code to see what is wrong.
@Rene Sorry to hear that, but it is difficult to offer any suggestions based on the current question. If you update it to include some example code that reproduces your problem, that will make it easier.
1

Assuming the objects really are instances of CustomObject, use LINQ Select method:

objList.Select(o => o as CustomObject).ToArray();

Otherwise you will get an array of null.

Comments

0

If its a List<CustomObject> then let us say

CustomObject[] coarr = list_of_customobject.ToArray();

If its an ArrayList then

CustomObject[] coarr = arraylist.OfType<CustomObject>().ToArray();   

Comments

0

If you are unsure whether all of your objects are of the type CustomObject try

var result = list.OfType<CustomObject>.ToArray();

Comments

0

Strictly speaking you cannot add elements to an array, since an array's length remains constant over its lifetime. There are two things you can do:

Create a new array

myArray = myTList.ToArray() // generic)
myArray = myArrayList.Cast<CustomObject>().ToArray() // cast, non-generic
myArray = myArrayList.OfType<CustomObject>().ToArray() // filter by type, non-generic

Set elements of an array

myArray[x] = myTList[y] // generic
myArray[x] = (CustomObject)myArrayList[y] // non-generic

I recommend you to take the generic collection whenever possible. They provide you additional type safety. Casting object variables cause runtime errors you could detect at compile time by using generic types.

If you actually want to add elements to an existing collection, you may try to use a dynamic collection type rather than an array: List<T> : IList<T> or LinkedList<T> : ICollection<T> are a good point to start, or maybe more specific types like Stack<T> or Queue<T>.

Comments

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.