Reputation: 77
Perhaps I didn't explaine myself the first time around so here is my second go at it.
I need to declare objects from a list of strings inside an array.
So my array goes out to a DB and collects the names from one colums. This names will all be an object. Now I want to define each object with that name fromt he column dynamicly.
So the array has say 5 elements in it of type string.
So going though my for look i cannot seem t o dynamicly create the object.
So instead of manually going myobject test = new myobject();
I just want to declare it by looping though the array.
Upvotes: 0
Views: 142
Reputation: 2987
You can convert an array to objects by using System.Linq's select operator. You create an object for every i in the array and return a new object for it like this
var array = new string[2]{"one","two"};
var objects = array.Select(i=> new Object{Name = array[i]}).ToArray();
Upvotes: 0
Reputation: 5224
MyObject[] myArray = new MyObject[3];
for (int i =0; i < myArray.Length; i++)
{
MyObject obj = new MyObject();
myArray[i] = obj;
}
Upvotes: 0
Reputation: 5541
I'am not pretty sure what your question is, but if I see your code than you want to create objects in your array?
Maybe this is your solution:
MyObject[] myArray = new MyObject[4];
for (int i =0; i < myArray.Length; ++)
{
myArray[i] = new MyObject();
}
Hope this will help you.
Upvotes: 2