I'm starting to have a lot of Flags in my application and it's hard to keep track of the status of them all.
Sometimes I want to temporarily change a flag if it's a certain value and then restore the value.
The simple way is.
Of course the problem with that is when you have sixteen flags and your whole sub turns into turning flags on and off and five lines of code that's actually doing anything.Code:Private ASub As Sub Dim flg As Boolean flg = Flags(0) Flags(0)=False ' do sub stuff Flags(0) = flg
So what I tried was this which failed immediately:
(and now looking back at it I can see that I was just doing a stupid bugassert and not passing a variable name at all. I know better. probably...)
SetRestoreFlags would actually be a class module that was initiated with whatever I want the flags to be.Code:' Flags in whatever modules. I like to group them all into one. Private fAllowFlickering As Boolean Private fShowDieRolls As Boolean ' Both Properties below have an associated Property Let of course. Public Property Get AllowFlickering As Boolean AllowFlickering = fAllowFlickering End Property Public Property Get ShowDieRolls As Boolean ShowDieRolls = fShowDieRolls End Property Private Sub SetRestoreFlags(ParamArray Variables()) Dim n As Long For n = LBound(Variables) To UBound(Variables) MsgBox Variables(n) Next n End Sub Private Sub Form_Load() SetRestoreFlags AllowFlickering = False, ShowDieRolls = True End Sub
Any time a Flag status is sent to the Class, it would save the current value in a collection or array.
This would mean that the Flags sent must be Public or Friend in scope.
On Class Terminate it would restore all flags to whatever value they were.
Example case:
You can turn Definitions on or off (Definitions of words used in the app). They appear in an auxilliary window.
So In the DisplayDefinition sub, it would check this flag and kick out if disabled or display the definition if enabled.
But maybe there's a definition I want the user to see and I want to override the setting.
So what I'm trying to achieve is doing this without having to send every possible flag.
I mean I still have to type out all the flags but it's one line of code plus the class declaration.
But that's still better than however many lines of code at the beginning and end of any sub it applies to.
Not to mention, much easier debugging because it's all in one place.
And I want to send the flags in any quantity and in any order.
If it recognizes the flag-property variable then it knows what to do with it.
I know there are a lot of ways to do this and I've been doing all of them.
But I'm looking for something more automated so I can just send off a list of flags and the status I want them to be temporarily and when it's done the class goes out of scope and the sub exits with flags set as they were when the sub was entered.




Reply With Quote