c# controlling program flow l by object type while in a loop -


i working system allows users add c.v.s company database. document consists of sections , each section there can 1 or more fields. field corresponds user control mapped specific field type. user controls not bound document object in way , save data document page containing user controls there method this:

public void savedata(document document) {     // user controls on page     var usercontrols = finddescendants<usercontrol>(this);      foreach (var section in document.sections)     {         foreach (var field in section.fields)         {             if (field address)             {                 var address = field address;                  var addresscontrol = usercontrols.firstordefault(o => o.clientid.contains(field.id)) addressusercontrol;                 addresscontrol.savedata(address);             }             else if (field telephonenumbers)             {                 var telephonenumbers = field telephonenumbers;                  var telephonenumberscontrol = usercontrols.firstordefault(o => o.clientid.contains(field.id)) telephonenumbersusercontrol;                 telephonenumberscontrol.savedata(telephonenumbers);             }             ...             ...             else if (field employment)             {                 var employment = field employment;                  var employmentcontrol = usercontrols.firstordefault(o => o.clientid.contains(field.id)) employmentusercontrol;                 employmentcontrol.savedata(employment);             }         }     } } 

i not nested loops , each time new field type added conditional statement within foreach grows.

any ideas how structured more elegantly?


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -