c# - How to pass long list of parameters to method Or any other best way to achieve this -


i passing 20+ arguments of different types method. there should clean way pass this. can please me.

i can pass array of object having these 20+ arguments in target method have put checks on type. way pass long list of arguments.

code: sample code not full

private datatable createdatatable(string[] args) {     datatable dt = new datatable();     dt.clear();      foreach (var arg in args)     {     dt.columns.add(arg);     }      return dt; } 

i can pass array in method because arguments of same type.

datatable dt = createdatatable(new string[] { "projectid",                          "parentprojectid",                          "projectname",                          "creationdate"); 

now here have more 20+ values of diff types following

int projectid = 100; int? parentprojectid = somecondition ? 10 : null; string projectname = "good project" datetime createddate = datetime.now; . . . 

in method assign values columns.

assignvaluestodatatable(dt, arguments list ......) // implementation this. here passing 20+ arguments. private datatable assignvaluestodatatable(datatable dt, arguments list ........) {     datarow row = dt.newrow();     row["projectid"] = projectid;     .     .     .      dt.rows.add(row); } 

can please help. using c#4

edit: above code example real code more interesting know best method achieve this.

from coding horror (jeff atwood)

the more parameters method has, more complex is. limit number of parameters need in given method, or use object combine parameters.

above quote blog post. code smells

thanks.

pass object represents data instead:

private datatable assignvaluestodatatable(datetable dt, project project) {      row["projectid"] = project.id;      row["projectname"] = project.name;      ... } 

with

public class project {     public int id {get;set;}     public string name {get;set;}     ... } 

of course, question becomes : why use datatable at all? since project class better metaphor / mechanism expressing data, , list<project> (or bindinglist<project>) ideal collection of such.

(hint: very, very, very use datatable - or maybe less that)


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -