Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Property Read-Only.
Message
General information
Forum:
Visual FoxPro
Category:
Object Oriented Programming
Miscellaneous
Thread ID:
00865736
Message ID:
00921210
Views:
26
Hi Fabio,
I think I spoke too soon. This doesn't resolve the problem for me, but I think it's because of a difference in understanding. When I think of a "read-only" property, I think of one that cannot be set from outside of the class but CAN be set from within the class. It is essentially a PROTECTED property, except that you can read its value from outside of the class. However, your example with the HIDDEN assign method doesn't seem to accomplish that. Consider the following code.
LOCAL clsMyClass as MyClass

clsMyClass = CREATEOBJECT("MyClass")
WITH clsMyClass

	* This property is public so I should be able to set it.
	.PublicProperty = "Set"
	
	* This property is read-only so I should NOT be able to set it.
	.ReadOnlyProperty = "Set"
	
	* I should be able to read both of these properties.
	? .PublicProperty
	? .ReadOnlyProperty	
	
ENDWITH


DEFINE CLASS MyClass AS Custom

	PublicProperty = "Unset"
	ReadOnlyProperty = "Unset"
	
	HIDDEN PROCEDURE ReadOnlyProperty_Assign(varNewValue)
		*This.ReadOnlyProperty = varNewValue
	ENDPROC

	HIDDEN PROCEDURE Init
		This.PublicProperty = "Initialized"
		This.ReadOnlyProperty = "Initialized"
	ENDPROC

ENDDEFINE
This code DOES make the property read-only from outside of the class. However, unless I'm missing something (which is entirely possible,) it also makes it read-only from within the class. You cannot change the value of .ReadOnlyProperty from within the class. If I uncomment the line in the Assign method, the property is no longer read-only at all. Even though the procedure is hidden, it always executes. You may not be able to call it directly from outside of the class, but it always executes when you attempt to change the value of the .ReadOnlyProperty.

If there was a way to determine whether the value was being set from inside or from outside the class, then the Assign method could conditionally accept the value or return an error, but I'm not aware of a way to do that.
John Groft
Consultant
Computer Task Group, Inc.
Previous
Reply
Map
View

Click here to load this message in the networking platform