getting the name of a passed parameter
-
I have a reporting interface that generates 100 or so reports. Each report uses a varying combination of 11 total parameters (from 0 - 4 at most). So I could have: Report report = new Report(StartDate, EndDate, NameID, LocID) //or just Report(StartDate, NameID) //or Report() My problem is: how do I get my constructor to accept the parameters and know which ones were passed without overloading it a gillion times. Is there a way to a generic type parameter and get the actual name of the parameter passed? If I have the name of the parameter I can cast to the right type. I could even create a hashtable with all my parameter names, types and build my AddParameter method off of the parameter name. Actually If I can get the name of the parameter, and I know the most that will be passed is four, five constructors and be good to go...I think. Is this possible? Robert
-
I have a reporting interface that generates 100 or so reports. Each report uses a varying combination of 11 total parameters (from 0 - 4 at most). So I could have: Report report = new Report(StartDate, EndDate, NameID, LocID) //or just Report(StartDate, NameID) //or Report() My problem is: how do I get my constructor to accept the parameters and know which ones were passed without overloading it a gillion times. Is there a way to a generic type parameter and get the actual name of the parameter passed? If I have the name of the parameter I can cast to the right type. I could even create a hashtable with all my parameter names, types and build my AddParameter method off of the parameter name. Actually If I can get the name of the parameter, and I know the most that will be passed is four, five constructors and be good to go...I think. Is this possible? Robert
-
I have a reporting interface that generates 100 or so reports. Each report uses a varying combination of 11 total parameters (from 0 - 4 at most). So I could have: Report report = new Report(StartDate, EndDate, NameID, LocID) //or just Report(StartDate, NameID) //or Report() My problem is: how do I get my constructor to accept the parameters and know which ones were passed without overloading it a gillion times. Is there a way to a generic type parameter and get the actual name of the parameter passed? If I have the name of the parameter I can cast to the right type. I could even create a hashtable with all my parameter names, types and build my AddParameter method off of the parameter name. Actually If I can get the name of the parameter, and I know the most that will be passed is four, five constructors and be good to go...I think. Is this possible? Robert
Besides albean suggestion, which makes it very flexible for extending parameters, you can always set properties of a class and pass this class as a parameter to your Report constructor. Acting as a substitute for God, he becomes a dispenser of justice. - Alexandre Dumas
-
I have a reporting interface that generates 100 or so reports. Each report uses a varying combination of 11 total parameters (from 0 - 4 at most). So I could have: Report report = new Report(StartDate, EndDate, NameID, LocID) //or just Report(StartDate, NameID) //or Report() My problem is: how do I get my constructor to accept the parameters and know which ones were passed without overloading it a gillion times. Is there a way to a generic type parameter and get the actual name of the parameter passed? If I have the name of the parameter I can cast to the right type. I could even create a hashtable with all my parameter names, types and build my AddParameter method off of the parameter name. Actually If I can get the name of the parameter, and I know the most that will be passed is four, five constructors and be good to go...I think. Is this possible? Robert
Martin Fowler had a solution for this type of problem in Refactoring with Introduce Parameter Object (p. 295). The concept is to "Create a new class to represent the group of parameters you are replacing."
ReportParams rp = new ReportParams (StartDate, EndDate, NameID, LocID);
Report report = new Report (rp);Your new parameter object could have all the permutations of parameters and remove those details from the Report class.
α.γεεκ
Fortune passes everywhere.
Duke Leto Atreides -
Excellent idea. Obviously never thought of it.
-
Martin Fowler had a solution for this type of problem in Refactoring with Introduce Parameter Object (p. 295). The concept is to "Create a new class to represent the group of parameters you are replacing."
ReportParams rp = new ReportParams (StartDate, EndDate, NameID, LocID);
Report report = new Report (rp);Your new parameter object could have all the permutations of parameters and remove those details from the Report class.
α.γεεκ
Fortune passes everywhere.
Duke Leto AtreidesOff to Amazon.com I guess. Thanks for the tip.