Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro Subsitution in C# 1

Status
Not open for further replies.

wawalter

Programmer
Aug 2, 2001
125
US
Hello,

I am wondering if there is a way of performing macro subsitution in C#? I used to do this in Fox Pro and it is basically replacing one variable for another. Here is an example of what I am trying to do:

I have five label controls on my form lbl1 - lbl5. I want to access each of these in a loop and change their text values.


loop 5 times
cnt++;
x = "lbl" + cnt;
x.Text = "text for label " + cnt
endloop


I think I can accomplish this by creating the label controls at runtime, but I am curious if C# has something similar to macro subsitution.

Thanks for your help,
Bill
 
You can add those controls to an array, and loop through them with a foreach:
Code:
  Label[] myLabels = new Label[5];
  myLabels[0] = lblFirstName;
  myLabels[1] = lblMiddleName;
    :
  myLables[4] = lblCity;

  foreach(Label l in myLabels)
  {
   l.Text = "New label text here";
  }
This works because all Label controls (indeed, everything in .NET) inherits from System.Object, so everything is an object at heart.

Chip H.


____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
I think this will work great for my current program. Thanks a lot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top