Hi,
I want to pass an object to another function (of another class) to assign new value to it: code below
Code:
public class MyClass
{
InfoSupplier infosupplier;
public void myFunc(){
Info infocarrier = new Info();
this.infosupplier.supply(infocarrier); //infosupplier has already been declared and allocated elsewhere
//the problem is after this line, infocarrier still doesn't have the new values (members)
//which means the assignment inside the function supply() is NOT effective
}
}
public class InfoSupplier
{
Info info;
public void supply (Info ic)
{
ic = this.info ;//this.info has been declared and allocated, assigned value elsewhere
}
}
What is wrong with the assignment? Given everything in Java is pointer
Thanks.
Last edited by choconlangthang; August 8th, 2010 at 04:08 AM.
Reason: missing the question :)
Do you get any errors? Please post full text here.
Nope, there's no error, which cause a run-time bug for me.
Currently my workaround is that I assign all values (primitive type, not object type), however it is not what it SHOULD be, and lengthy.
[code ]
public void supply (Info ic)
{
ic = this.info ;//this.info has been declared and allocated, assigned value elsewhere
ic.member1.value = this.info.member1.value; //value is of primitive type, like int, boolean,...
//rest of code
}
[/code]
As of "full text", I suppose you mean my real code. It's just the same with some name changing.
If there are no error messages, can you show what the values are by using println()s to display the variables when you set their values and when later you try to use them, to show what is wrong.
Code:
public void supply (Info ic)
{
ic = this.info ;
The ic used in this code is a local variable and it disappears (goes out of scope) when the method exits.
Change supply() to return a reference to an Info object and return this.info.
Last edited by Norm; August 8th, 2010 at 10:14 AM.
All parameters in Java are passed 'by value', which means that you can't pass back a changed 'top-level' reference - only modify properties of the object passed.
Here's how we handle converting from languages which allow pass by reference (C#, VB, and C++) to Java:
@Norm: thanks but returning is not what I want, since I might need to return multiple values.
@keang & David: thanks for excellent article & code. So the trick is to wrap your object inside another object (like David's: T argvalue), making your original object sub-level.
So the trick is to wrap your object inside another object (like David's: T argvalue), making your original object sub-level.
No, you should write your code in a manner is appropriate to the language you are using. David's code is, as he says, how they convert other languages that do pass by reference to Java. This 'trick' reduces the code changes required to get the non Java code ported to Java. I don't imagine they write new code like this.
In Java you would either change the contents of the object passed in to the method or you return the new object instance.
That's absolutely right keang - converters just try to produce the equivalent code, which will often be different than the code you should be writing from scratch.
David Anton
Convert between VB, C#, C++, & Java www.tangiblesoftwaresolutions.com
Instant C# - VB to C# Converter
Instant VB - C# to VB Converter
Bookmarks