Mike
Mike

Reputation: 1492

foreach loop from multiple arrays c#

This should be a simple question. All I want to know is if there is a better way of coding this. I want to do a foreach loop for every array, without having to redeclare the foreach loop. Is there a way c# projects this? I was thinking of putting this in a Collection...?

Please, critique my code.

        foreach (TextBox tb in vert)
        {
            if (tb.Text == box.Text)                
                conflicts.Add(tb);                
        }
        foreach (TextBox tb in hort)
        {
            if (tb.Text == box.Text)                
                conflicts.Add(tb);                
        }
        foreach (TextBox tb in cube)
        {
            if (tb.Text == box.Text)
                conflicts.Add(tb);                
        }

Upvotes: 4

Views: 7550

Answers (7)

Daniel Bern
Daniel Bern

Reputation: 137

If you try to create Sudoku game(mentioned in comments) first read about Permutation group and Combinatorics. This will help you to choose more efficient Application Model w/o using foreach on text boxes. Using lazy computation resolve the problem with object reduction but not improve your logics man.

Upvotes: -1

JeppeSN
JeppeSN

Reputation: 21

There are of course many ways to write this, but you could also do

  foreach (var direction in new[] { vert, hort, cube })
    foreach (TextBox tb in direction)
      if (tb.Text == box.Text)
        conflicts.Add(tb);

Upvotes: 1

48klocs
48klocs

Reputation: 6103

You should be able to use Enumerable.Concat to glue them together if you're using .Net 3.5 or higher.

foreach (TextBox tb in vert.Concat(hort).Concat(cube))

Upvotes: 0

Randolpho
Randolpho

Reputation: 56381

If you can't use LINQ for whatever reason (and I highly suggest you do) you could make your array searching a single method. For example:

public void FindConflicts(IEnumerable<TextBox> tbList, IList<TextBox> conflicts, string test)
{
   foreach(TextBox tb in tbList)
   {
      if(tb.Text == test)
      {
          conflicts.Add(tb);
      }
   }
}

And then call it like so:

FindConflicts(vert, conflicts, box.Text);
FindConflicts(hort, conflicts, box.Text);
FindConflicts(cube, conflicts, box.Text);

Upvotes: 1

ljs
ljs

Reputation: 37807

Another .net 3.5 approach:-

conflicts.AddRange(from textBox in vert.Concat(hort).Concat(cube)
                   where textBox.Text == box.Text
                   select textBox);

Upvotes: 3

Justin Niessner
Justin Niessner

Reputation: 245389

var unionResult = vert.Concat(hort).Concat(cube)

foreach(TextBox tb in unionResult)
    if(tb.Text == box.Text)
        conflicts.Add(tb);

Upvotes: 0

SLaks
SLaks

Reputation: 887195

You can use LINQ:

conflicts.AddRange(
    vert.Concat(hort).Concat(cube)
        .Where(tb => tb.Text == box.Text)
); 

I'm assuming that conflicts is a List<TextBox>, which has an AddRange method. If it isn't, you'll need to call Add in a (single) loop.
If you're creating conflicts, (or if it starts empty), you can call .ToList() instead.

Upvotes: 13

Related Questions