Loop in Instantiating object

Discussion in 'Mixed Languages' started by carmz, Aug 12, 2012.

  1. carmz

    carmz MDL Senior Member

    Nov 29, 2011
    285
    7
    10
    #1 carmz, Aug 12, 2012
    Last edited by a moderator: Apr 20, 2017
    Hi,can i ask some help,is it possible to make a for loop when instantiating object of Button in C#?

    I tried it but it gives me an error...Can you please help me on this.

    Thank you in advance.

    Code:
      for(int i=0;i<10;i++)
        {
           Button btn + i = new Button();
           mypanel.Controls.Add(btn + i);
       }
    
    
     
  2. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    420
    198
    10
    #2 Calistoga, Aug 13, 2012
    Last edited by a moderator: Apr 20, 2017
    Remove the + i part.
    Code:
    for(int i = 0; i < 10; i++)
    {
        Button btn = new Button();
        mypanel.Controls.Add(btn);
    }
     
  3. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,519
    7,103
    120
    @carmz

    You can't use variables during the object instance.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...