-1

I have some variables and there results which are going into database.

For example

        float[] qty1;
        Quote quote = new Quote();
        quote.qty1 = qty1[0];
        quote.qty2 = qty1[1];
        quote.qty3 = qty1[2];
        quote.qty4 = qty1[3];

I try to make this process more dynamic

           for (int i = 0; i <= 3; i++)
            {
            
            quote.qtyi = qty1[i];
            }

please help me how i can use quote.qtyi and value of i, so it will read quote.qty1, quote.qty2, quote.qty3, quote.qty4

Michaelweston
  • 149
  • 1
  • 11

1 Answers1

-1

Use reflection:

for (int i = 0; i <= 3; i++)
        {
        
        quote.GetType().GetProperty("qty"+i).SetValue(quote,qt1[i]);

        }

quote.getType().getProperty("qty"+i) -> Gets the property called "qty"+i from the object "quote".

.setValue(quote,qt1[i]) -> sets the value "qt1[i]" to the property (if she exists) of the object "quote".

My first answer had TYPOS, now it's compilable

Marco Silva
  • 191
  • 1
  • 11