Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Pass an argument to use with a foreach loop

Pass an argument to use with a foreach loop

Scheduled Pinned Locked Moved C#
data-structuresquestion
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mc_Topaz
    wrote on last edited by
    #1

    I want to pass an argument to a foreach loop. This to specify the foreach loop to loop through a collection in a specific way. See this code:

    foreach (DataType instance in collection(argument))
    {
    // Do something
    }

    This is my test program:

    class Program
    {
    static void Main(string[] args)
    {
    Cells cells = new Cells();

      foreach (Cell cell in cells)
      {
         Console.WriteLine(cell.Value);
      }
    }
    

    }

    public class Cell
    {
    private string column;
    private int row;
    private string value;

    public Cell(string column, int row, string value)
    {
    this.column = column;
    this.row = row;
    this.value = value;
    }

    public string Value
    {
    get { return value; }
    set { this.value = value; }
    }
    }

    public class Cells : IEnumerator, IEnumerable
    {
    private Cell[] cellList;
    int position = -1;

    public Cells()
    {
    cellList = new Cell[10]
    {
    new Cell("A", 1, "A:1"),
    new Cell("A", 2, "A:2"),
    new Cell("A", 3, "A:3"),
    new Cell("A", 4, "A:4"),
    new Cell("A", 5, "A:5"),

         new Cell("B", 1, "B:1"),
         new Cell("B", 2, "B:2"),
         new Cell("B", 3, "B:3"),
         new Cell("B", 4, "B:4"),
         new Cell("B", 5, "B:5"),
      };
    

    }

    public IEnumerator GetEnumerator()
    {
    return (IEnumerator)this;
    }

    public bool MoveNext()
    {
    position++;
    return (position < cellList.Length);
    }

    public void Reset()
    {
    position = 0;
    }

    public object Current
    {
    get { return cellList[position]; }
    }
    }

    Notice in the cellList array in the Cells class which is looped through in the Main()-method. I want to loop through all Cell instances in the list which has the column member set to "B". I would like to prefer to write it like this in the Main()-method:

    class Program
    {
    static void Main(string[] args)
    {
    Cells cells = new Cells();

      foreach (Cell cell in cells("B"))
      {
         Console.WriteLine(cell.Value);
      }
    }
    

    }

    or

    class Program
    {
    static void Main(string[] args)
    {
    Cells cells = new Cells();

      foreach (Cell cell in cells.Column("B"))
      {
         Console.WriteLine(cell.Value);
      }
    }
    

    }

    Is it possible to modify my test code to this? I'm not looking for this answer:

    class Program
    {
    static void M

    OriginalGriffO A S L 4 Replies Last reply
    0
    • M Mc_Topaz

      I want to pass an argument to a foreach loop. This to specify the foreach loop to loop through a collection in a specific way. See this code:

      foreach (DataType instance in collection(argument))
      {
      // Do something
      }

      This is my test program:

      class Program
      {
      static void Main(string[] args)
      {
      Cells cells = new Cells();

        foreach (Cell cell in cells)
        {
           Console.WriteLine(cell.Value);
        }
      }
      

      }

      public class Cell
      {
      private string column;
      private int row;
      private string value;

      public Cell(string column, int row, string value)
      {
      this.column = column;
      this.row = row;
      this.value = value;
      }

      public string Value
      {
      get { return value; }
      set { this.value = value; }
      }
      }

      public class Cells : IEnumerator, IEnumerable
      {
      private Cell[] cellList;
      int position = -1;

      public Cells()
      {
      cellList = new Cell[10]
      {
      new Cell("A", 1, "A:1"),
      new Cell("A", 2, "A:2"),
      new Cell("A", 3, "A:3"),
      new Cell("A", 4, "A:4"),
      new Cell("A", 5, "A:5"),

           new Cell("B", 1, "B:1"),
           new Cell("B", 2, "B:2"),
           new Cell("B", 3, "B:3"),
           new Cell("B", 4, "B:4"),
           new Cell("B", 5, "B:5"),
        };
      

      }

      public IEnumerator GetEnumerator()
      {
      return (IEnumerator)this;
      }

      public bool MoveNext()
      {
      position++;
      return (position < cellList.Length);
      }

      public void Reset()
      {
      position = 0;
      }

      public object Current
      {
      get { return cellList[position]; }
      }
      }

      Notice in the cellList array in the Cells class which is looped through in the Main()-method. I want to loop through all Cell instances in the list which has the column member set to "B". I would like to prefer to write it like this in the Main()-method:

      class Program
      {
      static void Main(string[] args)
      {
      Cells cells = new Cells();

        foreach (Cell cell in cells("B"))
        {
           Console.WriteLine(cell.Value);
        }
      }
      

      }

      or

      class Program
      {
      static void Main(string[] args)
      {
      Cells cells = new Cells();

        foreach (Cell cell in cells.Column("B"))
        {
           Console.WriteLine(cell.Value);
        }
      }
      

      }

      Is it possible to modify my test code to this? I'm not looking for this answer:

      class Program
      {
      static void M

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Yes, but not quite the way you have it. You would need to write a "Find(string s)" method (or similar) for your Cells class, which returns a list of each matching cell. This might defeat the purpose a bit, as it would move the "cell.Value.Contains(s)" into the method, rather than the foreach loop.

      I have learnt that you can not make someone love you, all you can do is stalk them and hope they panic and give in. Apathy Error: Don't bother striking any key.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      M 1 Reply Last reply
      0
      • M Mc_Topaz

        I want to pass an argument to a foreach loop. This to specify the foreach loop to loop through a collection in a specific way. See this code:

        foreach (DataType instance in collection(argument))
        {
        // Do something
        }

        This is my test program:

        class Program
        {
        static void Main(string[] args)
        {
        Cells cells = new Cells();

          foreach (Cell cell in cells)
          {
             Console.WriteLine(cell.Value);
          }
        }
        

        }

        public class Cell
        {
        private string column;
        private int row;
        private string value;

        public Cell(string column, int row, string value)
        {
        this.column = column;
        this.row = row;
        this.value = value;
        }

        public string Value
        {
        get { return value; }
        set { this.value = value; }
        }
        }

        public class Cells : IEnumerator, IEnumerable
        {
        private Cell[] cellList;
        int position = -1;

        public Cells()
        {
        cellList = new Cell[10]
        {
        new Cell("A", 1, "A:1"),
        new Cell("A", 2, "A:2"),
        new Cell("A", 3, "A:3"),
        new Cell("A", 4, "A:4"),
        new Cell("A", 5, "A:5"),

             new Cell("B", 1, "B:1"),
             new Cell("B", 2, "B:2"),
             new Cell("B", 3, "B:3"),
             new Cell("B", 4, "B:4"),
             new Cell("B", 5, "B:5"),
          };
        

        }

        public IEnumerator GetEnumerator()
        {
        return (IEnumerator)this;
        }

        public bool MoveNext()
        {
        position++;
        return (position < cellList.Length);
        }

        public void Reset()
        {
        position = 0;
        }

        public object Current
        {
        get { return cellList[position]; }
        }
        }

        Notice in the cellList array in the Cells class which is looped through in the Main()-method. I want to loop through all Cell instances in the list which has the column member set to "B". I would like to prefer to write it like this in the Main()-method:

        class Program
        {
        static void Main(string[] args)
        {
        Cells cells = new Cells();

          foreach (Cell cell in cells("B"))
          {
             Console.WriteLine(cell.Value);
          }
        }
        

        }

        or

        class Program
        {
        static void Main(string[] args)
        {
        Cells cells = new Cells();

          foreach (Cell cell in cells.Column("B"))
          {
             Console.WriteLine(cell.Value);
          }
        }
        

        }

        Is it possible to modify my test code to this? I'm not looking for this answer:

        class Program
        {
        static void M

        A Offline
        A Offline
        Abhinav S
        wrote on last edited by
        #3

        Have a look at the AsQueryable() extension method. See here.

        1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          Yes, but not quite the way you have it. You would need to write a "Find(string s)" method (or similar) for your Cells class, which returns a list of each matching cell. This might defeat the purpose a bit, as it would move the "cell.Value.Contains(s)" into the method, rather than the foreach loop.

          I have learnt that you can not make someone love you, all you can do is stalk them and hope they panic and give in. Apathy Error: Don't bother striking any key.

          M Offline
          M Offline
          Mc_Topaz
          wrote on last edited by
          #4

          I'm fine if I need to write:

          cell.Value.Contains(s)

          or something similar in the Cells class. I know I need to do some changes in the Cells class. I'm just wonder if I, in the Main()-method, can pass an argument in the foreach-loop condition, just like I stated it.

          1 Reply Last reply
          0
          • M Mc_Topaz

            I want to pass an argument to a foreach loop. This to specify the foreach loop to loop through a collection in a specific way. See this code:

            foreach (DataType instance in collection(argument))
            {
            // Do something
            }

            This is my test program:

            class Program
            {
            static void Main(string[] args)
            {
            Cells cells = new Cells();

              foreach (Cell cell in cells)
              {
                 Console.WriteLine(cell.Value);
              }
            }
            

            }

            public class Cell
            {
            private string column;
            private int row;
            private string value;

            public Cell(string column, int row, string value)
            {
            this.column = column;
            this.row = row;
            this.value = value;
            }

            public string Value
            {
            get { return value; }
            set { this.value = value; }
            }
            }

            public class Cells : IEnumerator, IEnumerable
            {
            private Cell[] cellList;
            int position = -1;

            public Cells()
            {
            cellList = new Cell[10]
            {
            new Cell("A", 1, "A:1"),
            new Cell("A", 2, "A:2"),
            new Cell("A", 3, "A:3"),
            new Cell("A", 4, "A:4"),
            new Cell("A", 5, "A:5"),

                 new Cell("B", 1, "B:1"),
                 new Cell("B", 2, "B:2"),
                 new Cell("B", 3, "B:3"),
                 new Cell("B", 4, "B:4"),
                 new Cell("B", 5, "B:5"),
              };
            

            }

            public IEnumerator GetEnumerator()
            {
            return (IEnumerator)this;
            }

            public bool MoveNext()
            {
            position++;
            return (position < cellList.Length);
            }

            public void Reset()
            {
            position = 0;
            }

            public object Current
            {
            get { return cellList[position]; }
            }
            }

            Notice in the cellList array in the Cells class which is looped through in the Main()-method. I want to loop through all Cell instances in the list which has the column member set to "B". I would like to prefer to write it like this in the Main()-method:

            class Program
            {
            static void Main(string[] args)
            {
            Cells cells = new Cells();

              foreach (Cell cell in cells("B"))
              {
                 Console.WriteLine(cell.Value);
              }
            }
            

            }

            or

            class Program
            {
            static void Main(string[] args)
            {
            Cells cells = new Cells();

              foreach (Cell cell in cells.Column("B"))
              {
                 Console.WriteLine(cell.Value);
              }
            }
            

            }

            Is it possible to modify my test code to this? I'm not looking for this answer:

            class Program
            {
            static void M

            S Offline
            S Offline
            sam
            wrote on last edited by
            #5

            Use LINQ !

            1 Reply Last reply
            0
            • M Mc_Topaz

              I want to pass an argument to a foreach loop. This to specify the foreach loop to loop through a collection in a specific way. See this code:

              foreach (DataType instance in collection(argument))
              {
              // Do something
              }

              This is my test program:

              class Program
              {
              static void Main(string[] args)
              {
              Cells cells = new Cells();

                foreach (Cell cell in cells)
                {
                   Console.WriteLine(cell.Value);
                }
              }
              

              }

              public class Cell
              {
              private string column;
              private int row;
              private string value;

              public Cell(string column, int row, string value)
              {
              this.column = column;
              this.row = row;
              this.value = value;
              }

              public string Value
              {
              get { return value; }
              set { this.value = value; }
              }
              }

              public class Cells : IEnumerator, IEnumerable
              {
              private Cell[] cellList;
              int position = -1;

              public Cells()
              {
              cellList = new Cell[10]
              {
              new Cell("A", 1, "A:1"),
              new Cell("A", 2, "A:2"),
              new Cell("A", 3, "A:3"),
              new Cell("A", 4, "A:4"),
              new Cell("A", 5, "A:5"),

                   new Cell("B", 1, "B:1"),
                   new Cell("B", 2, "B:2"),
                   new Cell("B", 3, "B:3"),
                   new Cell("B", 4, "B:4"),
                   new Cell("B", 5, "B:5"),
                };
              

              }

              public IEnumerator GetEnumerator()
              {
              return (IEnumerator)this;
              }

              public bool MoveNext()
              {
              position++;
              return (position < cellList.Length);
              }

              public void Reset()
              {
              position = 0;
              }

              public object Current
              {
              get { return cellList[position]; }
              }
              }

              Notice in the cellList array in the Cells class which is looped through in the Main()-method. I want to loop through all Cell instances in the list which has the column member set to "B". I would like to prefer to write it like this in the Main()-method:

              class Program
              {
              static void Main(string[] args)
              {
              Cells cells = new Cells();

                foreach (Cell cell in cells("B"))
                {
                   Console.WriteLine(cell.Value);
                }
              }
              

              }

              or

              class Program
              {
              static void Main(string[] args)
              {
              Cells cells = new Cells();

                foreach (Cell cell in cells.Column("B"))
                {
                   Console.WriteLine(cell.Value);
                }
              }
              

              }

              Is it possible to modify my test code to this? I'm not looking for this answer:

              class Program
              {
              static void M

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              If you want to be able to do

              foreach (Cell cell in cells.HavingColumn("B")) {
              Console.WriteLine(cell.Value);
              }

              all you have to do is add a method HavingColumn(string columnName) to the Cell class, which returns a System.Collections.IEnumerable or a System.Collections.IEnumerable<T=Cell>; one way of implementing that is by creating a List<Cell>. FWIW1: this operation will be more expensive than simply adding a test inside your foreach; which is how the method will be implemented anyway (but then it is hidden from the external world). FWIW2: and that must be how LINQ does it; the easiest and most expensive approach. :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read formatted code with indentation, so please use PRE tags for code snippets.


              I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups