set & get functions


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: set & get functions

Hybrid View

  1. #1
    maz Guest

    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


  2. #2
    Kent Guest

    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center