hi everybody!
-
Well, if one can help - it would be nice! I have a simple property that returns an object of class MyClass. Within this property i defines the "get" as well as the "set" accessors. Somthing like... public MyClass MyPropertyName { get{return _myPropertyName;} set{_myPropertyName = value;} } The "get" accessor, as we all know, returns a reference to the _myPropertyName, so outer code can easily change the _myPropertyName public fields. The "set" accessor however change the whole object, that is, replace the current _myPropertyName with the specified "value" object. The question: Is it possible to lock the fields of _myPropertyName from changing when accessing them using the "get" accessor? Ok, I know that i can define the "MyClass" fields as readonly fields and initialize them within the instance constructors, however, these field must be writeable in my application. I also know that i can use internal field in "MyClass" that inidicates whether the instance is readonly and check this value whenever a property is gonna be changed by outer code. What i looking for is a solution that eliminates the need of much code to handle this requirement. Ok, if anyone faced the same requirement and solve this problem in ease way, and could post his solution - it would be nice. Thanks in advanced, elaj.
-
Well, if one can help - it would be nice! I have a simple property that returns an object of class MyClass. Within this property i defines the "get" as well as the "set" accessors. Somthing like... public MyClass MyPropertyName { get{return _myPropertyName;} set{_myPropertyName = value;} } The "get" accessor, as we all know, returns a reference to the _myPropertyName, so outer code can easily change the _myPropertyName public fields. The "set" accessor however change the whole object, that is, replace the current _myPropertyName with the specified "value" object. The question: Is it possible to lock the fields of _myPropertyName from changing when accessing them using the "get" accessor? Ok, I know that i can define the "MyClass" fields as readonly fields and initialize them within the instance constructors, however, these field must be writeable in my application. I also know that i can use internal field in "MyClass" that inidicates whether the instance is readonly and check this value whenever a property is gonna be changed by outer code. What i looking for is a solution that eliminates the need of much code to handle this requirement. Ok, if anyone faced the same requirement and solve this problem in ease way, and could post his solution - it would be nice. Thanks in advanced, elaj.
The easy way to solve this: public MyClass MyPropertyName { get{return _myPropertyName.Clone();} set{_myPropertyName = value;} } They get a copy, so any changes are not reflected in your object.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Well, if one can help - it would be nice! I have a simple property that returns an object of class MyClass. Within this property i defines the "get" as well as the "set" accessors. Somthing like... public MyClass MyPropertyName { get{return _myPropertyName;} set{_myPropertyName = value;} } The "get" accessor, as we all know, returns a reference to the _myPropertyName, so outer code can easily change the _myPropertyName public fields. The "set" accessor however change the whole object, that is, replace the current _myPropertyName with the specified "value" object. The question: Is it possible to lock the fields of _myPropertyName from changing when accessing them using the "get" accessor? Ok, I know that i can define the "MyClass" fields as readonly fields and initialize them within the instance constructors, however, these field must be writeable in my application. I also know that i can use internal field in "MyClass" that inidicates whether the instance is readonly and check this value whenever a property is gonna be changed by outer code. What i looking for is a solution that eliminates the need of much code to handle this requirement. Ok, if anyone faced the same requirement and solve this problem in ease way, and could post his solution - it would be nice. Thanks in advanced, elaj.
There is no way of making an object dynamically read-only wihtout logic in the class. An alternative could be to make a wrapper class that only exposes the contents from the class as read-only. Example:
public class MyClassReader {
private MyClass _daClass;
public MyClassReader(MyClass daClass) {
_daClass = daClass;
}public int SomeProperty { get { return _daClass.SomeProperty; } }
public string SomeOtherProperty { get { return _daClass.SomeOtherProperty; } }}
Another alternative could be to make the properties virtual and make a class that inherits from MyClass and overrides the properties and throws an exception if the set accessor is used. A bit of code in both cases, but both these alternatives has the advantage that it leaves the original MyClass unchanged (or mostly unchanged) and unaware of all the fuss.
--- b { font-weight: normal; }
-
The easy way to solve this: public MyClass MyPropertyName { get{return _myPropertyName.Clone();} set{_myPropertyName = value;} } They get a copy, so any changes are not reflected in your object.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
Interesting and, in retrospect, obvious answer. I didn't think of that. Thanks!
Rob Manderson My bloghttp://robmanderson.blogspot.com[^]