I have a volume variable that I want to be protected from modification unless the person calls a certain function. Is there a way to make it to where it can only be modified by that function other than creating a private class within the class. I suppose that creating a private class is a good idea, but I would be interested if someone else has a different approach. AudioPlayer should never be allowed to change the volume without calling SetVolume. That's the code I have here, but I wondered if people had a different way.
public class AudioPlayer
{
private class VolumeManager
{
private AudioPlayer mAudioPlayer;
public VolumeManager(AudioPlayer audioPlayer)
{
mAudioPlayer = audioPlayer;
}
private float volume;
public void SetVolume(float _volume)
{
volume = _volume;
//Do other necessary things that must happen when volume is changed
//This is the point of the question
mAudioPlayer.ModifyChannelVolume(Volume);
}
public float GetVolume()
{
return volume;
}
}
private VolumeManager mVolumeManager;
public AudioPlayer()
{
mVolumeManager = new VolumeManager(this);
}
public void ModifyVolume(float volume)
{
mVolumeManager.SetVolume(volume);
}
}