views:

30

answers:

3

I have a class definition that I've seen other define properties that return collections of objects.

Public Property GetAllAdults() as Adults
...
End Property

I made the argument that this should be a method in the class, because it doesn't define an attribute of the class, and could not be extended with parameters. Is/Are there reasons why this should be defined as a property vs. a function?

+3  A: 

I would argue that since this is a simple Getter rather than something that actually performs some action, it is much better suited as a Property.

It's name should be changed to reflect the fact that it is a property, thus changing from GetAllAdults() to Adults.

Furthermore, I would also argue that since there is only a getter...you might think the collection is read-only, which is not the case. To make it truly read-only, you should expose the property as a ReadOnlyCollection (if possible):

Public Property Adults() as ReadOnlyCollection(Of Adult)
...
End Property
Justin Niessner
You're fast, every good idea I had, I could also find here! I resignated and deleted my answer :-P
jdehaan
That's that was kind of my thinking. I was just supprized, I'd never thought about the possibilities of using properties in place of methods.
trav801
A: 

Defining collections as properties makes it easier to do data binding.

Dismissile
A: 

I would simply rename GetAllAdults() to Adults() -- that way you satisfy the guideline of the property defining an attribute of the class. You're not extending it as you said, so why create code smoke that could simply be solved in the symantics?

George