Class Properties

D

Dave

I was just wondering what the difference between using readonly properties
in a class definition instead of using a function?

Example:

Public Class myClass
Private privateValues() as Long
'
' do other stuff to initialize everything here
'
Public ReadOnly Property getPrivateLongValue_p(ByVal index as integer)
as Long
Get
return privateValues(index)
End Get
End Property
' versus
Public Function getPrivateLong_f(ByVal index as Integer) as Long
return privateValues(index)
End Function
End Class

This may not be a good example of a reason to use one over the other, but
was interested to know what the differences are?
 
S

smith

as I understand it, property accessors are functions so go with what you
feel makes more logical sense for the user-devs.

-smith
 
H

Herfried K. Wagner [MVP]

Dave said:
I was just wondering what the difference between using readonly properties
in a class definition instead of using a function?

Classes represent entities.
Properties represent the entitiy's attributes.
Methods represent operations that can be performed by the entity.

So it's on the one hand a decision based on semantics. On the other hand,
the characeristics of the implementation should influence the decision.
Accessing a property should be "fast", there should not be long calculations
involved. Properties simply return the value of an attribute (sure, this
can include some calculation work or similar). Methods do some work before
returning in most cases.
 
G

Guest

There are a few differences that might prompt one to use a property instead
of a Method such as accessing class properties thru the PropertyDescriptor
Collection Class in the case of accessing classes with different properties
in a routine that doesn't know before hand what class will be passed to it as
an object.
 
D

Dave

Thanks to all who responded. Your explanations have helped me get a better
understanding and have been very helpful.

Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top