Lots of time you see code like
static flag as boolean
if flag then return
flag = true
// rest of method
flag = false
In a simple method/event with only a handful of lines there’s probably nothing wrong with this
But what do you do when the method spans into hundreds or thousands of lines ?
And if you use the philosophy of “bail early” ?
Then your methods/events start to look like
static flag as boolean
if flag then return
flag = true
if condition then
flag = false
return
end if
if condition2 then
if condition3 then
flag = false
return
end if
else
flag = false
return
end if
flag = false
And following all those returns & making sure you reset the flag correctly in every one is tedious.
So what if you could make it so you NEVER had to worry about the flag remaining set when you returned ?
Remember that objects get destroyed when the last reference to them is removed – they automatically get cleaned up.
At first glance it seems that you should be able to do
static flag as date
if flag <> nil then return
flag = new Date()
// rest of method
return
and then when the method ends the date object would go out of scope, get destroyed and the world would be happy & safe for not forgetting to flip that flag.
Sadly because the variable is a static this wont happen – the static persists across calls and so it wont get destroyed.
So the trick is how to make the variable persist across method calls AND also get destroyed when things go out of scope.
But that doesn’t mean we’re stuck.
One issue we do have is that in the way we’ve set up our flag property its a value type which means other code can’t hold on to a reference to it to do anything later.
And because its a static declared in a method other code outside this method cannot touch it to flip its value.
So we need to deal with both these issues.
The second one is easiest to deal with – we can simply move the static from being declared in the method to a property on the class that is only used by this method (although its much more usual to see such a blocking action like this happen in several methods or events rather than just a single one)
So we’ll change
static flag as date
to a similarly named property on this class / window
By simply doing this we make it possible to use an object that, when created and then goes out of scope makes it so we can use the constructor & destructor to change out flag so things get blocked & allowed as needed.
So how do we have something flip the flag when we need ?
In theory we want our code to look like
if flag then return // remember flag is a private
// or protected property on the class
dim flipper as new FlagFlipper( ???????? )
// rest of method
So what does this “Flipper” thing looks like & how does it work ?
We’ll create a class – FlagFlipper.
And this class needs to then be able to know how to set and clear the flag we set up. But we really don’t want this new class to have to reach inside other classes to do this.
And this is where delegates come into play.
A delegate is a “type safe function pointer” which means that
it’s a function you can pass around like any other parameter
it’s type safe – you can’t just pass any old function
So our new class is going to look something like
Class FlagFlipper
Constructor( ?????????? )
// flip the flag in the target class somehow
End Constructor
Destructor()
// flip the flag in the target class somehow
End Destructor
End Class
To this we’ll add 2 delegates
Delegate SetFlag()
Delegate ClearFlag()
Note that we do not put code in these.
Recall we do not want the flag flipper to know the innards of a class to know how to do its work.
So we need to “pass” the right “function” to the flipper so it can call them without having to know anything more than the two delegates.
So how do we do that ?
Well … addressof CREATES an instance of a delegate
So that seems like we need to use that somehow
We want the actual management of the flag to be in our original class.
So the methods to set and clear the flag should be part of that class (and it really doesn’t matter if this is a class/layout/ etc) Lets suppose we were originally using a Window – so we’ll add code to the Window
In my example so far things look like
Window Window1
Event Open
foo
End Event
Method foo
if flag then return
dim flipper as new FlagFlipper( ??????????? )
foo // to make it so we can see the
// second call to foo is blocked
End Method
Private Property flag as boolan
End Window
And as stated we’re going to add two methods – setflag and clear flag
Now things look like
Window Window1
Event Open
foo
End Event
Sub foo
if flag then return
dim flipper as new FlagFlipper( ??????????? )
foo // to make it so we can see the
// second call to foo is blocked
End Sub
Private Sub SetFlag()
flag = true
End Sub
Private Sub ClearFlag()
flag = false
End Sub
Private Property flag as boolan
End Window
Now how do we have to make the Constructor & set up of the instance look so the right setflag / clearflag methods are called ?
Well … delegates & address of !
If we make the constructor look like the following
Class FlagFlipper
Constructor( setFlagMethod as SetFlag,
clearFlagMethod as ClearFlag )
// flip the flag in the target class somehow
setFlagMethod.Invoke
mSetFlag = setFlagMethod
mClearFlag = clearFlagMethod
End Constructor
Destructor()
// flip the flag in the target class somehow
End Destructor
Delegate SetFlag()
Delegate ClearFlag()
Private mSetFlag as SetFlag
Private mClearFlag as ClearFlag
End Class
Note that it holds on to a reference to the method to call to set or clear the flag
And the set up in the method that originally needed the flag looks like
Window Window1
Event Open
foo
End Event
Sub foo
if flag then return
dim flipper as new FlagFlipper( AddressOf SetFlag,
AddressOf ClearFlag )
foo // to make it so we can see the
// second call to foo is blocked
End Sub
Private Sub SetFlag()
flag = true
End Sub
Private Sub ClearFlag()
flag = false
End Sub
Private Property flag as boolan
End Window
And now, by using an object that when it goes out of scope automatically cleans up after itself we’ll no longer have to worry about “did I clear that flag ?”
Here’s the completed project