Page 1 of 1

value handling in methode with faults

Posted: Mon Aug 05, 2019 7:28 pm
by seal58
Hi,
i've been searching for a fault in my code for hours. Now I'd like to get any help from this forum.
Long time ago I programmed with Turbo Pascal quite well. Now I just started with Java and get many problems, then - OOP is rather hard to get in.
In VMD I wrote this small method:

Code: Select all

public void voltageToToneAndOctave (double cvValue, byte toneNr, byte octaveNr)
   {
   toneNr = (byte)((cvValue % 1) * 12.); // isolate tone value 0..0.999
   octaveNr = (byte)cvValue;
   Log("voltageToToneAndOctave 843 value="+cvValue+" toneNr="+toneNr+"  octaveNr="+octaveNr);
   }
With Log() I can see that it works fine.

That method is called in another method:

Code: Select all

   voltageToToneAndOctave(newSignalValue1, tone1, octave1);
   Log("DVM1 437 value="+newSignalValue1+" tone1="+tone1+" octave1="+octave1);         
tone1 and octave1 are declared in [User variables & Functions] as byte.
The calling method always gets zero values given back. And I can't find out why.
I already renamed the variables in order to prevent from "hidden" variable name collisions.
I also tryed to use int type instead of byte.
Has anybody an idea what is going on?

Re: value handling in methode with faults

Posted: Mon Aug 05, 2019 7:48 pm
by Captain
(This should probably be in the Module Designer forum, but I guess the mods will move it...)

The problem here is that when you call a method, primitives (int, byte, etc) are passed by value, not by reference (like objects). When you call voltageToToneAndOctave, the variable values are copied to toneNr and octaveNr, and you are only changing the local variables. Nothing is passed back to the calling method.

Here's a couple of solutions I can think of:

- Pass objects instead of primitives to voltageToToneAndOctave (which is a little more expensive...)

- Use the return statement at the end of voltageToToneAndOctave, and return something that can hold two byte values (like a byte array)

Re: value handling in methode with faults

Posted: Mon Aug 05, 2019 8:21 pm
by Cherry Dan
Markus is correct. You're modifying local variables in your function, and these variables are not getting returned.

Based on your code, it would be much smarter to have two functions:

public byte voltageToTone(double cvValue)

and

public byte voltageToOctave (double cvValue)

Be sure to return the value at the end!

All the best,
Dan

Re: value handling in methode with faults

Posted: Mon Aug 05, 2019 9:30 pm
by nekomatic
There are good Java resources from JetBrains Academy at https://hi.hyperskill.org/

Re: value handling in methode with faults

Posted: Tue Aug 06, 2019 7:23 pm
by seal58
First - Thanks for very quick reply. And also excuse me, unfortunately I started this theme in the wrong forum area.

As immediate solution I followed the advice to write my convertion method as two byte type methods (former "function"). That helped me to finish my small module project.

To be honest, I still try programming Java as I did with PASCAL and of course many things go wrong each time. Therefore I can't hardly beliefe, that parameter handling in Java does not work and is such different from my experience. My first Voltage Modules have been my first touch with Java and my "step into new area of OOP". I wanted to get a little bit familiar with Module Designer.

My try to place an own class within Module Designer failed with "illegal start of expression". Uff!

Now I started studying my new JAVA Programming book. It will take some time to read and understand these more than 1000 pages. Then I'll come back.

See you.