I have many pieces of code that need to be executed once during initialization.
I need to use a boolean flag this way because it is in the event
bool _fuse;
void PerformLayout()
{
Size size;
if (!_fuse)
{
size = _InitialContainerSize;
_fuse = true;
}
else
size = parent.Size;
}
Since this happens often, I did something to make this boolean look like a fuse :
So, I did this:
bool _fuse;
void PerformLayout()
{
Size size;
if (!Burnt(ref _fuse))
size = _InitialContainerSize;
else
size = parent.Size;
}
If it is initialized to false, the query returns false once, set the switch to true, and successive calls return true.
public static bool Burnt(ref bool value)
{
if (!value)
{
value = true;
return false;
}
else
return true;
}
Of course, this works, but I'm only moderately satisfied, and I'm sure there are more elegant solutions. What would be yours?
source
share