Posted By:
Darren_Hobbs
Posted On:
Monday, February 26, 2001 08:32 AM
If you know in advance what the different parameter combinations will be, you could simply overload your method (ie. write several implementations with different signatures). What I sometimes do is implement the method with the most parameters, and write 'adapter' methods that take fewer parameters, and fill in the blanks with default values, or null if appropriate. eg:
public void setFullName(String newTitle, String newForename, String newMiddlename, String newSurname) {
this.title = newTitle;
this.forename = newForename;
this.middlename = newMiddlename;
this.surname = newSurname;
}
public void setFullName(String newSurname) {
setFullName("Mr//Mrs", "", "", newSurname);
}
If you don't know what parameters you will be getting, the only real way to handle that is to add them to Hashtable or HashMap and pass that in as a parameter to your method:
Hashtable myHash = new Hashtable();
myHash.put("forename","John");
myHash.put("surname","Smith");
myMethod(myHash);
Within myMethod you can then iterate through the hashtable contents using an Enumeration or an Iterator (see the API documentation for more details).