locked
How to Exit the loop in C# before the end

    Question

  •  

    If I want to exit in VB.Net I can use (Exit Sub, Exit For, Exit While ...), but how can I do the same in C#??

     

    There is no Exit, and "breake" doesn't give a control what to breake like VB.NET.

     

    for exampl:

     

    I want to exit this code before i = 10

     

     

    Code Block

    for (int i = 0; i < 10; i++)

    {

    .. // do somting

    // somtime I need to exit when i = 5

    // for example to start a new page while printing.

    }

     

     

    Friday, December 07, 2007 8:42 AM

Answers

  • Code Block

    if (i == 5)

    break;

     

     

    --

    Regards,

    Daniel Kuppitz

    Friday, December 07, 2007 8:44 AM
  • Replace break; with return;. Then the method is finished.

     

    --

    Regards,

    Daniel Kuppitz

     

    Friday, December 07, 2007 3:32 PM
  •  Hani Felemban wrote:

    Thanks for replay but the case is various

     
     

     

    class PrintTabuler
    {
    PrintTabuler(string[,] data)
    {

    static int startItem ;

     

    for (int i = startItem; i < data.GetLength(0); i++)

    {

    for (int ii = 0; ii < data.GetLength(1); ii++)

    {

    e.Graphics.DrawRectangle(Pens.Black, new Rectangle(X, Y, colWidth[ii], 20));

    e.Graphics.DrawString(data[i,ii], txtFont, txtBrush, new RectangleF(X, Y, colWidth[ii], 20));

    X += colWidth[ii];

    }

    X = space + marginRight;

    Y += 20;

     

    if (Y > 0.95 * pageHieght)

    {

    e.HasMorePages = true;

    startItem = i + 1;

    break;

    }

    }

    // these two lines should run only in the last page.

    // but the "break" does not effect these

    if (startItem == data.GetLength (0))

    {

    e.HasMorePages = false;

    startItem = 0;

    }

}
}
 
Is this what you want?
Monday, December 10, 2007 2:25 AM

All replies

  • Code Block

    if (i == 5)

    break;

     

     

    --

    Regards,

    Daniel Kuppitz

    Friday, December 07, 2007 8:44 AM
  • Thanks for replay but the case is various

     
    let me explain:
     
    Code Block
    class dataSource
    {
    // let's say this send a print order to class printTabuer passing to it the data in Array

    private void buttonPrint_Click(object sender, EventArgs e)

    {

    string[,] data = new string[500,4] { ...// there is 500 elements}

    PrintTabuler printData = new PrintTabuler(date)

    }

    }

     

     

     

     
    Code Block
    class PrintTabuler
    {
    PrintTabuler(string[,] data)
    {

    static int startItem ;

     

    for (int i = startItem; i < data.GetLength(0); i++)

    {

    for (int ii = 0; ii < data.GetLength(1); ii++)

    {

    e.Graphics.DrawRectangle(Pens.Black, new Rectangle(X, Y, colWidth[ii], 20));

    e.Graphics.DrawString(data[i,ii], txtFont, txtBrush, new RectangleF(X, Y, colWidth[ii], 20));

    X += colWidth[ii];

    }

    X = space + marginRight;

    Y += 20;

     

    if (Y > 0.95 * pageHieght)

    {

    e.HasMorePages = true;

    startItem = i + 1;

    break;

    }

    }

    // these two lines should run only in the last page.

    // but the "break" does not effect these

    e.HasMorePages = false;

    startItem = 0;

    }
    }

     

     

    Friday, December 07, 2007 2:44 PM
  • Replace break; with return;. Then the method is finished.

     

    --

    Regards,

    Daniel Kuppitz

     

    Friday, December 07, 2007 3:32 PM
  • If you want to break out of multiple loops in one command without "return"ing from the method, your only option in C# is "goto".  Goto is generally considered harmful, but your other options are more complicated.

    In some cases, you can achieve the same result by reoriganizing the method.  Another option is to move the loops into their own method so you can use "return".  You can also use a Boolean true/false to determine wither to loop again or not.

    -Ryan / Kardax
    Friday, December 07, 2007 5:31 PM
  • Mr. Daniel Kuppitz

     

    It is work. Thak you.

     

    Thanks for every one answered me. I like .NET becouse there are people like you.

     

    Thanks again for you all.

     

    Best regard

    Friday, December 07, 2007 7:07 PM
  •  Ryan Lamansky / Kardax wrote:
    If you want to break out of multiple loops in one command without "return"ing from the method, your only option in C# is "goto".  Goto is generally considered harmful, but your other options are more complicated.

    In some cases, you can achieve the same result by reoriganizing the method.  Another option is to move the loops into their own method so you can use "return".  You can also use a Boolean true/false to determine wither to loop again or not.

    -Ryan / Kardax

     

    Doesn't that create extra garbage that would need to be collected later?  Yes, it gets collected sooner or later.  But now the garbage collection has more work to do.  Decide if this is really garbage or not. 

     

    My old school solution was to set the loop index to the 'end of loop' value.  The loop would then exit on the iteration. 

     

    Rudedog

    Friday, December 07, 2007 7:45 PM
  • Booleans, like any value type, live on the stack.  So there's nothing extra for the GC to track.  Setting the loop itereator to the end value will work, but it's not necessarily a direct break... any code after you set the end value will still run, unless you add more code to manage this situation.

    -Ryan / Kardax
    Friday, December 07, 2007 10:40 PM
  •  Hani Felemban wrote:

    Thanks for replay but the case is various

     
     

     

    class PrintTabuler
    {
    PrintTabuler(string[,] data)
    {

    static int startItem ;

     

    for (int i = startItem; i < data.GetLength(0); i++)

    {

    for (int ii = 0; ii < data.GetLength(1); ii++)

    {

    e.Graphics.DrawRectangle(Pens.Black, new Rectangle(X, Y, colWidth[ii], 20));

    e.Graphics.DrawString(data[i,ii], txtFont, txtBrush, new RectangleF(X, Y, colWidth[ii], 20));

    X += colWidth[ii];

    }

    X = space + marginRight;

    Y += 20;

     

    if (Y > 0.95 * pageHieght)

    {

    e.HasMorePages = true;

    startItem = i + 1;

    break;

    }

    }

    // these two lines should run only in the last page.

    // but the "break" does not effect these

    if (startItem == data.GetLength (0))

    {

    e.HasMorePages = false;

    startItem = 0;

    }

}
}
 
Is this what you want?
Monday, December 10, 2007 2:25 AM
  •  

    It's worked also Mr. AlexBB
    Monday, December 17, 2007 9:32 PM
  •  Hani Felemban wrote:

     

    It's worked also Mr. AlexBB

     

    What makes you think I am a Mr. Maybe I am Mrs. or Ms. or Sr. or Dr. or Hon. or St.. or...  whateverSmile

     

    In this Christmas season I am actually St. AlexBB.Smile

    Thursday, December 20, 2007 12:32 AM