Restore EE End Property Public Property Let Restore Enable Events(Value As Boolean) this. I've used your error numbering approach in my own implementation, but I wanted to keep the example focused on the persistence.Restore EE = Value End Property Public Property Get Restore Screen Updating() As Boolean Restore Screen Updating = this. Adding in a bunch of error handling is necessary, but I didn't want it to clutter the code.
I'd appreciate a review of the class and the test harness. Restore SU = DEFAULT_RESTORE_SCREEN_UPDATING End Sub 'By default, restore the settings if we didn't do it explicitly Private Sub Class_Terminate() If this. Restore CA End Property Public Property Let Restore Calculation(Value As Boolean) this. Raise -1000, "CExcel Properties", "Properties have already been persisted." End If End Sub Public Sub Restore() 'Only restore the settings that we want restored '(which by default is all of them) With Application If this. I like your point about the Restore on Terminate being exposed as a property.
Are there other Excel Properties that could be added? Restore CA = Value End Property Public Property Get Restore Display Alerts() As Boolean Restore Display Alerts = this. I actually added the Terminate event in the CR textbox, so I wasn't in IDE/OOP mentality when I added it.
Class Module: 'These constants define the default restoration behaviors for the class Private Const DEFAULT_RESTORE_CALCULATION = True Private Const DEFAULT_RESTORE_DISPLAY_ALERTS = True Private Const DEFAULT_RESTORE_ENABLE_EVENTS = True Private Const DEFAULT_RESTORE_SCREEN_UPDATING = True 'Set this to true to ensure a persisted state is restored, even if the consumer forgets to restore Private Const RESTORE_ON_TERMINATE = True 'Private members stored in a Type Private this As TMembers Private Type TMembers Calculation As Xl Calculation State Display Alerts As Boolean Enable Events As Boolean Screen Updating As Boolean Restore CA As Boolean Restore DA As Boolean Restore EE As Boolean Restore SU As Boolean Is Persisted As Boolean Is Restored As Boolean End Type 'Set the default restoration behaviours on intialize Private Sub Class_Initialize() this. Restore DA End Property Public Property Let Restore Display Alerts(Value As Boolean) this. Likewise the members of the this type, and their usage, were typed in the CR textbox, so abbreviating them was easier than typing them in full without Intellisense.
Restore DA = Value End Property Public Property Get Restore Enable Events() As Boolean Restore Enable Events = this. So yes, the Type members should match the property names.
Public b Screen Updating As Boolean Public b Enable Events As Boolean Public xl Calc As Xl Calculation Public Sub Persist App Settings() b Screen Updating = Application. Namely: Furthermore, if the restore procedure is never called, due to an error or code branching, then the settings are never restored. The class exposes properties that allow you to disable certain Excel properties from being restored, and a series of constants allow you to define the default behaviour of the class. Restore End If End Sub Public Property Get Restore Calculation() As Boolean Restore Calculation = this. It's logical, for my workload, to save and restore the calculation property, but it mightn't be a property that you want to change, or want to have to turn off every time, so the constants were a way of making the class adaptable to a developer's preferred default behavior.
Calculation End Sub Public Sub Disable App Settings() With Application . Also, there are at least 4 properties that are regularly persisted, but usually only 3 of the 4 are persisted. I've written a class, and a test harness that shows how it can be used in a stack, and without explicitly restoring the properties. Also, you'll want a test that covers this nasty gotcha: The constants were designed so that a developer could alter the default behaviors without having to edit any code.
Calculation = xl Calculation Manual End With End Sub Public Sub Restore App Settings() With Application . That's potentially a lot of private variables to declare and assign, and it can quickly get confusing about when and where properties are set and unset. You'll want a test for each property you're persisting, that fails when the getter doesn't return the value that was persisted; you'll want a test for each getter again, that fails when it doesn't return the expected value after it was saved and then modified.
Calculation = xl Calc End With End Sub That works well enough when the procedure is viewed in isolation, but it mightn't work very well once the call stack gets deeper. For example, you'll want a test that fails when twice.
Is Persisted Then 'Save all of the settings With Application this.
Or it could be just me not liking the VB And the client code that's handling it can use the enum instead of hard-coding error numbers everywhere, and if you have more errors to raise, you just add an enum member and VBA automatically makes it - and more importantly you want your tests to fail when something changes in the class under test, that changes the behavior that the test is documenting.
Or it could be my C# eyes being used to such explicit calls.