Diana
Diana

Reputation: 274

how to change a variable name

I have this code in C# but it can go up to 1000 pages . how can I have the name of the variable be page_i without having to have a case and write out 1000 cases ?

    int i=0;
    while (i< sizeofallpages){
        switch (i)
        {
            case 0:

                PdfPage page = document.AddPage();
                break;
            case 1:

                PdfPage page1 = document.AddPage();
                break;
            case 2:

                PdfPage page2 = document.AddPage();
                break;
    }

Upvotes: 0

Views: 225

Answers (2)

JNL
JNL

Reputation: 4703

You just need to create a List of objects and then use it accordingly.

List<MyObject> list = new List<MyObject>();

for(int count=0; i<sizeofallpages; count++){
    list.add(new MyObject());
}

Then, just access the objects from the List.

Upvotes: 4

Liel
Liel

Reputation: 2437

If you like to keep references to the pages:

int i=0;
PdfPage page = null;
PdfPage[] pages = new PdfPage[sizeofallpages];
while (i < sizeofallpages)
{
    page = document.AddPage();
    pages[i] = page;
    i++;
}

After that, if you would like to use a page, simply access it by:

page[i]

Upvotes: 6

Related Questions