Get element in List<T>

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?THVpZ2k=?=

    Get element in List<T>

    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.
    --
    Luigi

  • Frans Bouma [C# MVP]

    #2
    Re: Get element in List&lt;T&gt;

    Luigi wrote:
    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<Tclass 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#)
    ------------------------------------------------------------------------

    Comment

    • Anthony Jones

      #3
      Re: Get element in List&lt;T&gt;

      "Luigi" <ciupazNoSpamGr azie@inwind.itw rote in message
      news:E942BEEA-24A4-4AF1-93C6-1E8BC5093EC9@mi crosoft.com...
      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(de legate(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?

      --
      Anthony Jones - MVP ASP/ASP.NET

      Comment

      • =?Utf-8?B?THVpZ2k=?=

        #4
        Re: Get element in List&lt;T&gt;

        "Anthony Jones" wrote:
        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(de legate(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

        Comment

        • =?Utf-8?B?THVpZ2k=?=

          #5
          Re: Get element in List&lt;T&gt;

          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<ColumnTota lComposition;
          .......

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

          private static List<ColumnGetT otalComposition (Column total)
          {
          ....to implement
          }

          Thanks a lot.

          Luigi

          Comment

          • Anthony Jones

            #6
            Re: Get element in List&lt;T&gt;

            "Luigi" <ciupazNoSpamGr azie@inwind.itw rote in message
            news:C46C6CCA-F2BA-411B-9864-EF1B08DC00FD@mi crosoft.com...
            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<ColumnTota lComposition;
            ......
            >
            How can I write a method that returns me the List<Columncomp osition for
            a
            particular Column?
            >
            private static List<ColumnGetT otalComposition (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 TotalCompositio n 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.


            --
            Anthony Jones - MVP ASP/ASP.NET

            Comment

            Working...