Pass an argument to use with a foreach loop
-
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 -
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 MYes, 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 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 -
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'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.
-
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 -
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 MIf 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 aSystem.Collections.IEnumerable
or aSystem.Collections.IEnumerable<T=Cell>
; one way of implementing that is by creating aList<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).