Property vs. Function in a class

S

Sean

I've got a quick question about classes. I've written a class to facilitate file access. One of its abilities is to parse delimited text into strField(), and set a variable iFieldCount (internal to the class).

In the documentation, it says that Property Get shouldn't contain any executable code. Should the retrieval of information from the class be a property or a method (function)? The following is what I'm doing, and seems to work fine, but it does contain 'executable' code. Is this just a matter of protocol, or will the use of a Property Get in this way come back and bite me somehow?


Property Get Field(iField As Integer) As String

If iField > 0 And iField <= iFieldCount Then
Field = strField(iField)
Else
Field = ""
End If

End Property


Thanks, Sean
 
G

Graham R Seach

Sean,

In the traditional OO sense, a property is an attribute. It's a value that
describes (at least partially) some characteristic of the object to which it
refers. For example, a dog can have the following attributes (properties),
which describe the dog:
* Breed
* Name
* Date of Birth
* Colour

The same dog can perform a number of actions, which are termed "methods".
* Bark
* Eat
* Bite Postman

The assertion that a Property Get should not contain any executable code is
not quite accurate in my view, because there are many times when a property
must be calculated. In order to calculate the return value, code sometimes
must perform a whole raft of actions and calculations, perhaps even making
method calls to carry out the work. I'd say that constitutes "executable
code". I think what the author really intended to say, was that a Property
Get should not change any object, object property, or permanent datum.

In your case, the GetField() procedure is defiinitely a method. An easy way
to tell if it should be a property or a method, is to examine its name. If
the name contains a verb (an action word) - it's a method, because a verb
says it is doing something. Another way, is to examine its purpose. Unless
it's returning an object's attribute or characteristic, it's a method.

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
---------------------------

I've got a quick question about classes. I've written a class to facilitate
file access. One of its abilities is to parse delimited text into
strField(), and set a variable iFieldCount (internal to the class).

In the documentation, it says that Property Get shouldn't contain any
executable code. Should the retrieval of information from the class be a
property or a method (function)? The following is what I'm doing, and seems
to work fine, but it does contain 'executable' code. Is this just a matter
of protocol, or will the use of a Property Get in this way come back and
bite me somehow?


Property Get Field(iField As Integer) As String

If iField > 0 And iField <= iFieldCount Then
Field = strField(iField)
Else
Field = ""
End If

End Property


Thanks, Sean
 
S

Sean

Excellent explination. Thank you!!

In looking back over my class definition, I see part of my problem is what I named the properties and methods. I had a property named GetFilesize, which definately should be a property of the file object, I just named it poorly. I will be renaming in Filesize.

The GetField in my sample is still a little bit of a grey area to me because it satisfies arguments for properties and methods, but I can work through that. The strField() array is a private array within the class which is built when a specific prototype of a ReadLine method is called. Since I'm not changing the class data in any way, and the executable code is merely validation of the requested field, I think I'll leave it as a property, but rename it to just 'Field'.
 
G

Graham R Seach

Sean,

The determination of whether something should be a property or method is based on what the *class* represents. If the class represents a file, for example, then maybe FileSize could be a property. If the class represents a file, and GetField uses ReadLine to return a line from a file, then I would suggest that it, too, should probably be a property named "Field" or "Line".

You have to be specific about object naming and procedure purpose, otherwise you can end up writing spaghetti code.

Remember, if the procedure is an action that's carried out *by the object*, then it's a method. If it's an attribute that describes something about the object, it's a property.

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
---------------------------

Excellent explination. Thank you!!

In looking back over my class definition, I see part of my problem is what I named the properties and methods. I had a property named GetFilesize, which definately should be a property of the file object, I just named it poorly. I will be renaming in Filesize.

The GetField in my sample is still a little bit of a grey area to me because it satisfies arguments for properties and methods, but I can work through that. The strField() array is a private array within the class which is built when a specific prototype of a ReadLine method is called. Since I'm not changing the class data in any way, and the executable code is merely validation of the requested field, I think I'll leave it as a property, but rename it to just 'Field'.
 
Top