Bulli
Bulli

Reputation: 125

Change a Property with a Loop

I have 10 Labels and a Object with 10 Properties. I want to have a Loop that put on the first Label the first Property of the object, on the second Label the second Property from the object and so on..

My problem is to change the property Name of the object with the loop because it isn't a string...

_Label1.Fill = object .Color1;     
_Label2.Fill = object .Color2;    
_Label3.Fill = object .Color3; 

Upvotes: 0

Views: 226

Answers (1)

Romano Zumbé
Romano Zumbé

Reputation: 8079

You could achieve this with reflection like this:

for(int i= 1; i<= 10; i++)
{
     Label[i-1].Fill = (Color)object.GetType().GetProperty("Color" + i.ToString()).GetValue(object, null);
}

I assume, that you have the labels in an array or list and that the Color Properties are of type Color

Upvotes: 3

Related Questions