4

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);                
        }
3
  • What version of .NET are you using? Commented Jun 1, 2010 at 14:13
  • Can a single textbox be in two of the lists? Commented Jun 1, 2010 at 14:17
  • Yes, in short, each array is a textbox[] of cube, hort, and vert Textboxes of a sudoku prog. So for structural purposes, I'd like to have each array independent of others. Commented Jun 1, 2010 at 14:21

7 Answers 7

13

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I changed my conflicts var from TextBox[] to List<TextBox>. works great! Thanks to all the responses!
You can also write conflicts = (...).ToArray().
because there is once instance of the same textbox in each array, I simply add the where clause (... && tb.Name != box.Name) –
@Mike: ` && tb != box. You don't need to compare the Name` strings.
3

Another .net 3.5 approach:-

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

Comments

1

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);

Comments

1

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);

Comments

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

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

Comments

0

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))

3 Comments

Union will remove duplicates
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?
@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.
-1

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.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.