Property Procedures

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,832
Office Version
  1. 365
  2. 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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
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?
 
Upvote 0
Yes, thanks.
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,214,552
Messages
6,120,172
Members
448,948
Latest member
spamiki

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