Interate with objects

  • Thread starter Thread starter Victor Pereira
  • Start date Start date
V

Victor Pereira

Hi can i iterate with objects ? i mean, if i have button1, button2, button3
and i wanna change it´s text, can i do something like this ?

for(int i = 0;i < 3;i++)
this.button.text = "Text = " + i;

Thanks in advance,

Victor
 
No, you have to put the objects in some aggregate structure first.

Having said that, you'll find that all controls on your form are
already in the aggregate structure "Controls", so you can loop through
that:

foreach (Control c in this.Controls)
{
Button b = c as Button;
if (b != null)
{
... do something with button b ...
}
}
 
Victor Pereira said:
Hi can i iterate with objects ? i mean, if i have button1, button2, button3
and i wanna change it´s text, can i do something like this ?

for(int i = 0;i < 3;i++)
this.button.text = "Text = " + i;


The easiest thing to do is make an array:

Button[] buttons;

and then populate it. Then you can have your code *exactly* as above
(but with buttons rather than button).
 
Bruce and Jon, Thanks for your reply!

Reguards,

Victor
Victor Pereira said:
Hi can i iterate with objects ? i mean, if i have button1, button2, button3
and i wanna change it´s text, can i do something like this ?

for(int i = 0;i < 3;i++)
this.button.text = "Text = " + i;


The easiest thing to do is make an array:

Button[] buttons;

and then populate it. Then you can have your code *exactly* as above
(but with buttons rather than button).
 

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

Back
Top