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

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

AbuTurab

New Member
Joined
Jan 10, 2015
Messages
31
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
 

Kyle123

Well-known Member
Joined
Jan 24, 2012
Messages
2,720
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.
 

Forum statistics

Threads
1,144,376
Messages
5,724,001
Members
422,530
Latest member
Badpoisondwarf

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
Top