none
How does one EXIT a foreach loop

    Question

  • If I am looping through a collection and find what I am looking for, how do I exit this loop?
    Tuesday, April 04, 2006 11:56 AM

Answers

  • hi


    string look_for = "bbb";

    ArrayList names = new ArrayList();

    names.Add("aaa");

    names.Add("bbb");

    names.Add("ccc");

    foreach (string name in names)

    {

    if (look_for == name)

    {

    break;

    }

    }


    Hope this helps

    Tuesday, April 04, 2006 12:40 PM

All replies

  • with

    break;

    /Erwin

    Tuesday, April 04, 2006 12:12 PM
  • hi


    string look_for = "bbb";

    ArrayList names = new ArrayList();

    names.Add("aaa");

    names.Add("bbb");

    names.Add("ccc");

    foreach (string name in names)

    {

    if (look_for == name)

    {

    break;

    }

    }


    Hope this helps

    Tuesday, April 04, 2006 12:40 PM
  • Hi..

     

    try

    {

    if(flgstop == false)

    {

    //strfindpath = txtSearchFilesorFolders.Text;

    foreach (string d in Directory.GetDirectories(sDir))

    {

    foreach (string f in Directory.GetFiles(d, strfindpath)) // txtSearchFilesorFolders.Text )) // + "*.xml")) // Condition to search file in only XML File

    {

    Listbox1.Items.Add(f)

    }

    DirSearchFile(d);

    Application.DoEvents();

    if(flgstop == true)

    {

    lblMessage.Text = "Stop Searching..";

    break;

    }

    }

    Thursday, August 10, 2006 12:13 PM