Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I create a loop to process variables of a class.

Status
Not open for further replies.

Exie

Programmer
Sep 3, 2003
156
AU
Hi Folks,

I have a class (aka Form Bean) which has a number of variables (39 actually) and I need to copy all the values into a new class (in my case a Bean object). At current I have a setup like so:

private CenterBean convertCenter(CenterForm myForm) {
return new CenterBean(myForm.val1,myForm.val2,myForm.val3 ....);
}

This gets rather messy from a code point of view, how could I clean it up ?

I figured I could pass through the form to the bean constructor, but then I would still want a loop to assign var1 in the form to var1 in the bean....

Can I do something like:
for (int i=0;i < 40;i++){
var = myForm.var;
}

... of course, the variables are not an array. Any thoughts are welcome.

 
You could use JavaBean introspection and reflection :

Code:
  public void invoke(String mName) {
    try {
      Method m = 
        ChoiceApplet04.class.getDeclaredMethod(
        mName, null);
      m.invoke(this, null);
    }
    catch (Exception ex) {
      System.err.println(&quot;no such method: &quot;+mName);
    }
  }

(from )
 
Hi,

introspection & reflection eh ? I recently attended a Mastering Struts course (with no Java experience of course) and the instructor rambled on about this over 30 minutes!

Needless to say, I didn't understand a word he said, but as nobody else did either, I wasn't too concerned about it. I suppose it was more important than I thought!

Thanks for the example, I'll have a mess around and try and work it out.

Tim.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top