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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Call setters from another class 1

Status
Not open for further replies.

dreampolice

Technical User
Dec 20, 2006
85
US
I have alot of lines that call setter methods. Anyway to put this all in another class and call it so I dont have to list all these in my current class?
Code:
PersonClass chalk = new PersonClass();
chalk.setHeight(request.getParameter("height"));
chalk.setWeight(request.getParameter("weight"));
chalk.setCountry(request.getParameter("country"));
chalk.setLanguage(request.getParameter("language"));
chalk.setExperience(request.getParameter("experience"));
chalk.setLevel(request.getParameter("level"));
.......//goes on for 20 more lines 
...
 
Why not just put it in a separate function and pass the object you're setting to it?
 
Add a constructor to PersonClass that takes the request as a parameter.

- Rod
 
A few questions. Why did you add Class after Person?

Why is your variable named chalk?

PersonClass should have a constructor which has all the setters you need as parameters not the request(sorry rod).

if you have JVM 1.5 or higher use enums instaed ot strings in the request.getparameter.

For the rest, sometimes you actually need some code to do some work.

Christiaan Baes
Belgium

"My old site" - Me
 
Christian said:
PersonClass should have a constructor which has all the setters you need as parameters not the request(sorry rod).

No apology needed. I'm just now picking Java back up for an upcoming job. :)

I almost mentioned that it should have the parameters based constructor, but dreampolice didn't seem to like having all those calls to request.GetParameter in the code.

Would a private function in the current class (ie PersonClass PersonFromRequest(requesttype request)), which in turn uses the constructor you suggest, be the proper way to get the GetParameter()s out of the mainline code?

- Rod
 
Code:
class PersonClass
{
  public PersonClass( Request  request )
  {
    ImportRequest( request );
  }

  public ImportRequest( Request  request )
  {
    setHeight( request.getParameter( "height" ) );
    setWeight( request.getParameter( "weight" ) );
    setCountry( request.getParameter( "country" ) );
    setLanguage( request.getParameter( "language" ) );
    setExperience( request.getParameter( "experience" ) );
    setLevel( request.getParameter( "level" ) );
    .......//goes on for 20 more lines 
    ...
  }
  ...
}
 
Thanks, it works great!
I put this part in my Bean class (Model part of the MVC):
Code:
class PersonClass
{
  public PersonClass( Request  request )
  {
    ImportRequest( request );
  }

  public ImportRequest( Request  request )
  {
    setHeight( request.getParameter( "height" ) );
    setWeight( request.getParameter( "weight" ) );
    .......//goes on for 20 more lines 
    ...
  }
  ...
}

and can now call it in my Servlet (Controller part of the MVC).
 
So dreampolice you say this is MVC???

So what if I want to use this Model in a swing application? Do I just rewrite it. Your Controller is supposed to fille the model not the view. The request is part of the view as far as I know.

So if you want to add a swingdialog/frame you just make another model?

Enlighthen me please because I do not understand.

Christiaan Baes
Belgium

"My old site" - Me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top