CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 1999
    Posts
    11

    how to return 2 value?

    in C++, I can do it by:

    void getParam(int *width, int *height);
    int width, height;
    getParam(width, height);



    but how can I do it in JAVA because JAVA does not accept point?




  2. #2
    Join Date
    Nov 1999
    Location
    Tilburg-Breda, Noord-Brabant, The Netherlands
    Posts
    135

    Re: how to return 2 value?

    hi there,

    Could you explain more specific what you want (and not how to do it in C++) ?

    Greets,
    Jan Meeuwesen.


  3. #3
    Join Date
    Jan 2000
    Location
    CA, USA
    Posts
    305

    Re: how to return 2 value?

    Well,
    In Java, all objects are passed by reference.
    So any modifications that you make to the passed
    arguments will be reflected back as in C++.

    Thanks.



  4. #4
    Join Date
    Nov 1999
    Location
    Indianapolis, IN
    Posts
    191

    Re: how to return 2 value?

    Returning multiple values via a return statement is not allowed in Java. Thankfully.

    If you truly wish to get back multiple values, consider creating a new object to hold your values. Often times, at least in business apps, you'll end up adding behavior and/or additional attributes anyway.

    If this doesn't seem appropriate in your case, then maybe rethink your design a bit, such that you don't need multiple return values. The entire Java language is written without multiple return values...

    good luck,


  5. #5
    Join Date
    Jun 1999
    Posts
    31

    Re: how to return 2 value?


    You can either break it up into two methods

    int getWidth();
    int getHeight();



    or return an object with all the information

    java.awt.Dimension getSize();






  6. #6
    Join Date
    Aug 1999
    Location
    San Diego
    Posts
    155

    Re: how to return 2 value?

    Actually if you pass 2 object parameters into the method such as : myMethod(Object a, Object b), then the objects can be modified, since it a reference your passing.

    So in terms of you argument, it would look a little more like:


    public void getParam(Integer width, Integer height) {
    width = new Integer(100);
    height = new Integer(100);
    }




    However this may be more effort than its worth (when you want plain int values).
    If you have to send them back at the same time, you could also return a Dimension Object.

    Dustin


Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured