arteny
arteny

Reputation: 379

How to create property with set accessor acessible only outside of owner class

I need a property where set is accessible only from constructor or outside of owner class (it can't be changed within class methods). Something opposite to a private modifier. Any suggestions?

Upvotes: 0

Views: 215

Answers (2)

Max Yankov
Max Yankov

Reputation: 13327

If you have a variable that your class needs to depends to, but can't change, it seems like it belongs in some other class or struct, which your class will only have a reference of.

Upvotes: 0

Bas Slagter
Bas Slagter

Reputation: 9929

This is kind of weird. Because you are the one creating the class...make it public and just don't touch the property from inside the class (this can hold if you make sure to write unit tests to test the invariant when calling methods on the current class). But...if you really want something like this, I guess you need to wrap the properties in some other class which can only be accessed through a setter method on the current class. Or something like that.

Upvotes: 1

Related Questions