Dynamically Reference object name

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)
 
J

John B

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
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top