Reputation: 63
How would you get a variable from each object in a list?
I've got this so far:
void SortList()
{
int j = modules.Count;
string[] titles = new string[j];
for (int i = 0; i > modules.Count; i++)
{
titles[i] =
}
}
And I'm trying to get the variable "code" from each object in modules.
thanks.
Upvotes: 1
Views: 5340
Reputation: 13273
Implying modules is a list or an array,
void SortList()
{
int j = modules.Count;
string[] titles = new string[j];
foreach (String title in modules)
{
titles[i] = title.code
}
}
As stated by Cuong Le, you could also use Linq to get a shorter version (Depending of which .Net version you are on).
titles = modules.Select(x => x.code).ToArray();
Upvotes: 4
Reputation: 75306
You can use LINQ with simple code with Select
method:
titles = modules.Select(x => x.code).ToArray();
Upvotes: 1