Results 1 to 6 of 6
  1. #1
    buntyindia is offline Member
    Join Date
    May 2011
    Posts
    13
    Rep Power
    0

    Question Dynamically create objects, set value and call

    Hi,

    In my application I am processing XML using xstream and getting mGnrt object then setting it to wrapper after adding headerObjects and sending to DataService for execution.

    Below is a sample code, for doing same...In my application for each service request I have to do the below thing but Objects will change means the "Some" part in below code is variable and will be replaced by service name ..
    how to achieve this means to deal it dynamically....

    XStream xstream = new XStream();
    xstream.alias("SomeRequestGnrt",SomeRequestGnrt.class);
    SomeRequestGnrt mGnrt=(SomeRequestGnrt)xstream.fromXML("<SomeReque stGnrt> <UserId>nits</UserId> <Pswd>1234</Pswd></SomeRequestGnrt>");

    SomeRequestWrapper someRequestWrapper = new SomeRequestWrapper();
    someRequestWrapper.setSomeRequestGnrt(mGnrt);
    RequestObjectHeader requestObjectHeader = someRequestWrapper.getRequestHeader();
    DataService.LognAuth(SomeRequestWrapper,ctxt);

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,575
    Rep Power
    23

    Default

    You create objects "dynamically' by using the new statement.
    You can not change variable names at execution time. They are set when you type in the code.

    There are some tricks you can do with the classes in the reflect package.

    Can you define your requirements a bit more?

  3. #3
    buntyindia is offline Member
    Join Date
    May 2011
    Posts
    13
    Rep Power
    0

    Default

    Requirement is....user select a service on GUI and in textbox we have a inputXMl..user update it with value and submit...in logic using xstream create the object and submit to DataService layer for processing....


    As you said can do using reflection how we can achieve this using reflection ..means as you see above code "Some" part will always be variable

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,575
    Rep Power
    23

    Default

    Another approach to "dynamically" create variable names at execution time would be to use a Map. The keys would be the variable names.

  5. #5
    buntyindia is offline Member
    Join Date
    May 2011
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Another approach to "dynamically" create variable names at execution time would be to use a Map. The keys would be the variable names.
    Is there any example for both approach

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,575
    Rep Power
    23

Similar Threads

  1. Replies: 1
    Last Post: 01-22-2009, 04:25 PM
  2. Replies: 0
    Last Post: 02-17-2008, 08:57 AM
  3. Dynamically create a button, but what happened?
    By love2java in forum AWT / Swing
    Replies: 1
    Last Post: 02-17-2008, 12:01 AM
  4. How to create widgets dynamically
    By sarbuland in forum Advanced Java
    Replies: 0
    Last Post: 02-06-2008, 08:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •