How could I reverse the way a foreach travels through the object?

Asked

Viewed 164 times

0

Code to follow:

foreach(Control objCtrl in groupBox.Controls)
{
    if (objCtrl is NumericTextBox)
    {
        int i = objCtrl.Text.Length;
        MessageBox.Show(i.ToString());
        if (String.IsNullOrEmpty(objCtrl.Text) || objCtrl.Text == "0,00")
        {
            this.exibeNotificacaoCampoVazio(objCtrl, "Atenção", "O esse campo não pode ser vazio");
            return false;
        }
    }

}
  • 1

    Besides not being appropriate, if you know the fixed size of the collection, it would make more sense to use a be simple with decreasing

  • 3

    And yet... why do you want to do this? If the idea is to go through all the items, what’s the difference in order? Explain your scenario better as it seems you are focusing on a solution that is not the treatment of your problem.

  • He’s going through some Textbox there displays a message on top of it. Explaining in a simplified way just want to know how to reverse the way he is displaying the message, it worked!

1 answer

5

With the extension method IEnumerable<T>.Reverse

Example:

// using System.Collections.Generic;
// using System.Linq;
IEnumerable<int> list = new[] { 1, 2, 3 };

foreach (var item in list.Reverse())
{
    Console.Write(item + " ");
} // saída: 3 2 1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.