Wednesday, May 03, 2006

Read only class attribute

Say I have a class that has some attribute that is basically read only. Each child of the class has a fixed value for this attribute for all instances of that child class. Three possible ways to implement this are:

1. Add an element named MyAttribute. In the constructor, set the value of MyAttribute.

2. Add a function named MyAttribute that is overwritten in each child class.

3. Add an element named FMyAttribute and then add a MyField property with a read specifier of FMyField. In the constructor, set the value of FMyAttribute.

4. Add a Get method named GetMyAttribute and then add a MyField property with a read specifier of GetMyField. In each class, override GetMyAttribute.

I assume option 1 is the least desirable since it doesn't enforce read-only and basically does not fit in with the standards we are striving for. Option 4 is seems to be the most robust, but is overkill. Any opinions.

1 comment:

Brad said...

I agree; option 4 is overkill. Why would you need anything more than option 3?