Is there a way to protect a class variable from being modified outside of a function

Gandalf458

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);
    }
}
Joel Coehoorn

The problem, as I see it, is that even with a private field it's still somewhat intuitive and natural to want to assign directly to the field. We want to make very sure this doesn't happen. In that case, I recommend building this as a property, rather than a field, and just being disciplined about only assigning to the property:

public class AudioPlayer
{

    public float Volume 
    {
       get { return _volume_NeverSetThisDirectly;}
       set 
       {
           _volume = value;
           //Do other necessary things that must happen when volume is changed
           ModifyChannelVolume(_volume_NeverSetThisDirectly);
       }
    }
    [Browsable(false)]
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    [EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
    private float _volume_NeverSetThisDirectly; //Never assign to this directly!
}

This won't enforce it to the degree that you're asking for, but it does flip the intuitive and natural way someone will work in this class to use the value in the right way, rather than the wrong way. It's also a heck of a lot less code and complexity to maintain. The addition of the attributes largely won't effect things for someone working in this class already, but since we're changing to use social pressures rather than technical prohibitions, the more warning signs we have up, the better.

This also gives you an opening in the future when you find that one weird case where you do want to change the volume field without all of that other stuff happening, that you can do so without needing to do strange things to a private class instance.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Is there a way to protect a class variable from being modified outside of a function

From Dev

Is there a way to (actually) protect an object from being modified?

From Dev

Will this approach protect my database from being modified?

From Dev

Is there a way to protect a file from being deleted, but not from being altered?

From Dev

How to protect bash function from being overridden?

From Dev

Best way to protect javascript code from working with modified parameters array

From Dev

How to prevent class instance from being modified?

From Dev

How to prevent class instance from being modified?

From Dev

Refer to defined variable from function outside Python class

From Dev

Is there a way to protect all existing rows from being edited in an SQL table?

From Dev

Is there any way to access a variable from outside a function, explicitly as a variable and not via assignment to a function?

From Dev

In c++, if a member pointer point to some data, how to protect that data from being modified?

From Dev

Accessing a variable from outside the function?

From Dev

Is there a way to restrict a pointer from being modified by specific functions?

From Dev

In c# how to prevent a class from being modified

From Dev

Why should variable has public or private or protect but function should not in a class

From Dev

Access variable in function, outside class without error

From Dev

protect images from being copied

From Dev

How to access a CardLayout variable from an outside class?

From Dev

python obtain variable from outside of a class

From Dev

How to keep a class from being instantiated outside of a Factory

From Dev

Javascript reaching variable from outside of anonymous function

From Dev

Access a function variable within an object from outside

From Dev

accessing a variable from outside the function in python

From Dev

Calling a JavaScript variable inside a function from outside

From Dev

How to access a FREE variable of a function from outside

From Dev

Modify outside variable from within AJAX function?

From Dev

Using a defined variable from outside the current function

From Dev

accessing a variable from outside the function in python

Related Related

  1. 1

    Is there a way to protect a class variable from being modified outside of a function

  2. 2

    Is there a way to (actually) protect an object from being modified?

  3. 3

    Will this approach protect my database from being modified?

  4. 4

    Is there a way to protect a file from being deleted, but not from being altered?

  5. 5

    How to protect bash function from being overridden?

  6. 6

    Best way to protect javascript code from working with modified parameters array

  7. 7

    How to prevent class instance from being modified?

  8. 8

    How to prevent class instance from being modified?

  9. 9

    Refer to defined variable from function outside Python class

  10. 10

    Is there a way to protect all existing rows from being edited in an SQL table?

  11. 11

    Is there any way to access a variable from outside a function, explicitly as a variable and not via assignment to a function?

  12. 12

    In c++, if a member pointer point to some data, how to protect that data from being modified?

  13. 13

    Accessing a variable from outside the function?

  14. 14

    Is there a way to restrict a pointer from being modified by specific functions?

  15. 15

    In c# how to prevent a class from being modified

  16. 16

    Why should variable has public or private or protect but function should not in a class

  17. 17

    Access variable in function, outside class without error

  18. 18

    protect images from being copied

  19. 19

    How to access a CardLayout variable from an outside class?

  20. 20

    python obtain variable from outside of a class

  21. 21

    How to keep a class from being instantiated outside of a Factory

  22. 22

    Javascript reaching variable from outside of anonymous function

  23. 23

    Access a function variable within an object from outside

  24. 24

    accessing a variable from outside the function in python

  25. 25

    Calling a JavaScript variable inside a function from outside

  26. 26

    How to access a FREE variable of a function from outside

  27. 27

    Modify outside variable from within AJAX function?

  28. 28

    Using a defined variable from outside the current function

  29. 29

    accessing a variable from outside the function in python

HotTag

Archive