if anyone can help on this one.
i'm trying to create one function to consolidate all my set functions for
the attributes in my class, and one function for all the get methods. can
i do that? so far i was able to just send multiple arguments into the set
function and that worked. but no way can i fiugure out how to return more
than one value and one data type from the get function.
thanks in advance
08-01-2002, 08:05 PM
Kent
Re: set & get functions
Maz,
Firstly, that's not good form. Secondly, assuming you have an EXCELLENT reason
for doing so, the only way is to do this:
public Object getMethod(int whichField) {
Object retVal = null;
if (whichField == ...) {
retVal = ...;
} else if () {
}
return retVal;
}
You could also use reflection:
public Object getMethod(String fieldName) {
Object retVal = null;
try {
Field f = this.getClass().getField(fieldName);
retVal = f.get(this);
} catch (Exception e) {
//field not found
}
return (retVal);
}
Note that the return value must always be Object. Native types will need
to be wrapped by their class equivalents. eg. int = Integer, float = float.
Regards,
Kent
"maz" <mazenalie@yahoo.com> wrote:
>
>if anyone can help on this one.
>i'm trying to create one function to consolidate all my set functions for
>the attributes in my class, and one function for all the get methods. can
>i do that? so far i was able to just send multiple arguments into the set
>function and that worked. but no way can i fiugure out how to return more
>than one value and one data type from the get function.
>thanks in advance
>