Javanese Python
Javanese Python

Reputation: 73

ComboBox Items from a for loop method

I. MY METHOD

 private void itemLoop(int start, int finish)
            {
                finish += 1;
                System.Object[] itemobj = new System.Object[finish];
                for (int i = start; i <= finish; i++)
                {
                    itemobj[i] = i;
                }
            }

II. I can't call the method. How can I call this method to create a range of items in ComboBox using loop?

this.cbMonth.Items.AddRange(itemLoop(1, 12));

Upvotes: 1

Views: 111

Answers (2)

kiafiore
kiafiore

Reputation: 1693

The best way is the one that Fubo has explained.

this.cbMonth.Items.AddRange(Enumerable.Range(1,12));

But if you have to use your method, you have to return the array:

            private System.Object[] ItemLoop(int start, int finish)
            {
                finish += 1;
                System.Object[] itemobj = new System.Object[finish];
                for (int i = start; i <= finish; i++)
                {
                    itemobj[i] = i;
                }
                return itemobj;
            }

At the end you can call this:

this.cbMonth.Items.AddRange(ItemLoop(1, 12));

Upvotes: 1

fubo
fubo

Reputation: 45947

there is no need to implement a custom range method

this.cbMonth.Items.AddRange(Enumerable.Range(1,12));

Upvotes: 4

Related Questions