Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. file info. rss.
 From: Ken Murphy
  Where is Ken Murphy?
 Ken Murphy
 To: ian pano
  Where is ian pano?
 Metro Manila
 ian pano
Subject: RE: creating get and set methods for classes
Thread ID: 155190 Message ID: 155203 # Views: 1 # Ratings: 0
Version: Visual FoxPro 9 Category: Classes and Objects
Date: Thursday, December 27, 2007 4:03:24 PM         

> dear experts,
> DEFINE CLASS person as Custom
> 	PROTECTED fname
> 	PROCEDURE setfName()
> 		This.Fname = tcStr
> 	FUNCTION getfName()
> 		RETURN This.fname 

> if i am not mistaken, this is a good practice in OOP, providing an interface for the class users to access or assign a property instead of allowing them to directly manipulate a class' property.
> however, is it worth the effort if the class has more than 30 or so properties?
> tia,
> christian pano
> " The bad is entirely my own fault "- Andy Kramek


Out of those 30 or so properties, how many of them need to be accessed from outside of the class? Those are the properties that probably need assign/access methods. The properties that are accessed only from within the class may not need access/assign methods.

The idea here is that your class should encapsulate everything it needs - including validation of those properties. For example, if you wish to assign a value to a date property, then you need to check that the value being assigned is indeed a date value. The class should include this validation code in the assign method for that property. Were you to just make the property public (and you can do this in VFP) then it would be possible for you to assign any value to that property. Remember, VFP is not a strongly typed language. You can assign anything to a publicly defined property.

Basically, the "best practices" is to define your properties as hidden or protected and then use assign/access methods to write or read to/from them. In doing so you can validate what is being written to that property.

For more on this, check Andy's excellent blog items here:


and here:


You shall know the truth - and the truth shall set you free. (John 8:33)


creating get and set methods for classes Posted by Christian Pano @ 12/27/2007 7:13:02 AM
RE: creating get and set methods for classes Posted by Vladimir Zhuravlev @ 12/27/2007 7:55:21 AM
RE: creating get and set methods for classes Posted by Boudewijn Lutgerink @ 12/27/2007 9:02:11 AM
RE: creating get and set methods for classes Posted by Cetin Basoz @ 12/27/2007 3:09:00 PM
RE: creating get and set methods for classes Posted by Ken Murphy @ 12/27/2007 4:03:24 PM