tags:

views:

337

answers:

7

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);                
        }
+11  A: 

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.

SLaks
I changed my conflicts var from TextBox[] to List<TextBox>. works great! Thanks to all the responses!
Mike
You can also write `conflicts = (...).ToArray()`.
SLaks
Mike
SLaks
thanks SLaks :D
Mike
A: 
var unionResult = vert.Concat(hort).Concat(cube)

foreach(TextBox tb in unionResult)
    if(tb.Text == box.Text)
        conflicts.Add(tb);
Justin Niessner
A: 

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))
48klocs
`Union` will remove duplicates
SLaks
I do not want to remove dups, as each array is a collection of 9 textboxes. If Union eliminates dups, is there a natural join function?
Mike
@Slaks - thanks. I knew that (and included a link to the documentation on Enumerable.Concat), but my fingers didn't cooperate with my brain when I typed out the code example. I edited it to use the Concat method instead of Union.
48klocs
+2  A: 

Another .net 3.5 approach:-

conflicts.AddRange(from textBox in vert.Concat(hort).Concat(cube)
                   where textBox.Text == box.Text
                   select textBox);
kronoz
+1  A: 

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);
Randolpho
A: 

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);
JeppeSN
A: 

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.

Daniel Bern
I appreciate the reference, but this is entirely for learning purposes.
Mike