Property Procedures

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,459
Office Version
  1. 2019
Platform
  1. Windows
I have read an article which states the following:

" ..... when you use Property procedures, your class is aware of changes made to a property
value. If you use public variables, you have no way of knowing when a consumer procedure changes
a given property’s value."

Can some one please explain / give an example of the statement:

"If you use public variables, you have no way of knowing when a consumer procedure changes
a given property’s value."


Thanks


 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
37,191
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Let's say you have a class called Class1 and you need to expose a Boolean value from it. You can do this in two ways - either using a property procedure, or using a public variable. If you use a public variable, like:
Code:
Public Changed as Boolean
the calling code can change the value using code like this:
Code:
Dim myClass as Class1
Set myClass = New Class1
myClass.Changed = True

which will change the value of the variable directly, and your class code has no way of reacting to this change, other than say by periodically checking the value of the variable.

If, on the other hand you use a property procedure:
Code:
' code in Class1
Private m_bChanged As Boolean
Public Property Get Changed() As Boolean
   Changed = m_bChanged
End Property
Public Property Let Changed(ByVal bChanged As Boolean)
   m_bChanged = bChanged
   msgbox "The variable has been changed!"
End Property

the only way the calling routine (code for that is the same either way) can change the value of the mb_Changed variable is by calling the property procedure which can automatically run whatever code you require (in this example, the msgbox code).

Does that help?
 

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,459
Office Version
  1. 2019
Platform
  1. Windows
Yes, thanks.
 
Last edited by a moderator:

Forum statistics

Threads
1,137,298
Messages
5,680,678
Members
419,924
Latest member
Dhamodharan992

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