EDIT:
If you got here looking for the fast answer: It is possible to do this, but the overview of the function does not show the result I was looking for.
The answer 'Code Contracts' by Aidin gives an error if you omit one of the optional, but required (because...) parameters. This seems to be the easiest way to force it upon your user, but requires some explanation to the user of the function.
The answer 'Place Logic in Class' by Shawn Holzworth fitted my need of easily showing the user of the function what options they have, but it requires a different approach and a lot of code rewriting.
I'm trying to create a function that accepts different sets of parameters. In the code I check what set of parameters are given and based on that I do something to the URL in order to receive a set of values containing personal information from insurance companies (that is why there is no other code). A search on Google gave no results on making the parameters conditional on each other, therefore I'm not sure it is even possible.
Basically the idea is to have a function call like this:
dobFormatted
, infodateFormatted
, (BSN | insuranceID | lastname | postalcode, Homenummer[, Homenummeradd])
[, insuranceType]
Allowing the function to be used in these ways:
dobFormatted, infodateFormatted, BSN
dobFormatted, infodateFormatted, BSN, insuranceType
dobFormatted, infodateFormatted, insuranceID
dobFormatted, infodateFormatted, insuranceID, insuranceType
dobFormatted, infodateFormatted, lastname
dobFormatted, infodateFormatted, lastname, insuranceType
dobFormatted, infodateFormatted, postalcode, Homenummer
dobFormatted, infodateFormatted, postalcode, Homenummer, insuranceType
dobFormatted, infodateFormatted, postalcode, Homenummer, Homenummeradd
dobFormatted, infodateFormatted, postalcode, Homenummer, Homenummeradd, insuranceType
So the code forces the call to the function to contain the required information. I'm aware that this could be achieved by creating multiple versions of the function requiring different sets of parameters and returning the output of the current function. However I think that would make the code less readable and maintainable. Besides, there are also 3 ways of completing the function using only 3 strings, but the output depends on the 3rd parameter.
Currently I have this code:
public static string getVecozoURL(string dobFormatted, string infodateFormatted,
string BSN = "", string insuranceID= "", string lastname= "",
string postalcode = "", int Homenummer = 0, string Homenummeradd = "",
string insuranceType = "Both") {
string URL= "";
if (FCelfproef.BSN(BSN)) {
// Do stuff
} else if (!insuranceID.IsEmpty()) {
// Do stuff
} else if (postalcode .Length > 4 && Homenummer > 0) {
// Do stuff
} else if (!lastname.IsEmpty()) {
// Do stuff
}
// Do some other stuff
return URL;
}
Is there a easy way to force this or do I have to inform all people that are going to use this about the way the function works?