value handling in methode with faults

For discussion of the Voltage Modular synthesis ecosystem.
Post Reply
User avatar
seal58
Posts: 354
Joined: Fri Jul 12, 2019 5:28 pm
Location: Rostock, Germany
Contact:

value handling in methode with faults

Post 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?
Attachments
screenshot of log prints
screenshot of log prints
CV-Meter Test.jpg (26.01 KiB) Viewed 3202 times
User avatar
Captain
Posts: 103
Joined: Sat Aug 25, 2018 11:12 am

Re: value handling in methode with faults

Post 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)
Cherry Dan
Site Admin
Posts: 256
Joined: Fri Jul 27, 2018 5:36 pm

Re: value handling in methode with faults

Post 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
User avatar
nekomatic
Posts: 64
Joined: Mon Jul 08, 2019 8:52 pm

Re: value handling in methode with faults

Post by nekomatic »

There are good Java resources from JetBrains Academy at https://hi.hyperskill.org/
User avatar
seal58
Posts: 354
Joined: Fri Jul 12, 2019 5:28 pm
Location: Rostock, Germany
Contact:

Re: value handling in methode with faults

Post 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.
Post Reply

Return to “Voltage Modular”