Class Properties vs. Functions

ransomedbyfire

Board Regular
Joined
Mar 9, 2011
Messages
121
What is the point of using class properties when one can usually just use a function instead? Is there a good time to use one instead of the other? Is one generally better than the other? Why?
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
A property is usually for storing state in a backing private variable. A function is for some calculation or evaluation of state and returning that result.

So, my rule of thumb is this:
- storing and retrieving state? --> use properties
- retrieving a calculation on stored state --> use functions

but the dividing line is a thin one, and discussion can be endless.

In performance you will not detect differences I think, so it is your own choice mainly. Part of your personal style :)
 
Upvote 0
What is the point of using class properties when one can usually just use a function instead? Is there a good time to use one instead of the other? Is one generally better than the other? Why?

I'd say that using properties makes the code more clear, because the code will be "tighter" bound to the class. If you have enough time, then you could begin by using functions and then re-factor the code to use classes - that is, if you're not comfortable with using classes in the first place. One of the best sides in Excel programming is that you can do stuff first "well this seems to work" and then easily translate that to something that is more robust, looks better and/or is faster.
 
Upvote 0
Jaymond, I think he meant functions inside a class instead of a property.
You can easily implement a class property as a class function and sometimes also the other way around...
 
Upvote 0

Forum statistics

Threads
1,219,161
Messages
6,146,657
Members
450,706
Latest member
LGVBPP

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