VBA classes - Difference between Get Property and Function

Shads

New Member
Joined
Dec 3, 2014
Messages
5
This may be a philosophical and/or best-practise question, but what is the difference between the usage of Get properties and functions within a VBA class? For example, a class holding information about people may store the Date of Birth with associated Let and Get properties. Should the routine that returns the age of the person (by calculation from the DoB) be a Get property or a function? Is there best-practice/tradition for VB/VBA?
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi,

I think the choice of using Get or a function should be based on OOP principles.

For instance, in the case of Age, I think it should be Get (not a function) as it looks more object oriented and natural.

See how it looks then using:

Person.Age

or

Person.GetAge
 
Upvote 0
It's personal preference and convention, I'm of the opinion that a function should have an input and return something - so I'd use a property for your DoB example especially since you also have a Set property, DoB is read-write so you should really be able to access it in the same way.

There's perhaps more of a case for it being a function if it's read only. Though I'd still have it as a property.
 
Upvote 0
Hi, I created the following guide:

DEFINITIONS

Get
- return: yes
- parameter: no
- word form: adjective or not clear

Function
- return: yes
- parameter: yes/no
- word form: verb

Set
- return: no
- parameter: yes
- word form: adjective or not clear

Sub
- return: no
- parameter: yes/no
- word form: verb

QUESTION MAP

Code:
has return?
  |_ yes > has parameter?
  |  |_ yes: Function
  |  |_ no > likely verb?
  |      |_ yes: Function
  |      |_ no: Get
  |_ no > has parameter?
     |_ yes > likely verb?
     |   |_ yes: Sub
     |   |_ no: Set
     |_ no: Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,421
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top