简体   繁体   中英

c# do-while loop how to reset a variable?

 int line = 0;///i want to reset this back to 0///
    private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
    {
        int y = 0;
        Console.WriteLine("1st " + line);
        do
        {
            e.Graphics.DrawString(invoiceList[line], new Font("Arial", 10, FontStyle.Regular), Brushes.Black, 50, 200);
            Console.WriteLine("2nd " + line);
            line += 1;
            Console.WriteLine("3rd " + line);
            y += 1;

            if (y > 0)
            {
                e.HasMorePages = line != invoiceList.Count();

                Console.WriteLine("4th " + line);
                break;   
            }  
        } while (line < invoiceList.Count());
        Console.WriteLine("6th " + line);
    }

How to reset the variable line ? because its just keep on adding up when i press the print button from printPreviewDialog resulting System.ArgumentOutOfRangeException .

EDIT 1: Sorry guys it looks like i wasn't clear. its this line e.Graphics.DrawString(invoiceList[line], new Font("Arial", 10, FontStyle.Regular), Brushes.Black, 50, 200); that is giving me an error.

So first this is what happen when i press the print button

as you can see from the picture "6th( line ) = 12" or index 12.

so when i press the print button again from the print preview to print a physical copy it will show this . Sorry I'm really bad at explaining things.

you have to declare the variable inside the printDocument1_PrintPage . so that it will initialized with 0 each time. So the event signature may looks like:

  private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
    {
      int y = 0, line = 0;
      //Rest of code here
    }

Alternatively you can reset it to 0 after the looping, if so the snippet will be like:

int line = 0;
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
 {
    int y = 0;
    // code here
    Console.WriteLine("6th " + line);
    line=0; for the next time the value starts from 0
 }

If you need i to be in the outer scope, you must reset i = 0; somewhere in the inner scope.

After your Console.WriteLine("6th " + line); you can add one line immediately after and just say i = 0;

This way your code does what you're asking, and you still have that outer scope for i .

// This set to 0

        if (y > 0)
        {
            e.HasMorePages = line != invoiceList.Count();

            Console.WriteLine("4th " + line);
            break;   
        }  
    } while (line < invoiceList.Count());
    Console.WriteLine("6th " + line);
   line = 0;
}
private void printDocument1_BeginPrint(object sender, PrintEventArgs e)
    {
        line = 0;
    }

guys thanks for your time, i figured it out :P.

I used this program for sorting, hope it will help someone.

static void SortArray()
    {
        int[] array = { 40, 5, 6, 1, 90, 23, 5 };
        //Array.Sort(array);
        int i = 0;

        while (i < array.Length)
        {
            bool flag = false;
            if (i < array.Length - 1)
            {
                if (array[i] > array[i + 1])
                {
                    int temp = array[i];
                    array[i] = array[i + 1];
                    array[i + 1] = temp;
                    //reset i
                    i = 0;
                    flag = true;
                }
            }
            if (!flag)
            {
                i++;
            }
        }
        //PRINT
        for (int a = 0; a < array.Length; a++)
        {
            Console.WriteLine(array[a].ToString());
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM