We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Changing the class of a variable
Page Index Toggle Pages: 1
Changing the class of a variable (Read 718 times)
Changing the class of a variable
Jul 10th, 2007, 6:13pm
 
Is there any way to unset a variable?

I'm making a music-box toy using ddf's Minim library, and I want to be able to change the type of generated wave - but all the waves have a different class, eg. SineWave, SawWave, etc.

Is there any way to change the class of a variable, or to unset it so that I can set it again?
Re: Changing the class of a variable
Reply #1 - Jul 10th, 2007, 7:50pm
 
no. unsetting a variable would be assigning null, false or 0 to it. you are talking about casting a variable, that means changing it's type. that's not possible in java, it's a strictly typed language.

but there is help: if you're only using the generate() function from the classes you mentioned, you could use the interface instead: AudioSignal.

basically an interface is a common set of functions that a class can declare to have (implement). that way you can interact with objects of different types in a common way. in your example that would mean that you would have your variable be of type AudioSignal. since both SineWave and SawWave implement AudioSignal the variable can house both these types of objects with the limitation, that you can only call the functions defined by the interface (see link above).

AudioSignal mySignal = new SignWave();
// do smth here ...
mySignal = new SawWave();
// ...

F

Re: Changing the class of a variable
Reply #2 - Jul 11th, 2007, 12:14pm
 
Thanks! I didn't know you could do that.

On the page that you linked to it seems that the Oscillator class will be the one I'll use.

Thanks again for your help.
Re: Changing the class of a variable
Reply #3 - Jul 11th, 2007, 2:50pm
 
right, that will work too.
Page Index Toggle Pages: 1