HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Is there a better way to call the same method with different parameters?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
samethemethodwithwaybettercalldifferentthereparameters

Problem

I have a method(C#) which takes an XmlNodeList and a String and saves the xml data into a database. That code isn't the problem (at least for the moment), but because I have multiple XmlNodeLists, each needing a different String (the heading), I end up with code like

saveToDB(students, "Students");
saveToDB(teachers, "Teachers");
saveToDB(workers, "Workers");


...etc, there's about 8 of those in total. It all works okay, but I wonder if there's a better way to call the function, rather than doing it 8 times? I guess I could wrap all of the data into an object, but I'd still have to set it up, and pull the data out before and afterwards, so that's surely just moving the problem elsewhere? Or is there simply no real way around this, and the issue lies with how I've approached this method in the first place?

I look forward to your responses.

Solution

I think you could create a new saveToDB method that requires a Dictionary inside this new method you could cycle through the Keys (string) and for each one call the current saveToDB(string, XmlNodeList).

Yes, it only move your problem: you've to prefill the dictionary, but I really think there's no other options... at one point you HAVE to enumerate your datas.

Context

StackExchange Code Review Q#18301, answer score: 7

Revisions (0)

No revisions yet.