Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Problem passing values to a function

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! I'm surprised beacause I can't solve (and neither understand) a problem related I think to pass variables to a function.

Basically, I have this scene: a class MyClass containing some variables, and a MainClass where I initialize a MyClass object and I work with it. I need to change the variables into MyClass object obj passed to func, and keep the changes after the func call. The code is shown below:






And the results are:

During func:
obj.a = 1
obj.b = 20
After func:
obj.a = 0
obj.b = 20

¿¿¿??? WTF!! I can't understand that! Actually I'm simplifying the code a lot, but I'm sure that the error comes from here! Can anybody explain why the "a" variable isn't modified and "b" (and "c" too) are?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That code won't even compile. But after making a couple of small tweaks to get it to compile, no, it does not produce the results you say it does. It produces totally unsurprising results.

Try posting the actual code--an SSCCE that anybody can just copy, paste, compile, and run.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hernan Blanco wrote:Basically, I have this scene: a class MyClass containing some variables, and a MainClass where I initialize a MyClass object and I work with it. I need to change the variables into MyClass object obj passed to func, and keep the changes after the func call. The code is shown below:
...
¿¿¿??? WTF!!....


My thoughts exactly.

Actually I'm simplifying the code a lot...


Really? Classes are meant to perform a function, and in order for us puny humans to understand what it is, they should have descriptive names. All I can see so far is you saying 'I've got this class X that I want to do some weird things with, and I want it to work as I expect'.

What do you want it to do; and what do you expect as the result?

Hint: I suspect you'd be much better off giving us a real-life example (even if it's not in Java).

Winston
 
Hernan Blanco
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK. I was trying to sintetize my question as much as possible, but it seems that I wasn't explicit enough. So now I will go to specify better the situation.

I'm doing a client-server application. The Client send data contained into a Mensaje object (previously serialized with a DataOutputStream) using a DatagramSocket. The server recieves the datagram and dumps the byte array to a new Mensaje object using a DataInputStream (that's the goal of the getRequest() function); then it processes the information, and, finally, it sends the response to the client. The communication is not the problem, it works fine.

So, to simplify, I didn't add the communication module: I'm only giving to you a function called getRequest() that recieves as argument a Mensaje object, which should be modified. To simplify, I didn't add the communication module, so, into the getRequest() function I'm simulating that server recieves a byte array (called buffer) from line 20 to 30. Then, from 32 to 41, the server retrieves data from buffer, and represents it on screen. After that, coming back to main() function, the Mensaje object data is once more time printed on screen.

Now, I will paste an (I think...) SCCEE code that represents my problem.


Sample.java



Mensaje.java


The results thrown running Sample are:

During getRequest():
tipoMensaje = 0
idInvocacion = 0
idMetodo = 0
longArgumentos = 13
argumentos = [B@7c6768

After getRequest():
tipoMensaje = 0
idInvocacion = 0
idMetodo = 0
longArgumentos = 13
argumentos = [B@7c6768


But I don't understand why is it hapenning, beacause at the getRequest() invocation, I've setted tipoMensaje = 1 and idInvocacion = 1, so I was expecting the next results:

During getRequest():
tipoMensaje = 1
idInvocacion = 1
idMetodo = 0
longArgumentos = 13
argumentos = [B@7c6768

After getRequest():
tipoMensaje = 1
idInvocacion = 1
idMetodo = 0
longArgumentos = 13
argumentos = [B@7c6768



At now, I think that it's not a problem with passing values to the getRequest() function... Before I got a different problem I think, but I should to solve it now.

Thanks in advance!
Hernán.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hernan Blanco wrote:At now, I think that it's not a problem with passing values to the getRequest() function... Before I got a different problem I think, but I should to solve it now.


Well, I'm no great expert with DataStreams, but I can't see any major logic errors with what you've done so far.

However, you could confirm what's actually in the stream by adding
System.out.println("stream contents (out) = " + Arrays.toString(buffer));
right after your toByteArray(), and the contents of your string as read in with
System.out.println("args2 = " + Arrays.toString(args2));
or even possibly
System.out.println("string from args2 = " + new String(args2));

which may help to pinpoint where the error is occurring.

It also strikes me that your longArgumentos field is redundant, since it will always be the same value as argumentos.length.

Winston
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Your problem lies with

public void setIdMetodo(int im) {
idInvocacion = im;
}


Please correct same <<idMetod>> ....
 
When you have exhausted all possibilities, remember this: you haven't - Edison. Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic