Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trouble updating parameters between two classes

 
Eddie Shakarchi
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys, great forum - very useful.
When using more than one class, i'm having troubles passing variables between them (ie updating paramets when they change). In a simple example (with only 1 variable) there are two classes - Fader and FaderEffect (in seperate files). the faderEffect class has a constructor in it which creates a new faderEffect
public FaderEffect(float volume){
volumeLevel = volume;
}
In the fader class, i have code which creates a new object:
FaderEffect fader = null; // setting it up
if (fader==null){
fader = new FaderEffect(volume);
}
Which is fine. In the fader class i also have code which automatically updates the volume parameter:
public void parameterUpdate(String paramname, Object value) {
if (paramname.equals("volume"))
volume = new Float((String) value).floatValue();
}
But the problem is that the volumeLevel variable in the faderEffect class doesn't get updated because it only gets updated when you create the new object... so how can i get the varaibles to update automatically? At the moment i have to re-create a new 'Fader' each time...
I hope someone can help, as this is the last piece of the puzzle!
Thanks guys (wasn't sure which forum to put this in... i think it's pretty beginnner stuff, right? )
Eddie
[ April 06, 2004: Message edited by: Eddie Shakarchi ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eddie,
Welcome to JavaRanch!
One way: give the FaderEffect class a setVolume() method. Call it from Fader.parameterUpdate() to pass in the new value for volume. The setVolume() method should, of course, set the member variable to the new value.
 
Tobias Hess
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what Fader and FaderEffect means, but for me it look's like you have the same information stored in two different places. The fields volume in Fader and volumeLevel in faderEffect are the same peace of information, right? Then, just store it in fader only. Hand over a reference to the associated fader in the constructor of FaderEffect, and store it.

Whenever you need the volumeLevel in FaderEffect, call a get-Method of that reference instead.
 
Eddie Shakarchi
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies guys!
The Fader class takes in a specific type of audio object and converts it to an array (of amplitudes), which is passed to FaderEffect to be processed. In this example, FaderEffect simply changes the values in the array by multiplication... but i have some other rather nasty reverb and oscillating effects which are more complicated and pass many different types of varaibles - i can understand them but not the basic java (i come from a maths background not java - should learnt the java basics better in the first place!)
Ernest, i *think* i understand that... going to try it out - i get caught up on the easy bits though! i'm sure i'll be posting again :roll:
Tobias - you are correct that i am storing the same info in two places - i thought it was a bit stupid at the time! But i'm not sure what a handover reference bit. When you have this bit:
- do i make a method called getVolume() - where?
Also, if i change my contructor to public - then what do i change the code in the Fader class to?
Many thanks guys
[ April 06, 2004: Message edited by: Eddie Shakarchi ]
 
Eddie Shakarchi
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also ernest, if i'm passing through like 8 variables, then is it ok to have a method for each, or is that sloppy?
Thanks guys - this site is fantastic! It's now my new homepage heh
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Eddie Shakarchi:
Also ernest, if i'm passing through like 8 variables, then is it ok to have a method for each, or is that sloppy?

You almost certainly want to have a method to set each individually. Methods with 8 parameters are bad.
 
Eddie Shakarchi
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i think i'm nearly there
I've now got the constructor set as:
And a setvolume method:
And i'm trying to call it in Fader using this kind of thing:
What am i doing wrong here? When i compile Fader.java it tells me C
non-static method setVolume(float) cannot be referenced from a static context
FaderEffect.setVolume(volume);
^

[ April 06, 2004: Message edited by: Eddie Shakarchi ]
 
Tobias Hess
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When you have this bit:
theFader.getVolume();
- do i make a method called getVolume() - where?

To the associated Fader-object. For this to work, you only have to add the getVolume()-method to your Faderclass, and change the creation of FaderEffect as shown here:
 
Eddie Shakarchi
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fixed it:
You guys are the best!
 
Eddie Shakarchi
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys
Sorry - this creates a pretty complicated problem with my other classes - i've posted it in a seperate thread in the intermediate forum - please check it out: http://www.coderanch.com/t/373352/java/java/Constructor-super-value-as-first
[ April 06, 2004: Message edited by: Eddie Shakarchi ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic