0

i should construct dinamically the property name (to set it) in a web service invocation using the variables "section" and "fieldName" derived from the two nested foreach (as shown below). I tried with some code found googling "variable variables" but i didn't succeed. Any help would be appreciated. Thank you.

SyncRequest request = new SyncRequest();
[...]
foreach (String section in sections) {
    [DB query]
    DataTable dtSectionFields = drSection.GetSchemaTable();
    foreach (DataColumn column in dtSectionFields.Columns)  {
        [...]
        fieldName = [something];
        fieldContent = [something else];

        request.methodx.[section].[fieldName] = fieldContent;
    }
}
  • I guess there are coming "use reflection!" answers, but I think this should be solved strongly-typed. You might have to change your design a bit. Perhaps you can explain the example somewhat more? – CodeCaster Jun 05 '13 at 08:01
  • @DaveRook Sorry but maybe i don't understand. Yes, the next iteration overwrites the previous one, but the property should have been set in the meantime. Or did you mean something else? – user2454589 Jun 05 '13 at 08:32
  • @CodeCaster First i create a web service object ("request"), then i start looping through sections and, inside sections, through fields. What i wish to do is to replace [section] and [fieldName] placeholders in the last line ("request.methodx.[section].[fieldName]") with the two corresponding variables set by the nested loops at each iteration. Fundamentally i need to build dinamically the web service property invocation at each iteration to set it. – user2454589 Jun 05 '13 at 08:37
  • So can you make it a dictionary for example? – CodeCaster Jun 05 '13 at 08:38
  • @CodeCaster i read the "classic" thread about c# variable variables and i tried the (seemingly) most popular solution: var vartable = new Dictionary(); vartable[strLine] = new Variable(input); but maybe i didn't use it in the right way (it's obvious i'm not a c# guru). Would you please provide an example? – user2454589 Jun 05 '13 at 08:49
  • @everybody sorry for the horrible formatting of my replies but i can't make the markup work – user2454589 Jun 05 '13 at 08:53
  • I can't give an example as I still don't see where you want to go. Please show some examples with real data, no placeholders. – CodeCaster Jun 05 '13 at 09:18
  • @CodeCaster The outer foreach loop set the value of "section", the inner of "fieldName" and "fieldContent". Out of the loops, i created an instance of the web service (first line). The method is always the same so until "request.method" it's alright. Inside the inner loop i need to set dinamically the class and the property of the object to avoid hundreds of lines of "Switch". [to be continued] – user2454589 Jun 05 '13 at 11:13
  • From there i should set the value of request.method.manager.name, request.method.manager.surname, request.method.manager.gender [...] request.method.employee.name, request.method.employee.surname, request.method.employee.gender and so on. As you can see the invocation is made of four parts: first and second are fixed, third (manager, employee) is the "section" and fourth (name, surname, gender) is the "fieldName". [to be continued] – user2454589 Jun 05 '13 at 11:14
  • What i need is to "build" the complete name of the final property using the variables provided by the two loops and set their values with the value of the fieldContent variable. Sorry if you still don't understand, but i really can't imagine another to way to explain it. – user2454589 Jun 05 '13 at 11:15
  • Thanks for explaining, it is what I expected. You can indeed do this using reflection, search it. The neat solution would be to use an existing deserializer, which can just fill classes from your raw data so you don't have to manually loop over it. – CodeCaster Jun 05 '13 at 11:22

0 Answers0