0

I have following class

class Names
{
    public string Name1 { get; set; }
    public string Name2 { get; set; }
    //.
    //.
    public string Name5 { get; set; }
}

I want to access all properties in FOR loop

for (int i = 1; i <= 5; i++)
{
   string varName = "Name";
   //concat string varName and variable i 
   //to access properties Name1, Name2 and so on..
}

Is it possible?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Faisal.lh
  • 29
  • 5

4 Answers4

3

It definitely is possible, but the question is, if it is advisable. You can use reflection to achieve what you want

string concatenated = string.Empty;
for(int i = 1; i <= 5; i++)
{
    var variableName = $"Name{i}";
    var type = typeof(Names);

    var property = type.GetProperty(name);
    var value = property.GetValue(names);
    concatenated += value;
}

Anyway, unless you have a very good reason to do this, I would avoid this. You are sacrificing the merits of a strong typing system for an improvement that is none.

Of course there are good reasons to use reflection, but I don't see the merits in this case.

You could concatenate the names by means of string interpolation

var concatenated = $"{names.Name1}{names.Name2}{names.Name3}{names.Name4}{names.Name5}";

The advantage is, that compiler time type checking is possible for that solution. You'll get immediate feedback, if you misspelled one of the properties, instead on runtime errors you might have to debug.

Furthermore is this solution way clearer and does not require the reader to think more than necessary. (Code is read much more often than it is written, so plan accordingly)

But if you have to code this way to achieve what you want, you should really start thinking about your design. What is the problem domain that justifies the Names class and especially that concatenation?

Paul Kertscher
  • 9,416
  • 5
  • 32
  • 57
2

Try this:

        string varName= names// object to return property value
             .GetType() // get the type
             .GetProperty("Name" + i.ToString()) //get the property of type
             .GetValue(names); // get the value of property in object

The function GetType return type of object. The function GetProperty return type of property The function GetValue return the value of property un object

henoc salinas
  • 1,044
  • 1
  • 9
  • 20
0

Properties are not designed for this. Use List, for example:

class Names
{
   public List<string> NamesList { get; set; }
}

       ...

for (int i = 1; i <= 5; i++)
{
    var result += NamesList[i];
    ...
}
Ivan Tikhonov
  • 304
  • 2
  • 17
-2
string concatedstr=String. Empty ;

for (int i = 1; i <= 5; i++) {

    string varName = "Name";
    Type t = typeof(Car); 
    PropertyInfo prop = t.GetProperty(varName + i.ToString()); 
    if (null != prop) 
        concatedstr+= prop.GetValue(this, null);
    }
}
Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
  • dumping code without the slightest trace of explanation has a very low informational and educational value. – Mong Zhu Mar 06 '18 at 07:37
  • Please add some explanation – Peyman Mohamadpour Mar 06 '18 at 07:45
  • Here concatedstr is common variable that will be used in loop to concate all properties value of class. Inside loop reflection is used. As we can not directly loop through all properties of class, we can write them separately but can't loop through. VarName +i. ToString() means Name and Current i variable concate. i.e Name1, Name2, etc PropertyInfo prop=t.GetProperty(varName +i. ToString()) will get particular property of Class. Like Name1, Name2, etc Prop.GetValue(this, null) this will give current property value like Name1 value. Let me know if you don't get it. – DS Passionate Mar 06 '18 at 07:52