Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
C#
List<object> items= new List<object>();
List.Add(...);

..........
C#
var s = items[0]; // not working


From OP:
cannot convert source type 'object' to target type object[]
Posted
Updated 31-Jul-12 12:21pm
v3
Comments
lewax00 31-Jul-12 17:50pm    
How is it "not working" exactly? Is it throwing an exception? If so, which one? We need more details to give you any help.
[no name] 31-Jul-12 17:52pm    
Can not apply indexing by [] to an expression of type "object"
lewax00 31-Jul-12 17:57pm    
So I'm guessing that second part is in a different function then?
Sergey Alexandrovich Kryukov 31-Jul-12 18:00pm    
To "object" -- you could not, but you apply to a list of objects, so it will work.
--SA
Kenneth Haugland 31-Jul-12 18:15pm    
I deleted your answer, If you have additional commets press Add comment ot improve question instead of answering your own question. :)

C#
List<object> items= new List<object>();
items.Add(1);
items.Add(2);
items.Add(3);

var s = items[0];


The above works for me. Check your syntax.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 31-Jul-12 18:02pm    
Of course it should work. OP just presents a dirty question. Well, a 5.
--SA
Kenneth Haugland 31-Jul-12 18:06pm    
You guys missed somthing... :)
Sergey Alexandrovich Kryukov 31-Jul-12 18:54pm    
Excuse me, who is missing what?
--SA
Andrew Rissing 31-Jul-12 19:57pm    
I caught the problem, hence the comment about syntax. ;-)
Of course it's working, in all senses of this word. Just try your own code, as you post it and nothing else. But why using the type object with generic collection? It makes sense to use some more concrete type.

—SA
 
Share this answer
 
Think your mistake is confusing List with item.... You adds the new variables to the list and not the item... SO yes they are right in that your syntax is wrong...

You should just take a note that the new list you created is named "item" an not "List"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900