tags:

views:

41

answers:

2

Hi All,

I'm after some advice on the most efficient approach to stepping through a list of fields and filling in their values to a class object.

I currently accessing fields via (code isn't exact but you get the idea =] ):

private string fieldName;

private int fillValue;

SessionData rawdata = new SessionData();

var count = 1;

foreach (objecttype obj in list)
{

    fillValue = obj.valueA + obj.ValueB;

               if (count < 10)
             {
                 fieldName = "band0" + count;
             }
             else
             {
                 fieldName = "band" + count;

             }

             rawdata.GetType().GetProperty(fieldName).SetValue(rawdata, fillValue , null);

             count++;
}

This is the basic idea of how I'm filling fields "band01" to "band99" (for example) with values 1-99.

Is there any other more effective method for doing this besides writing an individula if statement for each field?

Thanks for your time.

A: 

Instead of the if statement, use

fieldName = String.Format( "band{0:00}", count)
RBW_IN
Great thanks for both your inputs!
Sykth
A: 
fieldName = "band" + count.ToString("00");
Vivek