0

I have an object called "Agent". Agent has among others, 10 properties named lab1 thru lab10. I need to assign these properties to text boxes on a form txtFieldLabel1 thru txtFieldLabel10. In the example below the left side of the operator in the loop is fine. I can't figure out the right side. I need to dynamically build the property name based on the index of the loop. This seems it should be fairly simple and similar to the left side of the operator.

       for (int i = 1; i <= 10; i++)
        {
            tlp.Controls["txtFieldLabel" + i.ToString()].Text = Agent.lab + i.ToString();
        }
Mark Clark
  • 157
  • 2
  • 14
  • 1
    post what Agent is, the class I mean – sertsedat Jul 28 '15 at 14:27
  • 1
    You need to use reflection. Look at http://stackoverflow.com/questions/1196991/get-property-value-from-string-using-reflection-in-c-sharp or http://stackoverflow.com/questions/10338018/how-to-get-a-property-value-using-reflection – Backs Jul 28 '15 at 14:29

4 Answers4

2
Agent.GetType().GetProperty("lab" + i).GetValue(Agent, null);

That will get the value of the property that, using reflection, is defined as labX, where X is the value of i.

Edit: changed to GetValue(Agent, null) instead of GetValue(Agent), as the overload for the single object parameter was introduced in .NET 4.5.

Thomas Stringer
  • 5,682
  • 3
  • 24
  • 40
  • While I like the simplicity of this answer and the similar one below, I get an error "No Overload for method 'GetValue' takes 1 arguments" – Mark Clark Jul 28 '15 at 16:10
  • I changed to ".GetValue(Agent, null)" and it works great! – Mark Clark Jul 28 '15 at 16:29
  • @MarkClark `GetValue(Object)` overload was introduced in .NET 4.5. I have changed my answer to reflect the pre-4.5 next-best overload of `GetValue(Object, Object[])`. – Thomas Stringer Jul 28 '15 at 17:06
1

You could use reflection as others mentioned, but it would be easier if you created Dictionary<int, string> inside your Agent class and define those KeyValuePairs with keys from 1 to 10 and desirable values corresponding to those keys. Here is an example:

public class Agent
{
    public Dictionary<int, string> Lab = new Dictionary<int, string>();

    public Agent()
    {
        this.Lab.Add(1, "Value 1");
        this.Lab.Add(2, "Value 2");
        this.Lab.Add(3, "Value 3");

        // ...

        this.Lab.Add(10, "Value 10");
    }
}

Then you could call it like this:

var agent = new Agent();
for (int i = 1; i <= 10; i++)
    tlp.Controls["txtFieldLabel" + i].Text = agent.Lab[i];
msmolcic
  • 6,407
  • 8
  • 32
  • 56
0

You can get the properties' values using reflection:

var agent = new Agent();
//...
var value = agent.GetType().GetProperty("lab" + i).GetValue(agent);

(Note: Agent is the class-name, while agent is the variable/instance)


Another (better/cleaner?) solution might be to implement the lab-properties as an array or List<string>, e.g:

class Agent {
    public List<string> Labs {get;set;}
}

Then you could iterate over all Labs:

for (var i=0; i<agent.Labs.Count; i++) {
    tlp.Controls["txtFieldLabel" + (i+1)].Text =
        agent.Labs[i];
}
M4N
  • 94,805
  • 45
  • 217
  • 260
0

This seems it should be fairly simple and similar to the left side of the operator.

It's not simple at all; you can do it using reflection, but that's pretty advanced programming.

I suspect there are more meaningful property names available to you than lab1, lab2, etc. and strongly recommend you use them. Anyone who has to come back to this code in a few months will be grateful.

Edmund Schweppe
  • 4,992
  • 1
  • 20
  • 26