-
set & get functions
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
-
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
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks