Click here to Skip to main content
15,890,506 members
Home / Discussions / C#
   

C#

 
GeneralRe: Unknown connection option in connection string: provider. Pin
Tunisien8611-May-10 4:16
Tunisien8611-May-10 4:16 
AnswerRe: Unknown connection option in connection string: provider. Pin
Tunisien8611-May-10 5:45
Tunisien8611-May-10 5:45 
AnswerRe: Unknown connection option in connection string: provider. Pin
William Winner11-May-10 6:05
William Winner11-May-10 6:05 
GeneralRe: Unknown connection option in connection string: provider. Pin
Tunisien8611-May-10 6:11
Tunisien8611-May-10 6:11 
QuestionDLL import problem Pin
SelvaKr11-May-10 0:33
SelvaKr11-May-10 0:33 
AnswerRe: DLL import problem Pin
Luc Pattyn11-May-10 2:11
sitebuilderLuc Pattyn11-May-10 2:11 
GeneralRe: DLL import problem Pin
SelvaKr13-May-10 23:43
SelvaKr13-May-10 23:43 
QuestionPass an argument to use with a foreach loop Pin
Mc_Topaz10-May-10 23:59
Mc_Topaz10-May-10 23:59 
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 Main(string[] args)
   {
      Cells cells = new Cells();

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


Thanks!
AnswerRe: Pass an argument to use with a foreach loop Pin
OriginalGriff11-May-10 0:31
mveOriginalGriff11-May-10 0:31 
GeneralRe: Pass an argument to use with a foreach loop Pin
Mc_Topaz11-May-10 0:55
Mc_Topaz11-May-10 0:55 
AnswerRe: Pass an argument to use with a foreach loop Pin
Abhinav S11-May-10 0:47
Abhinav S11-May-10 0:47 
AnswerRe: Pass an argument to use with a foreach loop Pin
sam#11-May-10 0:57
sam#11-May-10 0:57 
AnswerRe: Pass an argument to use with a foreach loop Pin
Luc Pattyn11-May-10 2:20
sitebuilderLuc Pattyn11-May-10 2:20 
Questiongetting ini value Pin
Member 59031010-May-10 22:27
Member 59031010-May-10 22:27 
AnswerRe: getting ini value Pin
OriginalGriff10-May-10 22:49
mveOriginalGriff10-May-10 22:49 
AnswerRe: getting ini value Pin
PIEBALDconsult11-May-10 3:38
mvePIEBALDconsult11-May-10 3:38 
QuestionSending data through SOAP Pin
Priya Prk10-May-10 21:49
Priya Prk10-May-10 21:49 
AnswerRe: Sending data through SOAP Pin
Abhinav S10-May-10 22:33
Abhinav S10-May-10 22:33 
QuestionPrint Visitor Card Pin
Hakmeh Mohannad10-May-10 21:49
Hakmeh Mohannad10-May-10 21:49 
Questionvss and visual studio Pin
Zeyad Jalil10-May-10 21:23
professionalZeyad Jalil10-May-10 21:23 
AnswerRe: vss and visual studio Pin
R. Giskard Reventlov10-May-10 21:27
R. Giskard Reventlov10-May-10 21:27 
AnswerRe: vss and visual studio Pin
Abhinav S10-May-10 21:28
Abhinav S10-May-10 21:28 
GeneralRe: vss and visual studio Pin
Zeyad Jalil10-May-10 22:27
professionalZeyad Jalil10-May-10 22:27 
AnswerRe: vss and visual studio Pin
Michel Godfroid10-May-10 22:49
Michel Godfroid10-May-10 22:49 
GeneralRe: vss and visual studio Pin
Zeyad Jalil10-May-10 23:00
professionalZeyad Jalil10-May-10 23:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.