Get element in List<T>

L

Luigi

Hi all,
I have a List of object Column, and the class Column has 2 properties:
a name (string) and a enum (type, normal or total).

public class Column
{
private string columnName;
public string ColumnName
{
get { return columnName; }
set { columnName = value; }
}

public ColumnType columnType;
public enum ColumnType : short { Normal = 0, Total = 1 };

}

How can I obtain the element of the List having a "columnName" (so I can get
the type of this columnName)?

Thank a lot.
 
F

Frans Bouma [C# MVP]

Luigi said:
Hi all,
I have a List of object Column, and the class Column has 2 properties:
a name (string) and a enum (type, normal or total).

public class Column
{
private string columnName;
public string ColumnName
{
get { return columnName; }
set { columnName = value; }
}

public ColumnType columnType;
public enum ColumnType : short { Normal = 0, Total = 1 };

}

How can I obtain the element of the List having a "columnName" (so I can get
the type of this columnName)?

The List<T> class isn't indexed, so you have to do a linear search. You
can use .FindAll() or if you're using .NET 3.5, linq to objects using
..Where(filter), but both are doing a linear search, so you can also
simply use a foreach statement over the List<T>.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
A

Anthony Jones

Luigi said:
Hi all,
I have a List of object Column, and the class Column has 2 properties:
a name (string) and a enum (type, normal or total).

public class Column
{
private string columnName;
public string ColumnName
{
get { return columnName; }
set { columnName = value; }
}

public ColumnType columnType;
public enum ColumnType : short { Normal = 0, Total = 1 };

}

How can I obtain the element of the List having a "columnName" (so I can
get
the type of this columnName)?

For the structure of your code I'll assume C# 2.

You need the Find method of the List class and pass it a predicate
function:-

Column col = Columns.Find(delegate(Column c) { return c.ColumnName ==
"columnName"; });

In C# 3 it collapses to

Column col = Columns.Find(c => c.ColumnName == "columnName");

BTW, What other names are attached to a Column objects? IOW why not call
ColumnName simply Name?
 
L

Luigi

Anthony Jones said:
For the structure of your code I'll assume C# 2.

You need the Find method of the List class and pass it a predicate
function:-

Column col = Columns.Find(delegate(Column c) { return c.ColumnName ==
"columnName"; });

In C# 3 it collapses to

Column col = Columns.Find(c => c.ColumnName == "columnName");

BTW, What other names are attached to a Column objects? IOW why not call
ColumnName simply Name?

Perferct, thank you Anthony and Frans.

Luigi
 
L

Luigi

Hi Anthony and Frans, a little variant.
My Column class has another property, a List<Column> (for example if a
column is a total column, and this list is its composition.

......
public List<Column> TotalComposition;
.......

How can I write a method that returns me the List<Column> composition for a
particular Column?

private static List<Column> GetTotalComposition(Column total)
{
....to implement
}

Thanks a lot.

Luigi
 
A

Anthony Jones

Luigi said:
Hi Anthony and Frans, a little variant.
My Column class has another property, a List<Column> (for example if a
column is a total column, and this list is its composition.

.....
public List<Column> TotalComposition;
......

How can I write a method that returns me the List<Column> composition for
a
particular Column?

private static List<Column> GetTotalComposition(Column total)
{
...to implement
}

I'm afraid you've lost me. You're going to need to put some more detail and
code into this one.

What for example do you mean by 'composition'? So far if a Column has a
property called TotalComposition what is stopping you from just accessing
it? That probably makes no sense to you but that it'll be because I haven't
been able to make sense of the above.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top