Dynamically Reference object name

  • Thread starter Thread starter TheLostLeaf
  • Start date Start date
T

TheLostLeaf

I am trying to dynamically create the object name..... but i can't get
it to work...
From This (works)
------------------------------------------------------------------
label1.Text = newDeck[0].ToString();
label2.Text = newDeck[1].ToString();
label3.Text = newDeck[2].ToString();
label4.Text = newDeck[3].ToString();
label5.Text = newDeck[4].ToString();


To This (errors)
 
TheLostLeaf said:
I am trying to dynamically create the object name..... but i can't get
it to work...
From This (works)
------------------------------------------------------------------
label1.Text = newDeck[0].ToString();
label2.Text = newDeck[1].ToString();
label3.Text = newDeck[2].ToString();
label4.Text = newDeck[3].ToString();
label5.Text = newDeck[4].ToString();


To This (errors)
----------------------------------------------------------------
for (int i = 1,i<6,i++)
{
label+i.Text = newDeck[i-1].ToString();
}

You will need to reference the label by name or index.

ie this.Controls["label" + i.ToString()"].Text = ...

I am not sure if the string indexer is new to .net 2.0 so you might have
to iterate through the controls to find the one that matches based on
the name if using .net 1.*.


HTH
JB
 
Hi John
I have the "same" problem, but I'll use this for ASPX application
I use .Net 2.0 and this doesn't work... could you help me ?

Thanks in advance.
//Luca


John B said:
TheLostLeaf said:
I am trying to dynamically create the object name..... but i can't get
it to work...
From This (works)
------------------------------------------------------------------
label1.Text = newDeck[0].ToString();
label2.Text = newDeck[1].ToString();
label3.Text = newDeck[2].ToString();
label4.Text = newDeck[3].ToString();
label5.Text = newDeck[4].ToString();


To This (errors)
----------------------------------------------------------------
for (int i = 1,i<6,i++)
{
label+i.Text = newDeck[i-1].ToString();
}

You will need to reference the label by name or index.

ie this.Controls["label" + i.ToString()"].Text = ...

I am not sure if the string indexer is new to .net 2.0 so you might have
to iterate through the controls to find the one that matches based on
the name if using .net 1.*.


HTH
JB
 
Back
Top