1

I had a Problem lastly while i was working in Unity I had a public Variable hidden in the Editor this caused me some Proplems which i reported as Bug because for me it looked like one

I did it like this:

public bool Variable_1 = true;

The Unity Support member than said to me that nothing is wrong with my Code but i should do it like this because this would be prefered:

private bool Variable_1{
    get
    {
        return this.Variable_1;
    }
    set
    {
        this.Variable_1 = value;
    }

}
public void Set_Variable_1(bool bool){
    this.Variable_1 = bool;
}

I wanted to make it public because i want to access it from a other class

So my Question is why would some one use the second Example Code over the first one. What is the Advantage of that? Because in my eyes the first example is much simpler and takes less lines [Edit] 21.7.17: I understand that getter and setter Methods are much more flexible but in this case it won't be needed.

Thanks for taking the Time and Reading this Post. Have a nice Day and keep Coding

[Edit] 21.7.17:
Also i like to point out that i teached Programming myself so i didn't look into set and get till now. I tried to understand the example from this site www.dotnetperls.com/property which is ruffly the same as my Example now. I don't know how good this site is but i understood it best there

Genei180
  • 53
  • 8
  • I'd use the second, but make it public and get rid of that method, but that might be some weird Unity paradigm I'm not familiar with. – juharr Jul 18 '17 at 19:01
  • That duplicate does not answer this question – Jack Miller Jul 18 '17 at 19:02
  • This one might do though [Private Accessor in C#](https://stackoverflow.com/questions/17777914/private-accessor-in-c-sharp) – Jack Miller Jul 18 '17 at 19:03
  • 1
    There´s indeed no reason to make the field a private property and set its value by a public setter-method. Instead you should create a property with a public getter and setter – MakePeaceGreatAgain Jul 18 '17 at 19:04
  • 3
    @JackMiller I'm not sure about that one either. This recommended code seems very odd. – juharr Jul 18 '17 at 19:05
  • 3
    On top of everything else the second one will not work since the property is referencing itself. Are you sure this is the exact code a support member suggested using? – juharr Jul 18 '17 at 19:07
  • 1
    The proposed code also makes the getter private (which is unusual). – charles-allen Jul 18 '17 at 19:16
  • @juharr The exactly suggestion was: "However, some would advise against the usage of such variables and refer you to use getter and setter methods combined with a private variable." This is wat i thought they meant. As i tested it the code worked but its possible that i overlocked something. – Genei180 Jul 21 '17 at 14:05
  • @JackMiller this kind of answers my question, but in this case i specifically don't want it to be read only. I want the variable be changeable from another class. Which kind of defeats the purpose of private i understand that. – Genei180 Jul 21 '17 at 14:45

1 Answers1

1

There´s indeed no reason to make the field a private property and set its value by a public setter-method. Instead you should create a property with a public getter and setter.

public bool Variable_1 { get; set; }

Apart from this none of your two solutions is a good idea, a public field enables anyone to set the value to any value, making it impossible to perform any validation on the new value. This of course also applies to an auto-implemented property like the above. However if you decide some time to do implement some validation any existing client-code won´t be affected as the public API stays the same. Doing this with a public field on the other side would be a breaking change, as you´d replace a field by a property.

The second one however is overcomplicated and does not provide any advantage.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • *public field enables anyone to set the value to any value* So does the public auto property. – Ivan Stoev Jul 18 '17 at 19:09
  • 1
    If C# 6+ you can also retain the initialised value as follows: `public bool Variable_1 { get; set; } = true;` (in earlier versions you need to set the initial value in the constructor). – charles-allen Jul 18 '17 at 19:18