Assign new value to an object in a function
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Assign new value to an object in a function

Hybrid View

  1. #1
    Join Date
    Oct 2004
    Location
    Ho Chi Minh, Vietnam
    Posts
    126

    Assign new value to an object in a function

    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 05:08 AM. Reason: missing the question :)

  2. #2
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,450

    Re: Assign new value to an object in a function

    Do you get any errors? Please post full text here.
    Norm

  3. #3
    Join Date
    Oct 2004
    Location
    Ho Chi Minh, Vietnam
    Posts
    126

    Re: Assign new value to an object in a function

    Quote Originally Posted by Norm View Post
    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.

  4. #4
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,450

    Re: Assign new value to an object in a function

    Not the code, The full text of the error message.

    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 11:14 AM.
    Norm

  5. #5
    Join Date
    Oct 2004
    Location
    Ho Chi Minh, Vietnam
    Posts
    126

    Re: Assign new value to an object in a function

    so, no error.

  6. #6
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,450

    Re: Assign new value to an object in a function

    Go back and read the update to my last post
    Norm

  7. #7
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Assign new value to an object in a function

    What is wrong with the assignment? Given everything in Java is pointer
    Read this article, it explains how Java passes parameters and why your code won't work.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  8. #8
    Join Date
    Aug 2005
    Posts
    198

    Re: Assign new value to an object in a function

    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:

    Code:
    public class MyClass
    {
    	private InfoSupplier infosupplier;
    
    	public final void myFunc()
    	{
    		Info infocarrier = new Info();
    		RefObject<Info> tempRef_infocarrier = new RefObject<Info>(infocarrier);
    		this.infosupplier.supply(tempRef_infocarrier);
    		infocarrier = tempRef_infocarrier.argvalue;
    	}
    }
    
    public class InfoSupplier
    {
    	private Info info;
    	public final void supply(RefObject<Info> ic)
    	{
    		ic.argvalue = this.info;
    	}
    }
    //----------------------------------------------------------------------------------------
    //	Copyright  2006 - 2010 Tangible Software Solutions Inc.
    //	This class can be used by anyone provided that the copyright notice remains intact.
    //
    //	This class is used to simulate the ability to pass arguments by reference in Java.
    //----------------------------------------------------------------------------------------
    public final class RefObject<T>
    {
    	public T argvalue;
    	public RefObject(T refarg)
    	{
    		argvalue = refarg;
    	}
    }
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  9. #9
    Join Date
    Oct 2004
    Location
    Ho Chi Minh, Vietnam
    Posts
    126

    Re: Assign new value to an object in a function

    @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.

  10. #10
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Assign new value to an object in a function

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  11. #11
    Join Date
    Aug 2005
    Posts
    198

    Re: Assign new value to an object in a function

    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

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center